用多个索引php加入一个数组

时间:2017-10-04 14:38:53

标签: php arrays

如何将具有多个索引的2个数组连接到1个数组中? 我有像这样的数组

Array
(
    [0] => Array
        (
            [wd[wd5][amount]] => 1.00
            [wd[wd5][address]] => 1BitcoinAddress
            [wd[wd5][currency]] => BTC

        )

    [1] => Array
        (
            [wd[wd7][amount]] => 1.00
            [wd[wd7][address]] => 1BitcoinAddress
            [wd[wd7][currency]] => BTC

        )

)

我想将该数组转换/更改/合并为与此类似的内容

array(
        'wd[wd5][amount]' => 1.00,
        'wd[wd5][address]' => '1BitcoinAddress',
        'wd[wd5][currency]' => 'BTC',
        'wd[wd7][amount]' => 0.0001,
        'wd[wd7][address]' => '1BitcoinAddress',
        'wd[wd7][currency]' => 'BTC'

);

我该怎么做?

6 个答案:

答案 0 :(得分:5)

使用call_user_func_arrayarray_merge

<?php

$array = [

    [
        "[wd[wd5][amount]]" => 1.00,
        "[wd[wd5][address]]" => "1BitcoinAddress",
        "[wd[wd5][currency]]" => "BTC"
    ],
    [
        "[wd[wd7][amount]]" => 1.00,
        "[wd[wd7][address]]" => "1BitcoinAddress",
        "[wd[wd7][currency]]" => "BTC"
    ]
];


$result = call_user_func_array('array_merge', $array);

echo "<pre>";
print_r($result);

答案 1 :(得分:1)

使用foreach循环数组并创建一个新数组。

$new_array = array();
foreach($array as $value){
    foreach($value as $k=>$v){
        $new_array[$k] = $v;
    }
}
print_r($new_array);

输出:

Array
(
    [wd[wd5][amount]] => 1.00
    [wd[wd5][address]] => 1BitcoinAddress
    [wd[wd5][currency]] => BTC
    [wd[wd7][amount]] => 1.00
    [wd[wd7][address]] => 1BitcoinAddress
    [wd[wd7][currency]] => BTC
)

答案 2 :(得分:1)

你可以这样做:

$result = array();
foreach($array as $item) {
    $result = array_merge($result, $item);
}

此处$result是一个新的空白数组,$array是要合并的数组。

答案 3 :(得分:0)

您可以使用RecursiveIteratorIteratorRecursiveArrayIterator来完成此操作。

虽然我很喜欢@BilalAkbar,但为了简单起见,现在更多的回答了。

<?php
$array = [
    [
        'wd[wd5][amount]' => 1.00,
        'wd[wd5][address]' => '1BitcoinAddress',
        'wd[wd5][currency]' => 'BTC',
    ],
        [
        'wd[wd7][amount]' => 1.00,
        'wd[wd7][address]' => '1BitcoinAddress',
        'wd[wd7][currency]' => 'BTC'
    ],

];

$result = [];
foreach (new RecursiveIteratorIterator(
         new RecursiveArrayIterator($array)
) as $key => $value) {
  $result[$key] = $value;
}

print_r($result);
/*
Array
(
    [wd[wd5][amount]] => 1
    [wd[wd5][address]] => 1BitcoinAddress
    [wd[wd5][currency]] => BTC
    [wd[wd7][amount]] => 1
    [wd[wd7][address]] => 1BitcoinAddress
    [wd[wd7][currency]] => BTC
)
*/

https://3v4l.org/0fCKj

答案 4 :(得分:0)

使用array_merge合并组合数组。

http://php.net/manual/en/function.array-merge.php

<?php

$arrayWithSubarrays = array(
    array(
        "[wd[wd5][amount]]" => 1.00,
        "[wd[wd5][address]]" => "1BitcoinAddress",
        "[wd[wd5][currency]]" => "BTC"
    ),
    array(
        "[wd[wd7][amount]]" => 1.00,
        "[wd[wd7][address]]" => "1BitcoinAddress",
        "[wd[wd7][currency]]" => "BTC"
    )
);

// merge each array explicitly:
$mergedArray1 = array_merge($arrayWithSubarrays[0],$arrayWithSubarrays[1]);

// or merge as many as you have in the array:
$mergedArray2 = array();
foreach($arrayWithSubarrays as $array) {
    $mergedArray2 = array_merge($mergedArray2, $array);
}

// (mergedArray1 contains the same data as mergedArray2)

答案 5 :(得分:0)

这里是答案,但是您有2个解决方案,而答案下面是一个不同的键。 否则,每次3个键相同时,只保存数组的最后一个值。

希望您能理解

<?php

$a = array
(
     array
        (
            'wd[wd[amount]]' => '1.00',
            '[wd[wd5][address]]' => '1BitcoinAddress',
            '[wd[wd5][currency]]' => 'BTC'


        ),

    array
        (
            'wd[wd[amount1]]' => '1.00',
            '[wd[wd5][address1]]' => '1BitcoinAddress',
            '[wd[wd5][currency1]]' => 'BTC'
        )

);
$total = count($a);
$p = array();
$q = array();
$pq = array();
for($i=0;$i<$total;$i++){
    $tarray = $a[$i];
    foreach($tarray as $k=>$v){
        array_push($p,$k);
        array_push($q,$v);

    }       
}
$pq = array_combine($p,$q);
print_r($pq);

?>