如何用Php

时间:2017-11-02 08:37:03

标签: php arrays json

我使用Php作为我的服务器端脚本语言。在我的项目中,我使用Json字符串解码成数组。 我的问题是如何根据数组值覆盖现有的数组索引。

我现有的数组如下:

$array1 =[
           {
             "Name":"apple",
             "color":"red",
             "property":[
                          {
                            "p1":"value1",
                            "p2":"value2"
                          }
                       ]
           },
           {
             "Name":"Grape",
             "color":"violet",
             "property":[
                          {
                            "p1":"value1",
                            "p2":"value2"
                          }
                     ]
           }
         ];

,更新后的数组内容如下:

$upadatearray = [
                  {
                     "Name":"apple",
                     "color":"green",
                     "property":[
                                  {
                                    "p1":"newvalue",
                                    "p2":"newvalue2"
                                  }
                               ]
                  }
                ];

我想用新的$ upadatearray更新现有的$ array1,并使用“Name”。如果它相同则替换。

我想看起来像:

$finalarray =[
                {
                  "Name":"apple",
                  "color":"green",
                  "property":[
                               {
                                 "p1":"newvalue",
                                 "p2":"newvalue2"
                               }
                             ]
                },
                {
                  "Name":"Grape",
                  "color":"violet",
                  "property":
                             [
                               {
                                 "p1":"value1",
                                 "p2":"value2"
                               }
                             ]
                }
           ];

我试过了:

for($j=0;$j<count($array1);$j++)
{
     if($array1[$j]['Name'] == $upadatearray[0]['Name'])
     $finalarray = array_replace($array1[$j],$upadatearray[0]);
}

但它无法正常工作。是否有任何可能的解决方案?

2 个答案:

答案 0 :(得分:1)

嗨,我认为这段代码可以帮到你。

//what i did is i created a final array variable which gets the value of  old array.
$finalArray = $array1;

//then i perform a foreach loop for old array
foreach ($array1 as $key => $oldarray) {
    //inside the updated array
    foreach ($upadatearray as $key => $newarray) {
        //if old array name and new array name is same replace content on the final array
        if ($oldarray['Name'] == $newarray['Name']) {
            $finalArray['Name'] = $newarray['Name'];
        }
    }
}

答案 1 :(得分:1)

让你有这两个数组:

$array1 ='[{"Name":"apple","color":"red","property":[{"p1":"value1","p2":"value2"}]},{"Name":"Grape","color":"violet","property":[{"p1":"value1","p2":"value2"}]}]';

$upadatearray = '[{"Name":"apple", "color":"green", "property":[{"p1":"newvalue","p2":"newvalue2"}]}]';

$array1 = json_decode($array1, true);
$upadatearray = json_decode($upadatearray, true);

您可以使用array_replace功能。但是要根据Name列替换项目,首先应该将此列作为数组的键

function make_column_key($arr, $col_name) {
  $keys = array_column($arr, $col_name);
  $result = array_combine($keys, $arr);
  return $result;
}

$array1 = make_column_key($array1, 'Name');
$upadatearray = make_column_key($upadatearray, 'Name');

现在只需使用array_replace

$finalarray = array_replace($array1, $upadatearray);

如果您不需要Name作为最终数组的键,则只能获取值:

$finalarray = array_values($finalarray);