0

I have three arrays & i like to convert it in one multidimensional array.

$array1=array('Kathy', 'Adam', 'Jenny');
$array2=array('student','teacher','driver');
$array3=array(2, 5, 8);

$mix=array(); 
$mix['name']=array_values( $array1);
$mix['profession']=array_values( $array2);
$mix['SL']=array_values( $array3);

OUTPUT:
Array
(
[name] => Array
    (
        [0] => Kathy
        [1] => Adam
        [2] => Jenny
    )

[profession] => Array
    (
        [0] => student
        [1] => teacher
        [2] => driver
    )

[SL] => Array
    (
        [0] => 2
        [1] => 5
        [2] => 8
    )

If i want to print like below what should i do please heolp me........

[0]=>Array
      (
         [name] =>  Kathy
         [profession] =>student
         [SL] => 2
    )

    [1]=>Array
    (    
         [name] => Adam
         [profession] =>teacher
         [SL] =>  5
     )

    [2]=>Array
    (
         [name] => Jenny
         [profession] =>driver
         [SL] =>  8
    )
4
Contributors
3
Replies
5
Views
4 Years
Discussion Span
Last Post by sanket044
0

declare a array with a name "$my_global"

reading individual values of those 3 arrays and make a new array as follows

$my_array = array("name"=> $array_1[$i],"profession"=>$array_2[$i],"SL"=>$array_3[$i]);  

(in your case  ,loop this for 3 times)

and combine the $my_array to $my_global

by doing this way i think you will get your actual output

please try it once

Edited by radhakrishna.p: giving clear way to his solution

1
$array1=array('Kathy', 'Adam', 'Jenny');
$array2=array('student','teacher','driver');
$array3=array(2, 5, 8);

for($x=0;$x<count($array1);$x++) $my_array[] = array("name"=>$array1[$x],"profession"=>$array2[$x],"SL"=>$array3[$x]);

I think that's pretty much it.

Edited by diafol

-1
$array1 = array('Kathy', 'Adam', 'Jenny');
$array2 = array('student','teacher','driver');
$array3 = array(2, 5, 8);
$my_array = array();
for($i=0;$i<count($array1);$i++){
   $my_array[$i] = array("name"=>$array1[$i],"profession"=>$array2[$i],"SL"=>$array3[$i]);
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.