代码之家  ›  专栏  ›  技术社区  ›  Shehary

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

  •  0
  • Shehary  · 技术社区  · 6 年前

    我正在尝试将两组数组与同一个键进行比较 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 只有在 产品\库存单位 匹配(存在于 $array1美元 )获取 full_price $array1美元 并更换 全价 价值 $array2美元

    2. 如果 product_id 在里面 $array1美元 不存在于 $array2美元 忽略这些产品。

    3. 分类产品价格(从低到高)使用 sort 功能

    我所尝试的是,部分工作,但无法使用分类产品价格(从低到高) 分类 功能,显示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
            }
        }
    )
    

    我需要帮助来解决问题,或者给我指明正确的方向,我如何才能实现目标

    2 回复  |  直到 6 年前
        1
  •  1
  •   Eddie    6 年前

    你可以这样做:

    //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
            )
    
    )
    
        2
  •  1
  •   AymDev    6 年前

    $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() spaceship operator

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