比较键上的两个数组,如果键不匹配,则创建多个数组设置值

时间:2018-10-19 11:23:34

标签: php arrays

我有两个带有key => value数据的多个数组。对于这样的例子:

$k_a = array( array(
        "article" =>  "c",
        "price"   =>  2
    ), array(
        "article" =>  "f",
        "price"   =>  3
    )
); 

$k_b = array( array(
        "article" =>  "a",
        "price"   =>  2
    ), array(
        "article" =>  "b",
        "price"   =>  4
    ), array(
        "article" =>  "c",
        "price"   =>  1
    ), array(
        "article" =>  "g",
        "price"   =>  3
    )
);

一个数组中包含所有文章,但是价格设置为零。

$a_f = array( array(
        "article"=> "a", 
        "price"  => 0,
    ), array(
        "article"=> "b", 
        "price"  => 0,
    ), array(
        "article"=> "c", 
        "price"  => 0,
    ), array(
        "article"=> "d", 
        "price"  => 0,
    ), array(
        "article"=> "e", 
        "price"  => 0,
    ), array(
        "article"=> "f", 
        "price"  => 0,
    ), array(
        "article"=> "g", 
        "price"  => 0,
    ), array(
        "article"=> "h", 
        "price"  => 0,
    )
);

现在,我想检查$ a_f中$ k_a和$ k_b中的键商品的值是否存在,并使用该商品的键价创建一个新数组。其他商品的价格应为零。 在和我想比较两个新数组,并删除价格为零的键商品。

在和数组上应该是这样的:

$k_a_new =array( array(
        "article" => "a",
        "price"   => 0
    ), array(
        "article" => "b",
        "price"   => 0
    ), array(
        "article" => "c",
        "price"   => 2
    ), array(
        "article" => "f",
        "price"   => 3
    ), array(
        "article" => "g",
        "price"   => 0
    )          
);

$k_b_new = array( array(
        "article" => "a",
        "price"   => 2
    ), array(
        "article" => "b",
        "price"   => 4
    ), array(
        "article" => "c",
        "price"   => 1
    ), array(
        "article" => "f",
        "price"   => 0
    ), array(
        "article" => "g",
        "price"   => 3
    )            
);

结果数组应包含两个数组的所有项目,但如果它存在,则它自己的项目的一部分。 有解决此问题的功能或特定方法吗?

我尝试遍历数组并显示相同的商品价值,但不知道如何更改价格值或将商品价格添加为零价格?

echo "<br>";
foreach($k_b as $key_1=>$value_1){
    foreach($value_1 as $k_11=>$v_11){
        if($k_11 == "article"){
            foreach($a_f as $key_2=>$value_2){
                foreach($value_2 as $k_21=>$v_21){
                    if($k_21 === "article"){
                        if($v_11 === $v_21){
                            print_r($v_21);
                            echo "<br>";
                        }
                    }
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您必须这样做:-

<?php

$k_a = array( array(
        "article" =>  "c",
        "price"   =>  2
    ), array(
        "article" =>  "f",
        "price"   =>  3
    )
); 

$k_b = array( array(
        "article" =>  "a",
        "price"   =>  2
    ), array(
        "article" =>  "b",
        "price"   =>  4
    ), array(
        "article" =>  "c",
        "price"   =>  1
    ), array(
        "article" =>  "g",
        "price"   =>  3
    )
);

$final_article_array = array_values(array_unique(array_merge(array_column($k_a,'article'),array_column($k_b,'article'))));
sort($final_article_array);

$k_a_new = [];
$k_b_new = [];
foreach($final_article_array as $key=>$final_article_arr){
    $k = array_search($final_article_arr,array_column($k_a,'article'));
    if($k !==false){
        $k_a_new[$final_article_arr]['article'] = $final_article_arr;
        $k_a_new[$final_article_arr]['price'] = $k_a[$k]['price'];
    }else{
        $k_a_new[$final_article_arr]['article'] = $final_article_arr;
        $k_a_new[$final_article_arr]['price'] = 0;
    }
    $j = array_search($final_article_arr,array_column($k_b,'article'));
    if($j !==false){
        $k_b_new[$final_article_arr]['article'] = $final_article_arr;
        $k_b_new[$final_article_arr]['price'] = $k_b[$j]['price'];
    }else{
        $k_b_new[$final_article_arr]['article'] = $final_article_arr;
        $k_b_new[$final_article_arr]['price'] = 0;
    }
}
$k_a_new = array_values($k_a_new);
$k_b_new = array_values($k_b_new);

print_r($k_a_new);
print_r($k_b_new);

输出:-{https://3v4l.org/DTiJk

注意:-说明

1)尝试从$k_a$k_b获取所有文章值,将它们合并以将所有文章放入一个数组中,从该数组中删除重复项,然后对该数组重新索引。现在,按值对本文数组进行排序。

2)迭代并检查$ k_a和$ k_b数组中是否存在该文章(使用array_search

3)现在,如果找到了文章,您将通过array_search()获得子数组的索引。

4)基于此键,获取子数组的价格值,并将其添加到带有相应商品的最终数组中。

5)如果没有索引,则将相应商品的价格0放入最终数组中。

Get all Php functions here