将2组数组与相似的键进行比较

时间:2018-06-21 15:05:00

标签: php arrays sorting mysqli

我正在尝试将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$array1进行比较,只有product_sku匹配(存在于$array1中)才能从full_price获取$array1并替换full_price

  2. 中的$array2
  3. 如果product_id中的$array1中的$array2不存在,则忽略这些产品。

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

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

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

我需要帮助来解决我们的问题或为我指明正确的方向

2 个答案:

答案 0 :(得分: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
        )

)

答案 1 :(得分: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 operator进行排序(如果在较低的PHP版本中则很容易替换):

usort($array2, function($a, $b) { return $a['full_price'] <=> $b['full_price']; });