..


Sponsored Links

array_chunk

Array_chunk function divides an array of new shares thus creating arrays.
The function accepts three parameters in question:

  • array - (required) specifies the array to be used;
  • Size - (required) specifies how many elements will contain the new array;
  • preserve_key - (optional) can have two values:
    • true - protects the keys from the original;
    • false - (default) does not preserve the keys of the original array, but creates new keys;
Let's see some examples:





 $ A = array ("a" => "Cat", "b" => "Dog", "c" => "Mouse", "d" => "Cow");







 print_r (array_chunk ($ a, 2));



The output of the code above will be:





 Array (



  



 [0] => Array (



    



 [0] => Cat



    



 [1] => Dog



  



 )



  



 [1] => Array (



    



 [0] => Rat



    



 [1] => Cow



  



 )







 )

 

We see a second example in which we will preserve the original keys arry:





 $ A = array ("a" => "Cat", "b" => "Dog", "c" => "Mouse", "d" => "Cow");







 print_r (array_chunk ($ a, 2, true));



The output of the code above will be:





 Array (



  



 [0] => Array (



    



 [A] => Cat



    



 [B] => Dog



  



 )



  



 [1] => Array (



    



 [C] => Rat



    



 [D] => Cow



  



 )







 )



In the same category ...
E-Learning
Linux (Course) Linux (Course)
Complete guide to open-source system. From 49 €.
MySQL (Course) MySQL (Course)
Management of open-source database. From 39 €.
PHP (Course) PHP (Course)
Full course for creating dynamic Web sites. From 49 €.
Sponsored Links