AdRock Posted November 19, 2014 Share Posted November 19, 2014 I have an array like this $rows = array( array( 'fruit.name' => 'Apple', 'fruit.colour' => 'Red', 'fruit.weight' => '0.1', 'vegetable.name' => 'Carrot', 'vegetable.colour' => 'Orange', 'vegetable.weight' => '0.05' ), array( 'fruit.name' => 'Banana', 'fruit.colour' => 'Yellow', 'fruit.weight' => '0.7', 'vegetable.name' => 'Potato', 'vegetable.colour' => 'Brown', 'vegetable.weight' => '0.6' ) ); And i want to be able to sort the array into 2 other arrays called 'fruits' and 'vegetables' based on the first part of the key name so up to the decimal point. With this array I should have 2 rows in each of the fruits and vegetable arrays. I have this code but it doesn't work and I can't see what I'm doing wrong. $fruits = array(); $vegetables = array(); foreach($rows as $row) { foreach($row as $key => $value) { if('fruit' == substr($key, 0, strpos($key, '.'))) { $fruits[$key] = $row; } else { $vegetables[$key] = $row; } } } echo "<pre>"; var_dump($fruits); echo "</pre>"; When i do a var_dump i get this array(3) { ["fruit.name"]=> array(6) { ["fruit.name"]=> string(6) "Banana" ["fruit.colour"]=> string(6) "Yellow" ["fruit.weight"]=> string(3) "0.7" ["vegetable.name"]=> string(6) "Potato" ["vegetable.colour"]=> string(5) "Brown" ["vegetable.weight"]=> string(3) "0.6" } ["fruit.colour"]=> array(6) { ["fruit.name"]=> string(6) "Banana" ["fruit.colour"]=> string(6) "Yellow" ["fruit.weight"]=> string(3) "0.7" ["vegetable.name"]=> string(6) "Potato" ["vegetable.colour"]=> string(5) "Brown" ["vegetable.weight"]=> string(3) "0.6" } ["fruit.weight"]=> array(6) { ["fruit.name"]=> string(6) "Banana" ["fruit.colour"]=> string(6) "Yellow" ["fruit.weight"]=> string(3) "0.7" ["vegetable.name"]=> string(6) "Potato" ["vegetable.colour"]=> string(5) "Brown" ["vegetable.weight"]=> string(3) "0.6" } } Any help please getting this to separate the array into 2 arrays each containing either fruits or vegetables. Quote Link to comment https://forums.phpfreaks.com/topic/292566-split-array-by-first-part-of-key-name-into-other-arrays/ Share on other sites More sharing options...
Barand Posted November 19, 2014 Share Posted November 19, 2014 (edited) try $data = array(); foreach ($rows as $arr) { foreach ($arr as $k => $v) { list($cat, $attr) = explode('.', $k); if ($attr == 'name') { $name = $v; $data[$cat][$name] = array(); } else { $data[$cat][$name][$attr] = $v; } } } RESULT $data = Array ( [fruit] => Array ( [Apple] => Array ( [colour] => Red [weight] => 0.1 ) [Banana] => Array ( [colour] => Yellow [weight] => 0.7 ) ) [vegetable] => Array ( [Carrot] => Array ( [colour] => Orange [weight] => 0.05 ) [Potato] => Array ( [colour] => Brown [weight] => 0.6 ) ) ) Edited November 19, 2014 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/292566-split-array-by-first-part-of-key-name-into-other-arrays/#findComment-1496991 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.