如何根据多维数组中的另一个值查找并替换数组值?

huangapple go评论73阅读模式
英文:

How can I find an replace an array value based on another value in the same multidimensional array

问题

所以我有一个多维数组,像下面这样:

    [0]=>
      array(4) {
        ["id"] => 110
        ["list"] => {}
        ["MainProduct"] => 2         <- 步骤2:当productName是Oranges时,将2替换为5
        ["productName"] => Oranges   <- 步骤1:找到包含"Oranges"的位置
      }
    [1]=>
      array(4) {
        ["id"] => 111
        ["list"] => {}
        ["MainProduct"] => 110
        ["productName"] => Apples
      }

最终,我需要实现的是找到`key`为`productName`且`value`为`Oranges`的位置,然后基于此替换同一子数组中`key`为`MainProduct`的`value`。

我想我应该先找到包含`Oranges`的数组的索引,然后替换同一子数组中`key`为`mainProduct`的`value`,但似乎无法正确执行。
`key`名称始终相同,而`value`名称不同。

另外,数组是动态的,所以包含`Oranges`的数组的索引不会总是相同的。

这是期望的结果:

    [0]=>
      array(4) {
        ["id"] => 110
        ["list"] => {}
        ["MainProduct"] => 5         <- 当productName是"Oranges"时,将其更改为5
        ["productName"] => Oranges   
      }
    [1]=>
      array(4) {
        ["id"] => 111
        ["list"] => {}
        ["MainProduct"] => 110
        ["productName"] => Apples
      }
英文:

So I have a multidimensional array like the one below:

[0]=&gt;
  array(4) {
    [&quot;id&quot;] =&gt; 110
    [&quot;list&quot;] =&gt; {}
    [&quot;MainProduct&quot;] =&gt; 2         &lt;- Step2: Replace the 2 with a 5 where productName is Oranges
    [&quot;productName&quot;] =&gt; Oranges   &lt;- Step1: Find where we have &quot;Oranges&quot;
  }
[1]=&gt;
  array(4) {
    [&quot;id&quot;] =&gt; 111
    [&quot;list&quot;] =&gt; {}
    [&quot;MainProduct&quot;] =&gt; 110
    [&quot;productName&quot;] =&gt; Apples
  }

Ultimately, what I need to achieve is to find were the value for key productName equals Oranges and based on that to replace the value for key MainProduct in the same sub array.

I thought I should first find the index for the array containing Oranges and then replace the value for key mainProduct but can't seem to do it properly.
They key names are always the same, while the value names are different.

Also, the array is dynamic so the array with Oranges won't always have the same index.

This is the desired result:

[0]=&gt;
  array(4) {
    [&quot;id&quot;] =&gt; 110
    [&quot;list&quot;] =&gt; {}
    [&quot;MainProduct&quot;] =&gt; 5         &lt;- This is changed to 5 where productName is &quot;Oranges&quot;
    [&quot;productName&quot;] =&gt; Oranges   
  }
[1]=&gt;
  array(4) {
    [&quot;id&quot;] =&gt; 111
    [&quot;list&quot;] =&gt; {}
    [&quot;MainProduct&quot;] =&gt; 110
    [&quot;productName&quot;] =&gt; Apples
  }

答案1

得分: 1

一个稍微变化的策略,你提出的:

    <?php
    $data = [
      [
        "id" => 110,
        "MainProduct" => 2,
        "productName" => "橙子",
      ],
      [
        "id" => 111,
        "MainProduct" => 110,
        "productName" => "苹果",
      ]
    ];
    
    $output = [];
    array_walk($data, function(&$entry) use($data)  {
      $mainProduct = array_search($entry['MainProduct'], array_column($data, 'id'));
      $entry['productName'] = $data[$mainProduct]['productName'];
    });
    
    print_r($data);
英文:

A slight variant of the strategy you proposed:

&lt;?php
$data = [
  [
    &quot;id&quot; =&gt; 110,
    &quot;MainProduct&quot; =&gt; 2,
    &quot;productName&quot; =&gt; &quot;Oranges&quot;,
  ],
  [
    &quot;id&quot; =&gt; 111,
    &quot;MainProduct&quot; =&gt; 110,
    &quot;productName&quot; =&gt; &quot;Apples&quot;,
  ]
];

$output = [];
array_walk($data, function(&amp;$entry) use($data)  {
  $mainProduct = array_search($entry[&#39;MainProduct&#39;], $data);
  $entry[&#39;productName&#39;] = $data[$mainProduct][&#39;productName&#39;];
});

print_r($data);

The output is:

Array
(
    [0] =&gt; Array
        (
            [id] =&gt; 110
            [MainProduct] =&gt; 2
            [productName] =&gt; Oranges
        )
    [1] =&gt; Array
        (
            [id] =&gt; 111
            [MainProduct] =&gt; 110
            [productName] =&gt; Oranges
        )
)

答案2

得分: 0

如果你获取了键并与productName的值结合,然后你可以搜索并获取到这个键。然后使用这个键来更新MainProduct

$array[array_search('Oranges', array_combine(array_keys($array), array_column($array, 'productName')))]['MainProduct'] = 5;

如果productName是唯一的,那么我会直接修改数组,以便以此为索引:

$array = array_column($array, null, 'productName');
$array['Orange']['MainProduct'] = 5;
英文:

If you get the keys and combine with the productName values, then you can search it and get the key. Then use the key to update MainProduct:

$array[array_search(&#39;Oranges&#39;, array_combine(array_keys($array), array_column($array, &#39;productName&#39;)))][&#39;MainProduct&#39;] = 5;

If productName is unique, then I would just modify the array to index on that:

$array = array_column($array, null, &#39;productName&#39;);
$array[&#39;Orange&#39;][&#39;MainProduct&#39;] = 5;

huangapple
  • 本文由 发表于 2023年3月4日 01:57:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630400.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定