1

I have this array

Array
(
[0] => Array
    (
        [0] => Name
        [1] => Email
        [2] => Address
    )

[1] => Array
    (
        [0] => vanson
        [1] => dhmax@pan.com
        [2] => gurgaon
    )

[2] => Array
    (
        [0] => john
        [1] => 
        [2] => 
    )

[3] => Array
    (
        [0] => sdf
        [1] => sdfsdf
        [2] => sdfsd
    )

)    

and i want this

Array
(       [0] => Name
        [1] => Email
        [2] => Address
        [0] => vanson
        [1] => dhmax@pan.com
        [2] => gurgaon
        [0] => john
        [1] => 
        [2] => 
        [0] => sdf
        [1] => sdfsdf
        [2] => sdfsd
  )    

WITHOUT OVERWRITING ITS INDEX

or i want to create dynamic arrays and put the same indexed values into individual arrays.something like this

$arr_1 = array(
                    [0] => Name
                    [1] => vanson
                    [2] => john
                    [3] => sdf
                    )
  $arr_2 = array(
                    [0] => Email
                    [1] => dhmax@pan.com
                    [2] => 
                    [3] => sdfsdf
                    )
   $arr_3 = array(
                    [0] => Address
                    [1] => gurgaon
                    [2] => 
                    [3] => sdfsd
                    )    

Arrays are created on the basis of the count of 1st indexed array.(Ex if 0th index has array of 4 then 4 arrays should be created.)

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
udgeet patel
  • 439
  • 1
  • 7
  • 23
  • 3
    You can't have more than one value for the same index. How would you be able to retrieve them individually if you did? Read up on arrays and how they work. – SISYN Dec 29 '12 at 05:19
  • As i have written in last code. I have to merge same indexed array values into one and other indexed array values into another array. So i thought if i merge all array index->values into one and then grab same indexed values into array. – udgeet patel Dec 29 '12 at 05:24

2 Answers2

3

PHP arrays cannot have multiple values with the same index, so your first suggestion is not possible.

However, the second option can be achieved with array_map(), by passing a null value as the callback parameter. Here's a convenient helper function that will do it (based on this excellent answer by Tadeck to another question):

function array_transpose( $array ) {
    $array = array_merge( array( null ), array_values( $array ) );
    return call_user_func_array( 'array_map', $array );
}

$columns = array_transpose( $rows );

(demo on codepad.org)

Community
  • 1
  • 1
Ilmari Karonen
  • 49,047
  • 9
  • 93
  • 153
-1

PHP have inbuilt function called array_chunk(),You can use it like this-

$array = Array(Array(1=>'Name',2=>'Email'),Array(1=>'suresh',2=>'email@gmail.com'));        
echo "<pre>";print_r(array_chunk($array,1,true));
Output-
Array ( [0] => Array ( [0] => Array ( [1] => Name [2] => Email ) ) [1] => Array ( [1] => Array ( [1] => suresh [2] => email@gmail.com ) ) ) 
Suresh Kamrushi
  • 15,627
  • 13
  • 75
  • 90