greenboxgoolu
greenboxgoolu

Reputation: 139

PHP array with subarray

Extracted data from excel

        $allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
        $arrayCount = count($allDataInSheet);
       for($i=1;$i<=$arrayCount;$i++)
        {                   
            $arraydata[]= array(
                                "FoodOrder_ID" => $allDataInSheet[$i]["B"],
                                "FoodID" => $allDataInSheet[$i]["C"],
                                "FoodName" => $allDataInSheet[$i]["D"],
                                "FoodQuantity" => $allDataInSheet[$i]["E"]);

        }




        $result = array();
        foreach ($arraydata as $element) {
             $result[$element['FoodOrder_ID']][] = $element;
        }




        echo "<pre>";
        print_r($result);
        echo "</pre>";
        exit;

Above is my code that able to return data that in excel

Array
(
    [FO012] => Array
        (
            [0] => Array
                (
                    [FoodOrder_ID] => FO012
                    [FoodID] => 22
                    [FoodName] => Burger
                    [FoodQuantity] => 1
                )

            [1] => Array
                (
                    [FoodOrder_ID] => FO012
                    [FoodID] => 23
                    [FoodName] => Coke
                    [FoodQuantity] => 1
                )

            [2] => Array
                (
                    [FoodOrder_ID] => FO012
                    [FoodID] => 53
                    [FoodName] => Sprite
                    [FoodQuantity] => 1
                )

        )

    [FO013] => Array
        (
            [0] => Array
                (
                    [FoodOrder_ID] => FO013
                    [FoodID] => 12
                    [FoodName] => Salad
                    [FoodQuantity] => 1
                )

        )

    [FO014] => Array
        (
            [0] => Array
                (
                    [FoodOrder_ID] => FO014
                    [FoodID] => 64
                    [FoodName] => Sandwich
                    [FoodQuantity] => 1
                )

        )

)

Above is my return result. What i wanted to do : In subarray , only show "FoodID" and "FoodQuantity" ?

Currently in subarray also showing foodorder_id , but what i wanted is that in subarray only show 2 of the data.

Any idea how to exclude it ?

Upvotes: 0

Views: 79

Answers (1)

verjas
verjas

Reputation: 1793

You could try this something like this. (Like @Darkbee said, you don't need two for loops):

$allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
$arrayCount = count($allDataInSheet);

$result = array();
for($i=1;$i<=$arrayCount;$i++)
{
    $FoodOrder_ID = $allDataInSheet[$i]["B"];
    if (!isset($result[$FoodOrder_ID]))
        $result[$FoodOrder_ID] = array();
        
    $result[$FoodOrder_ID][] = array(
        "FoodID" => $allDataInSheet[$i]["C"],
        "FoodQuantity" => $allDataInSheet[$i]["E"]
    );
}

echo "<pre>";
print_r($result);
echo "</pre>";
exit;

Can you check if this solves it?

Upvotes: 3

Related Questions