skip to Main Content

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


  1. 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.

    $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'];
        $sku = [];// <- here
        foreach ($data['items'] as $item) {
          $sku[] = $item['sku'];// <- and here
        }
        $dataArray[] = [
          "increment_id" => $increment_id,
          "status" => $status,
          "product_sku" => $sku,
          "customername" => $customername,
          "customersurname" => $customersurname,
          "email" => $email,
          "order_date" => $orderdata,
          "total" => $total
        ];
      }
    }
    
    Login or Signup to reply.
  2. 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 the sku values associated with each order like in my example below

    $phpDataArray = json_decode($xml, true);
    
    if (count($phpDataArray['items']) > 0) {
        $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'];
    
            $skuArray = array(); //Hold SKUs for this order
            foreach ($data['items'] as $item) {
                $skuArray[] = $item['sku']; //Accumulate SKUs in an array
            }
            
            $skuString = implode(';', $skuArray);
    
            $dataArray[] = [
                "increment_id" => $increment_id,
                "status" => $status,
                "product_sku" => $skuString, 
                "customername" => $customername,
                "customersurname" => $customersurname,
                "email" => $email,
                "order_date" => $orderdata,
                "total" => $total
            ];
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search