I want to get all the values from this array:
<root>
<Element1>
<items>
<Element0>
<sku>AAAA</sku>
</Element0>
<Element1>
<sku>BBBB</sku>
</Element1>
</items>
</Element1>
<items>
<Element0>
<sku>ABAB</sku>
</Element0>
</items>
<Element2>
</Element2>
...
...
</root>
The array is from Magento2 orders API so its dynamic, I achived all but I can’t get sku from nested arrays.
Here it’s what I tried to do:
$phpDataArray = json_decode($xml, true);
if (count($phpDataArray['items']) > 0) {
$sku = array();
$dataArray = array();
foreach ($phpDataArray['items'] as $index => $data) {
$status = $data['status'];
$customername = $data['customer_firstname'];
$customersurname = $data['customer_lastname'];
$email = $data['customer_email'];
$increment_id = $data['increment_id'];
$orderdata = $data['created_at'];
$total = $data['grand_total'];
foreach ($data['items'] as $item) {
$sku= $item['sku'];
}
$dataArray[] = [
"increment_id" => $increment_id,
"status" => $status,
"product_sku" => $sku,
"customername" => $customername,
"customersurname" => $customersurname,
"email" => $email,
"order_date" => $orderdata,
"total" => $total
];
}
}
But only last sku is shown.
Example: in element1 I have only BBBB but I want AAAA;BBBB
2
Answers
Before
$data['items']
iteration, initialize an array and push the elements there. This way you will not overwrite every previously set value of$sku
variable.My solution would be to collect all
sku
values in an array for each individual order, instead of overwriting them. This way, you can keep all thesku
values associated with each order like in my example below