Saul_dg
Saul_dg

Reputation: 37

sorting data from an array on php

i'm deploying a shipping service to shopify using a webhook to get this data converted in json, i trying to send only the fields name, quantity, sku and id in a table. i'm using foreach function to make it but no results.

Grettings!

[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
                           )


                   )

           )

   )

Upvotes: 0

Views: 118

Answers (3)

fiction
fiction

Reputation: 586

$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);

Upvotes: 1

spinsch
spinsch

Reputation: 1415

<?php

$yourData = array();
$shippingData =array();

foreach ($yourData as $fulfillments) {
    foreach ($fulfillments->line_items as $line_items) {
        $shippingData[] = array(
            'name' => $line_items->name,
            'quantity' => $line_items->quantity,
            'sku' => $line_items->sku,
            'id' => $line_items->id
        );
    }
}

echo json_encode($shippingData);

Upvotes: 0

Erlesand
Erlesand

Reputation: 1535

Try the following foreach(), I have assumed you store all the initial data in the variable $fulfillments

$items = array(); 

foreach($fulfillments[0]->line_items as $item)
{
    $items[] = array(
        "name"      => $item->name,
        "quantity"  => $item->quantity, 
        "sku"       => $item->sku, 
        "id"        => $item->id
    ); 
}

echo json_encode($items); 

Upvotes: 0

Related Questions