【问题标题】:merge two array's key value of same index合并相同索引的两个数组键值
【发布时间】:2018-09-02 03:20:34
【问题描述】:

我有 2 个数组,我必须在同一索引内合并数组。

Array
(
    [0] => Array
        (
            [vendor_name] => MIRAGE PET PRODUCTS
            [count_es] => 86
            [outofstalk] => 19
            [listing] => 64
            [pricing] => 2
            [discontinued] => 1
            [others] => 0
        )

    [1] => Array
        (
            [vendor_name] => THE HOME DEPOT
            [count_es] => 1
            [outofstalk] => 0
            [listing] => 1
            [pricing] => 0
            [discontinued] => 0
            [others] => 0
        )

)

这是第一个数组和

Array
   (   [0] => Array
        (
            [incorrect_esc_count] => 1
        )

    [1] => Array
        (
            [incorrect_esc_count] => 0
        ) 

) 

这是第二个数组。

我想要那个结果

Array
(
    [0] => Array
        (
            [vendor_name] => MIRAGE PET PRODUCTS
            [count_es] => 86
            [outofstalk] => 19
            [listing] => 64
            [pricing] => 2
            [discontinued] => 1
            [others] => 0
            [incorrect_esc_count] => 1
        )

    [1] => Array
        (
            [vendor_name] => THE HOME DEPOT
            [count_es] => 1
            [outofstalk] => 0
            [listing] => 1
            [pricing] => 0
            [discontinued] => 0
            [others] => 0
            [incorrect_esc_count] => 0
        )
)

我怎样才能做到这一点? `第一个数组从一个变量中获取,第二个数组从另一个变量中获取。我希望这个应该放在一个数组中,因为对于视图我使用 angular.js 并使用表

【问题讨论】:

  • 请格式化您的问题。
  • 你有相关代码要发吗?另外,您使用的是什么语言? php?最后,我觉得应该是“缺货”。
  • @ecg8 ,我正在使用 PHP 和 2 个不同的变量,因为它的 2 个查询并希望显示在同一个表中

标签: php merge associative-array


【解决方案1】:

您正试图将两个多维关联数组合并为一个。这段代码应该适合你,有一些 cmets:

<?php
// declare the first array, I did not add all of the key-value pairs
$marr1 = array
  (
  array("vendor_name"=>"MIRAGE PET PRODUCTS","count_es"=>86,"outofstalk"=>19),
  array("vendor_name"=>"THE HOME DEPOT","count_es"=>1,"outofstalk"=>0)
  );
// declare second array
$marr2 = array
  (
  array("incorrect_esc_count"=>1),
  array("incorrect_esc_count"=>0)
  );
// declare third empty array to hold the result
$marr3 = array();
// loop through both arrays
// this will not work if your arrays have different lengths
for ($i = 0; $i < count($marr1); $i++) {
    // merge the ith elements of both array and then push that array into the third array
    array_push($marr3, array_merge($marr1[$i],$marr2[$i]));
}
// print out result
print_r($marr3);
?>

【讨论】:

    猜你喜欢
    • 2016-03-08
    • 1970-01-01
    • 2017-11-01
    • 2014-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-02
    • 1970-01-01
    相关资源
    最近更新 更多