Jump to content

split array by first part of key name into other arrays


AdRock

Recommended Posts

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.

 

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
                )

        )

)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.