phpshopify

Sorting data from an array in PHP


I'm deploying a shipping service to Shopify using a webhook to get this data converted in JSON. I'm trying to send only the fields name, quantity, sku and id in a table.

I'm using foreach function to make it but getting no results.

[fulfillments] => Array
   (
       [0] => stdClass Object
           (
               [line_items] => Array
                   (
                       [0] => stdClass Object
                           (
                               [fulfillment_service] => manual
                               [fulfillment_status] => fulfilled
                               [gift_card] => 
                               [grams] => 2000
                               [id] => 470651995
                               [price] => 200.00
                               [product_id] => 304163215
                               [quantity] => 1
                               [requires_shipping] => 1
                               [sku] => 123456789
                               [taxable] => 
                               [title] => Product 1
                               [variant_id] => 709836495
                               [variant_title] => 
                               [vendor] => kkk
                               [name] => Product 1
                           )
                       [1] => stdClass Object
                           (
                               [fulfillment_service] => manual
                               [fulfillment_status] => fulfilled
                               [gift_card] => 
                               [grams] => 2000
                               [id] => 470651995
                               [price] => 200.00
                               [product_id] => 304163215
                               [quantity] => 3
                               [requires_shipping] => 1
                               [sku] => 123456789
                               [taxable] => 
                               [title] => Product 2
                               [variant_id] => 709836495
                               [variant_title] => 
                               [vendor] => kkk
                               [name] => Product 2
                           )
                   )
           )
   )

Solution

  • $your_array = json_decode(json_encode($your_array), true); 
        //it will casts from StdClass to an array
    $your_array = $your_array['fulfillments'];
    $result_array = array();
    
    $yaCnt = count($your_array);
    for($i = 0; $i < $yaCnt; ++$i)
    {
        $items = $yaCnt[$i]['line_items'];
        $iCnt = count($items);
        for($j = 0; $j < $iCnt; ++$j)
        {
            $result_array[] = array(
                'name'     => $items[$j]['name'],
                'quantity' => $items[$j]['quantity'],
                'sku'      => $items[$j]['sku'],
                'id'       => $items[$j]['id']
            );
        }
    }
    
    print_r($result_array);