0

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 ?

5
  • Remove the assignment of the other indicec, e.g. "FoodOrder_ID" => $allDataInSheet[$i]["B"],? Commented Sep 11, 2020 at 5:59
  • @DarkBee if i removed it , the below foreach will stop working. as i need the"FoodOrder_ID" to become the "parent" Commented Sep 11, 2020 at 6:04
  • Just move that logic in your first loop? That 2nd loop is redundant Commented Sep 11, 2020 at 6:05
  • Ahh got it , thanks , look like i still need to improve my array skill, thanks once again Commented Sep 11, 2020 at 6:27
  • No worries, practice makes perfect :) Commented Sep 11, 2020 at 6:32

1 Answer 1

3

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?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.