Convert array to multidimensional array

I like to convert one array to a multidimensional array. This is what I get for the web page to scrap the page, but this is not the end result I'm looking for.

Change:

Rooms: Array (
  [0] => name 
  [1] => value 
  [2] => size
  [3] =>  
  [4] => name 
  [5] => value 
  [6] => size
  [7] =>      
  [8] => name 
  [9] => value 
  [10] => size
  [11] =>  
  [12] => name 
  [13] => value 
  [14] => size
  [15] =>  
)

AT:

Rooms: Array (
  Room: Array (
    [0] => name 
    [1] => value 
    [2] => size
  ),
  Room: Array (
    [0] => name 
    [1] => value 
    [2] => size
  ),
  Room: Array (
    [0] => name 
    [1] => value 
    [2] => size
  )
)
+5
source share
1 answer

First use array_filter()to get rid of the nodes  :

$array = array_filter($array, function($x) { return trim($x) != ' '; });

// Or if your PHP is older than 5.3
$array = array_filter($array, create_function('$x', 'return trim($x) != " ";'));

Then use array_chunk()to break the array into pieces 3:

$array = array_chunk($array, 3);

This, of course, assumes that you will always receive only tuples containing name, valueand sizein that order.

+6
source

All Articles