0

我有一个如下所示的多维数组。我想知道的是如何提取每个数组中的第一个数组元素并为所有第一个数组元素构造一个数组。

     Array (
        '0' => Array (
                   'd1' => 'D1',
                   'c1' => 'C1',
                   't1' => 'T1',
                   'a1' => 'A1',
                ),
        '1' =>Array (
                   'd2' => 'D2',
                   'c2' => 'C2',
                   't2' => 'T2',
                   'a2' => 'A2',
                ),
          );

格式化后,我需要像下面的数组一样的数组。

      Array => 
           '0' => Array (
                  'd1' => 'D1',
                  'd2' => 'D2',
                  ),
           '1' => Array (
                  'c1' => 'C1',
                  'c2' => 'C2',
                  ),
           '2' => Array (
                  't1' => 'T1',
                  't2' => 'T2',
                  ),
           '3' => Array (
                  'a1' => 'A1',
                  'a2' => 'A2',
                  ),
        );
4

3 回答 3

0
$a = Array (
'0' => Array (
           'd1' => 'D1',
           'c1' => 'C1',
           't1' => 'T1',
           'a1' => 'A1',
        ),
'1' =>Array (
           'd2' => 'D2',
           'c2' => 'C2',
           't2' => 'T2',
           'a2' => 'A2',
        ),
  );


foreach($a as $k=>$v)
{
    $i = 0;
    foreach($v as $k1=>$v1)
    {
        $b[$i][$k1] = $v1;
        $i++;
    }
}
echo "<pre>";
print_r($b);
于 2013-01-08T06:12:12.453 回答
0
    $array = Array (
    '0' => Array (
       'd1' => 'D1',
       'c1' => 'C1',
       't1' => 'T1',
       'a1' => 'A1',
        ),
     '1' =>Array (
       'd2' => 'D2',
       'c2' => 'C2',
       't2' => 'T2',
       'a2' => 'A2',
       ),
     );
 while (list($key, $value) = each($array)) 
    {
    $i = 0;
      foreach ($value as $key1 => $val1) 
    {
        $array2[$i][$key1] = $val1;
        $i++;
    }
   }
  echo "<pre>";
  print_r($array2);
于 2013-01-08T07:27:36.440 回答
0

$user = Array ( [0] => Array ( [id] => 1 [Name] => Kabelo [Last_name] => 5d41402abc4b2a76b9719d911017c592 [username] => 5d41402abc4b2a76b9719d911017c592 [password] => 5d41402abc4b2a76b9719d911017c592 [reg_date] => 0000-00 -00 00:00:00)

[1] => Array
    (
        [id] => 2
        [Name] => Kgotso
        [Last_name] => Lekula
        [username] => 5d41402abc4b2a76b9719d911017c592
        [password] =>5d41402abc4b2a76b9719d911017c592
        [reg_date] => 0000-00-00 00:00:00
    )

[2] => Array
    (
        [id] => 3
        [Name] => Me
        [Last_name] => White
        [username] => 5d41402abc4b2a76b9719d911017c592
        [password] => jsksksh
        [reg_date] => 0000-00-00 00:00:00
    )

[3] => Array
    (
        [id] => 4
        [Name] => Hai
        [Last_name] => Black
        [username] => maki@gmail.com
        [password] => 5d41402abc4b2a76b9719d911017c592
        [reg_date] => 0000-00-00 00:00:00
    )

[4] => Array
    (
        [id] => 5
        [Name] => Tsek
        [Last_name] => Lengitja
        [username] => tebza@make.com
        [password] => 5d41402abc4b2a76b9719d911017c592
        [reg_date] => 0000-00-00 00:00:00
    )

[5] => Array
    (
        [id] => 6
        [Name] => Akebatle
        [Last_name] => akitsi
        [username] => me@nksjkjshdks.co.za
        [password] => 5d41402abc4b2a76b9719d911017c592
        [reg_date] => 0000-00-00 00:00:00
    )

[6] => Array
    (
        [id] => 7
        [Name] => Jeez
        [Last_name] => motho
        [username] => tjatji@solomzi.com
        [password] => sadasdad
        [reg_date] => 0000-00-00 00:00:00
    )

)

            foreach ($user as $key => $value) {

              print_r($user[$key]['Name'];

            }
于 2014-04-26T23:42:38.823 回答