Billy Adelphia
Billy Adelphia

Reputation: 1027

Join one array with multiple index php

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

Answers (6)

Sarvesh Patel
Sarvesh Patel

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

Tom Troyer
Tom Troyer

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

Lawrence Cherone
Lawrence Cherone

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
)
*/

https://3v4l.org/0fCKj

Upvotes: 0

Himanshu Upadhyay
Himanshu Upadhyay

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

Bilal Akbar
Bilal Akbar

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

Ravinder Reddy
Ravinder Reddy

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

Related Questions