Italiano
Italiano

Reputation: 55

array merge php with same index

I have this situation:

$qty = array(1) {[0]=> array(1) { ["qty"]=> string(5) "35254" }
$price = array(1) {[0]=> array(1) { ["price"]=> string(5) "1000" }

How can I get this?

$res = array(1) {[0]=> array(1) { ["qty"]=> string(5) "35254" ["price"]=> string(5) "1000"}

Thanks for the answers

Upvotes: 2

Views: 7945

Answers (4)

Ioan
Ioan

Reputation: 11

Not as pretty but with the same result.

$result = array_map(function ($e1,$e2) {
    return array_merge_recursive($e1, $e2);
}, $qty,$price);

$result =
    array(1) {
      [0]=>
      array(2) {
        ["qty"]=>
        string(5) "35254"
        ["price"]=>
        string(4) "1000"
      }
    }

and for indexed arrays

$a = ['a', 'b', 'c'];
$n = [1, 2, 3];

$result = array_map(function ($e1,$e2) {
    return [$e1, $e2];
}, $a,$n);

$result = [
 0 => ['a', 1],
 1 => ['b', 2],
 2 => ['c', 3]
];

Upvotes: 0

doydoy44
doydoy44

Reputation: 5782

May be it's that you want:

$res = array();
foreach($qty as $k => $v){
    $res[$k] = array_merge($qty[$k],$price[$k]);
}

The result :

array(1) {[0] => array(2) { 'qty' => string(5) "35254" 'price' => string(4) "1000" } }

Upvotes: 2

Ram Sharma
Ram Sharma

Reputation: 8819

try with

$res = array_merge_recursive($qty, $price);
print_r($res);

Upvotes: 0

Chris
Chris

Reputation: 3445

$qty = array("qty"=>"35254" );
$price = array ( "price"=> "1000" );

$combine = array_merge($qty,$price);
var_dump($combine);

Upvotes: 1

Related Questions