在php上从数组中排序数据


sorting data from an array on php

我正在部署一个运输服务来shopify使用webhook来获得json格式的数据转换,我试图在表中只发送字段名称,数量,sku和id。我使用foreach函数使它,但没有结果。

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
                           )

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

尝试以下foreach(),我假设您将所有初始数据存储在变量$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); 
<?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);