Q:

Separating array rows into other arrays based on key name

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.

A:

This seems to work:
$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'
        )
    );

    $fruits = $vegs = array();
    foreach ($rows as $arrays) {

        $fruit = array();
        $veg = array();
        foreach ($arrays as $key => $val) {

            $index = substr($key, strpos($key, ".") + 1);   

            if('fruit' == substr($key, 0, strpos($key, '.'))){

                $fruit[$index] = $val;
            } else {
                $veg[$index] = $val;
            }
        }

        $fruits[] = $fruit;
        $vegs[] = $veg;
    }
     var_dump($fruits, $vegs);

(Please overlook the fact I've called one of the vars $vegs)
Hope this helps!

