2014-09-20 151 views
1

我有一個這樣的數組: [我有一個數組多維數組

0] => Array 
     (
      [u_id] => 1 
     ) 

    [1] => Array 
     (
      [u_fname] => Aristo 
     ) 

    [2] => Array 
     (
      [u_id] => 6 
     ) 

    [3] => Array 
     (
      [u_fname] => Anil 
     ) 

不過,我想我在這個形式的數組:

array(array[0]=> 
    array([uid]=>0,['u_name']=>Aristo)), 
    array([1]=>[uid]=>1,['u_name']=>Anil) 
+0

你到目前爲止嘗試過什麼?請發佈您的代碼。您是否搜索了您的問題的答案? – 2014-09-20 08:04:45

回答

1

試試這個:

<?php 

//this is your array in question 
$single = array(
    array(
     'u_id' => 1 
    ), 
    array(
     'u_fname' => Aristo 
    ), 
    array(
     'u_id' => 6 
    ), 
    array(
     'u_fname' => Anil 
    ) 
); 

print_r($single); 

$multi = array_map(function($item) { 
    $return = array(); 
    array_walk_recursive($item, function($value, $index) use (&$return) { 
     $return[$index] = $value; //flatten array, but keeping index 
    }); 
    return $return; 
}, array_chunk($single, 2)); //split array into sections containing two elements 

print_r($multi); 

Output

Array 
(
    [0] => Array 
     (
      [u_id] => 1 
     ) 

    [1] => Array 
     (
      [u_fname] => Aristo 
     ) 

    [2] => Array 
     (
      [u_id] => 6 
     ) 

    [3] => Array 
     (
      [u_fname] => Anil 
     ) 

) 
Array 
(
    [0] => Array 
     (
      [u_id] => 1 
      [u_fname] => Aristo 
     ) 

    [1] => Array 
     (
      [u_id] => 6 
      [u_fname] => Anil 
     ) 

)