【问题标题】:Merge 2 Multidimensional Arrays合并 2 个多维数组
【发布时间】:2014-06-23 23:13:18
【问题描述】:

我正在为 Magento 肥皂调用格式化一个数组。我有两个 mysql 查询,结果是 ARRAY1 和 ARRAY2。如何合并/组合 ARRAY1 和 ARRAY2 以使结果类似于 ARRAY3?

感谢您的帮助。

阵列1

<pre>
array(2) {
  [0]=>
  array(3) {
    [0]=>
    string(6) "store1"
    [1]=>
    string(8) "product1"
  }
  [1]=>
  array(3) {
    [0]=>
    string(6) "store1"
    [1]=>
    string(8) "product2"
  }
  [2]=>
  array(2) {
    [0]=>
    string(6) "store2"
    [1]=>
    string(8) "product2"
  }
}
</pre>

数组2

<pre>
array(3) {
  [0]=>
  array(1) {
    ["status"]=>
    string(3) "Yes"
  }
  [1]=>
  array(1) {
    ["status"]=>
    string(2) "No"
  }
  [2]=>
  array(1) {
    ["status"]=>
    string(3) "Yes"
  }
}
</pre>

阵列3

<pre>
array (size=3)
  0 => 
    array (size=3)
      0 => string 'store1' (length=6)
      1 => string 'product1' (length=8)
      2 => 
          array (size=1)
           'status' => string 'Yes' (length=3)
  1 => 
    array (size=3)
      0 => string 'store1' (length=6)
      1 => string 'product2' (length=8)
      2 => 
          array (size=1)
           'status' => string 'No' (length=2)
  2 => 
    array (size=3)
      0 => string 'store2' (length=6)
      1 => string 'product2' (length=8)
      2 => 
          array (size=1)
           'status' => string 'No' (length=2)
</pre>

【问题讨论】:

  • 可以array_merge_recursive 帮忙吗?
  • 数组 1 或 2 中没有 store3product3 - 但它出现在数组 3 中。请修改您的数据或澄清您的问题。
  • 如果这两个数组之间存在关系,为什么不进行数据库表连接?它们来自不同的来源吗?
  • Yannick - 查询来自同一个表。我想做一个连接,但我被挂断了,因为我在其中一个查询上使用“mysqli_fetch_array”,在另一个查询上使用“mysqli_fetch_assoc”。
  • Mark M - 修正了数组中的拼写错误。谢谢。

标签: php mysql arrays magento soap


【解决方案1】:

假设两个数组完美排列,这可能会有所帮助:

<?php
$i = 0; 
foreach ($array1 as $item) {
array_push($item, $array2[$i]);
$i++;
} 
?>

【讨论】:

  • 我想这就是我要找的。现在做一些测试。
猜你喜欢
  • 2013-03-08
  • 2020-09-15
  • 1970-01-01
  • 1970-01-01
  • 2021-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多