2

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

1
  • what is the purpose of having multidimensional arrays? Commented Oct 17, 2014 at 13:30

4 Answers 4

2

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" } }
Sign up to request clarification or add additional context in comments.

Comments

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

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

1 Comment

This is not the sample input.
0

try with

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

2 Comments

You could also use $res = array_merge($qty[0], $price[0]); Depends on your use-case
This answer does not work with the asker's sample data. 3v4l.org/2lOFI
0

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]
];

2 Comments

This question is not asking for the transposition of two indexed arrays. If it was, it would be written as array_map(null, $a, $n);.
It would "prettier" to call array_merge_recursive() by its string name, but I still wouldn't. 3v4l.org/iKuNb

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.