-1

Actually i have two arrays as given below

$array1=array('0'=>'abc','1'=>'xyz');
$array2=array('0'=>'pqr','1'=>'mno');

I want to two arrays first created with key[0] and second with key[1]. means should look like as follow

$a1=array('0'=>'abc','1'=>'pqr');
$a2=array('0'=>'xyz','1'=>'mno');

The result must be

$a1 = array (
 '0' => $array1[0],
 '1' => $array2[0]
);

$a2 = array (
 '0' => $array1[1],
 '1' => $array2[1]
);
10
  • What have you done so far to achieve your goal? Commented Apr 14, 2018 at 13:09
  • what do you want the array to look like in the final result? can u make an example? Commented Apr 14, 2018 at 13:11
  • @sietse85 Second code, after "means should look like as follow" Commented Apr 14, 2018 at 13:12
  • 1
    $a1=array('0'=>$array1[0],'1'=>$array2[0]); $a2=array('0'=>$array1[1],'1'=>$array2[1]); Commented Apr 14, 2018 at 13:13
  • 1
    @MohdHasan You'll have to write a proper question so we can all understand what you need. stackoverflow.com/help/how-to-ask Commented Apr 14, 2018 at 13:17

1 Answer 1

1

You try this code

<?php

$array1=array('0'=>'abc','1'=>'xyz');
$array2=array('0'=>'pqr','1'=>'mno');

foreach($array1 as $key => $val){
    $array_name = "a".($key + 1);
    ${$array_name} = array();
    ${$array_name}[0] = $array1[$key];
    ${$array_name}[1] = $array2[$key];
}

var_dump($a1);
var_dump($a2);
Sign up to request clarification or add additional context in comments.

Comments

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.