0

我正在尝试将 2 组数组与相同的键进行比较product_sku

API 上的数组(JSON 响应)

$array1 = array (
    "products" => array(
        "category" => array(
            array(
                "product_sku" => "1",
                "product_type" => "Type",
                "customer_contact_name" => "Contact Name",
                "customer_telephone" => "0000 000 000",
                "customer_email" => "email@email.com",
                "customer_postcode" => "PostCode",
                "additional_info" => array(
                    "some information" => "some information"
                ),
                "full_price" => "50.00",
                "product_name" => "Product Name",
                "product_id" => "1",
                "customer_rating" => "0"
            ),
            array(
                "product_sku" => "2",
                "product_type" => "Type",
                "customer_contact_name" => "Contact Name",
                "customer_telephone" => "0000 000 000",
                "customer_email" => "email@email.com",
                "customer_postcode" => "PostCode",
                "additional_info" => array(
                    "some information" => "some information"
                ),
                "full_price" => "100.00",
                "product_name" => "Product Name",
                "product_id" => "2",
                "customer_rating" => "0"
            )
        )
    )
);

本地数据库上的数组(mysqli 查询)

$array2 = array (
    array(
        "product_sku" => "1",
        "product_type" => "Type",
        "contact_name" => "Contact Name",
        "phone" => "0000 000 000",
        "full_price" => "0.00",
        "product_name" => "Product Name",
        "product_id" => "1",
        "rating" => "0"
    ),
    array(
        "product_sku" => "3",
        "product_type" => "Type",
        "contact_name" => "Contact Name",
        "phone" => "0000 000 000",
        "full_price" => "80.00",
        "product_name" => "Product Name",
        "product_id" => "3",
        "rating" => "0"
    )
);

目标是

  1. 检查和仅当$array2匹配(存在于 中)时,获取from并替换中的值$array1product_sku$array1full_price$array1full_price$array2

  2. 如果product_idin$array1不存在 in $array2,则忽略这些产品。

  3. sort使用函数对产品价格进行排序(从低到高)

我尝试过的是,部分工作但无法使用sort功能对产品价格(从低到高)进行排序,它显示full_price某些产品的价值为0.00

$apiarray = $array1["products"]["category"];

foreach ($array2 as $arr2) {
    foreach ($apiarray as $arr1) {
        if ($arr1['product_sku']==$arr2['product_sku']) {
            echo $arr2['product_sku']; // Product from $array2
            echo number_format($arr1['full_price'],2); // Price from $array1
        } else {
            echo $arr2['product_sku']; // Product from $array2
            echo number_format($arr2['full_price'],2); // Price from $array2
        }
    }
)

我需要帮助来确定我们的目标或为我指明正确的方向我如何才能实现目标

4

2 回答 2

1

您可以执行以下操作:

//Make a temp associative array. This will make the product_sku as the key
//This is to make it easier to check if product_sku exist
$arrayTemp1 = array_column($array1['products']['category'], null, 'product_sku');

//Loop thru the array2.
//Check if the key exist on temp array. If it does, update the price
foreach( $array2 as &$value ) {
    if ( isset( $arrayTemp1[ $value['product_sku'] ] ) ) 
        $value['full_price'] = $arrayTemp1[ $value['product_sku'] ]['full_price'];
}

//Sort the $array2 using usort
usort( $array2, function($a, $b){
    return $a['full_price'] - $b['full_price'];
});

echo "<pre>";
print_r( $array2 );
echo "</pre>";

这将导致:

Array
(
    [0] => Array
        (
            [product_sku] => 1
            [product_type] => Type
            [contact_name] => Contact Name
            [phone] => 0000 000 000
            [full_price] => 50.00
            [product_name] => Product Name
            [product_id] => 1
            [rating] => 0
        )

    [1] => Array
        (
            [product_sku] => 3
            [product_type] => Type
            [contact_name] => Contact Name
            [phone] => 0000 000 000
            [full_price] => 80.00
            [product_name] => Product Name
            [product_id] => 3
            [rating] => 0
        )

)
于 2018-06-21T15:17:59.550 回答
1

您不是在编辑$array2,而只是在循环中打印。您的第二步不是必需的,它来自您的嵌套foreach.


首先,您可以获得 SKU 列表:

$sku_list = array_column($array1['products']['category'], 'product_sku');

然后更新价格:

// Loop through the 2nd array
for ($i = 0; $i < count($array2); $i += 1) {
    if (in_array($array2[$i]['product_sku'], $sku_list)) {

        // Get the index of the 1st array item which SKU is matching, and update
        $index = array_search($array2[$i]['product_sku'], $sku_list);
        $array2[$i]['full_price'] = $array1['products']['category'][$index]['full_price'];
    }
}

然后使用usort()PHP7 的spaceship 运算符进行排序(如果在较低的 PHP 版本中易于替换):

usort($array2, function($a, $b) { return $a['full_price'] <=> $b['full_price']; });
于 2018-06-21T15:45:53.483 回答