PHP

Examples of Array Functions in PHP

This article includes Examples of Array Functions in PHP. Basically, here I describe the usage of array_chunk(), and array_column() functions.

The array_chunck() Function in PHP

In order to split an array into chunks we can use the array_chunk() method. The function takes three parameters. While, the first one is the array that we need to split and the second one is the length of chunks. Also, we can specify whetether to preserve the keys or not as a boolean value of true or false as the third parameter. However, this parameter is optional with a default value of false.

The following code shows example usage 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/>';


?>

Output

Original Array…
1 2 3 4 5 6 7 8 9 10 11 12
Chunks of size 3…
1 2 3
4 5 6
7 8 9
10 11 12

Chunks of size 3 along with keys (Preserving 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 ) )

Chunks of size 3 along with keys (Not Preserving Keys)…
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 ) )

Chunks of size 5…
1 2 3 4 5
6 7 8 9 10
11 12
Chunks of size 5 along with keys (Preserving 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 ) )

Chunks of size 2…
1 2
3 4
5 6
7 8
9 10
11 12
Chunks of size 2 along with keys (Preserving 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

For the purpose of retrieving 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 third parameter. However, this parameter is optional. Further, the array should be a multi-dimensional array, or the array of objects.

The following code shows example usage 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'));
?>

Output

Output of the Examples of Array Functions in PHP
Output of the Examples of Array Functions in PHP

Further Reading

Examples of Array Functions in PHP

Basic Programs in PHP

Registration Form Using PDO in PHP

Inserting Information from Multiple CheckBox Selection in a Database Table in PHP

PHP Projects for Undergraduate Students

Architectural Constraints of REST API

REST API Concepts

Creating a Classified Ads Application in PHP

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *