如何在多维数组中获取每个数组的第一个元素?

时间:2013-01-08 06:04:02

标签: php arrays codeigniter

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

     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',
                  ),
        );

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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);

答案 2 :(得分:0)

$ user = 排列 (     [0] =&gt;排列         (             [id] =&gt; 1             [姓名] =&gt; Kabelo             [Last_name] =&gt; 5d41402abc4b2a76b9719d911017c592             [username] =&gt; 5d41402abc4b2a76b9719d911017c592             [密码] =&gt; 5d41402abc4b2a76b9719d911017c592             [reg_date] =&gt;
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'];

            }