Reputation: 1027
How to join 2 arrays with multiple index into 1 array ? I have array like this
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
)
)
I want to convert/change/merge that array into something exactly like this
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'
);
how do I do that ?
Upvotes: 0
Views: 1032
Reputation: 133
Here answer, but you have 2 solution one different key then below answer. otherwise only save array last values beacause every time 3 keys same.
hope you understand.
<?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);
?>
Upvotes: 0
Reputation: 1
Use array_merge to merge combine the arrays.
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)
Upvotes: 0
Reputation: 46602
You can do it using RecursiveIteratorIterator and RecursiveArrayIterator.
Though I'm liking @BilalAkbar's answer abit more now for the simplicity.
<?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
)
*/
Upvotes: 0
Reputation: 6565
you can do this like:
$result = array();
foreach($array as $item) {
$result = array_merge($result, $item);
}
here $result
is a new blank array, and $array
is the array to merge.
Upvotes: 1
Reputation: 4930
Using call_user_func_array and array_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);
Upvotes: 5
Reputation: 3879
Loop the array using foreach and create an new array.
$new_array = array();
foreach($array as $value){
foreach($value as $k=>$v){
$new_array[$k] = $v;
}
}
print_r($new_array);
Output:
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
)
Upvotes: 1