Date:

Share:

Examples of Array Functions in PHP

Related Articles

This article includes examples of array functions in PHP. Basically, here I describe the use of the array_chunk (), and array_column () functions.

The array_chunck () function in PHP

To split an array into chunks we can use the array_chunk () method. The function takes three parameters. Admittedly, the first is the array we need to split and the second is the length of the chunks. We can also specify whether or not to preserve the keys as a boolean value of true or false as the third parameter. However, this parameter is optional with the default value of false.

The following code shows an example use of this function.

<?php
$arr=array("a"=>1,"b"=>2,"c"=>3,"d"=>4,"e"=>5,"f"=>6,"g"=>7,"h"=>8,"i"=>9,"j"=>10,"k"=>11,"l"=>12);
echo 'Original Array...<br/>';
foreach($arr as $value)

    echo $value." ";

echo '<br/>';

$a1=array_chunk($arr, 3, true);
echo 'Chunks of size 3...<br/>';
foreach($a1 as $row=>$inner)

   foreach($inner as $value)
   
       echo $value." ";
   
   echo "<br/>";

echo "<br/><br/>";
echo 'Chunks of size 3 along with keys (Preserving Keys)...<br/>';
print_r(array_chunk($arr, 3, true));

echo "<br/><br/>";
echo 'Chunks of size 3 along with keys (Not Preserving Keys)...<br/>';
print_r(array_chunk($arr, 3));
echo '<br/><br/>';

$a2=array_chunk($arr, 5, true);
echo 'Chunks of size 5...<br/>';
foreach($a2 as $row=>$inner)

   foreach($inner as $value)
   
       echo $value." ";
   
   echo "<br/>";

echo 'Chunks of size 5 along with keys (Preserving Keys)...<br/>';
print_r(array_chunk($arr, 5, true));
echo '<br/><br/>';

$a3=array_chunk($arr, 2, true);
echo 'Chunks of size 2...<br/>';
foreach($a3 as $row=>$inner)

   foreach($inner as $value)
   
       echo $value." ";
   
   echo "<br/>";

echo 'Chunks of size 2 along with keys (Preserving Keys)...<br/>';
print_r(array_chunk($arr, 2, true));
echo '<br/>';


?>

Productivity

Original set…
1 2 3 4 5 6 7 8 9 10 11 12
Pieces of size 3…
One. Two. Three
4 5 6
7 8 9
10 11 12

3-piece chunks along with keys (preservation keys) …
array ( [0] => Array ( [a] => 1 [b] => 2 [c] => 3) [1] => Array ( [d] => 4 [e] => 5 [f] => 6) [2] => Array ( [g] => 7 [h] => 8 [i] => 9) [3] => Array ( [j] => 10 [k] => 11 [l] => 12))

3-piece chunks along with keys (no key holders) …
array ( [0] => Array ( [0] => 1 [1] => 2 [2] => 3) [1] => Array ( [0] => 4 [1] => 5 [2] => 6) [2] => Array ( [0] => 7 [1] => 8 [2] => 9) [3] => Array ( [0] => 10 [1] => 11 [2] => 12))

5 chunks
1 2 3 4 5
6 7 8 9 10
11 12
5-piece chunks along with keys (preservation keys) …
array ( [0] => Array ( [a] => 1 [b] => 2 [c] => 3 [d] => 4 [e] => 5) [1] => Array ( [f] => 6 [g] => 7 [h] => 8 [i] => 9 [j] => 10) [2] => Array ( [k] => 11 [l] => 12))

2 size chunks
1 2
3 4
5 6
7 8
9 10
11 12
2-piece chunks along with keys (preservation keys) …
array ( [0] => Array ( [a] => 1 [b] => 2) [1] => Array ( [c] => 3 [d] => 4) [2] => Array ( [e] => 5 [f] => 6) [3] => Array ( [g] => 7 [h] => 8) [4] => Array ( [i] => 9 [j] => 10) [5] => Array ( [k] => 11 [l] => 12))

The array_column () function in PHP

To retrieve the values ​​from a single column, we can use the array_column () function. Basically, the function takes the original array and the column key as parameters. Besides, we can also provide the index key as a third parameter. However, this parameter is optional. Moreover, the array should be a multidimensional array, or the array of objects.

The following code shows an example use of the array_column () function.

<?php
$book_array=array(
    array('bno'=>11,
        'btitle'=>'Java Complete Reference',
        'bprice'=>450
    ),
    array('bno'=>12,
        'btitle'=>'PHP Complete Reference',
        'bprice'=>400),
    array('bno'=>13,
        'btitle'=>'C# Complete Reference',
        'bprice'=>550),
    array('bno'=>14,
        'btitle'=>'C++ Complete Reference',
        'bprice'=>350),
    array('bno'=>15,
        'btitle'=>'Python Complete Reference',
        'bprice'=>430)
    );
  echo 'The original array...<br/>';
  print_r($book_array);
  echo '<br/>Printing only titles...<br/>';
  print_r(array_column($book_array, 'btitle'));
  echo '<br/>Printing only titles indexed by book number (bno)...<br/>';
  print_r(array_column($book_array, 'btitle', 'bno'));
?>

Productivity

Output of examples of array functions in PHP

Source

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Popular Articles