PHP数组和未设置的函数

时间:2013-05-03 11:04:55

标签: php arrays multidimensional-array

我有这样的数组。

    Array
    (
        [0] => Array
            (
                [email] => abc@gmail.com
                [timestamp] => 2013-05-03 09:20:01
            )

        [1] => Array
            (
                [email] => def@gmail.com
                [timestamp] => 2013-05-03 09:20:23
            )

        [2] => Array
            (
                [email] => ghi@gmail.com
                [timestamp] => 2013-05-03 09:20:43
            )

    )

我希望这就像这样简单。

    Array
    (
        [0] => abc@gmai1.com
        [1] => def@gmail.com
        [2] => ghi@gmail.com
    )

我尝试过未设置功能,但它仍然没有像我预期的那样工作。

我对数组概念并不大,因此我的愚蠢问题! :(

6 个答案:

答案 0 :(得分:6)

我认为最好使用array_map代替unset

function filter($x)
{
    return $x['email'];
}

$emails = array_map('filter', $your_array);

这基本上会使用filter函数将输入数组映射到输出数组。

答案 1 :(得分:3)

foreach($foo as $key=>$value)
{
  $foo[$key] = $value['email'];
}

答案 2 :(得分:1)

只需从您的旧阵列中挑选您想要的内容并将其放入新阵列中。

$newArray = array();
foreach($oldArray as $containedArray)
{
  $newArray[] = $containedArray['email'];
}

var_dump($newArray);

答案 3 :(得分:1)

<?php
 $array = Array
    (
        Array
            (
                email => 'abc@gmail.com',
                timestamp => '2013-05-03 09:20:01'
            ),

        Array
            (
                email => 'def@yahoo.co.in',
                timestamp => '2013-05-03 09:20:23'
            ),

        Array
            (
                email => 'ghi@gmail.com',
                timestamp => '2013-05-03 09:20:43'
            ),

);


foreach ($array as $array){
   $newArray[] = $array['email'];
}

var_dump($newArray);

答案 4 :(得分:0)

没有必要取消任何事情。只需指定适当的值:

<?php
$array = array(
      array(
            "email" => 'aaa@aaa.com',
            "timestamp" => 2,
            ),
      array(
            "email" => 'bbb@aaa.com',
            "timestamp" => 3,
            ),
);

foreach($array as $key => $value)
{
      $array[$key] = $value["email"];
}

var_dump($array);

答案 5 :(得分:0)

试试这个,

 <?php
  $array= array(
    0 => array
        (
           'email' => 'abc@gmail.com',
            'timestamp' =>' 2013-05-03 09:20:01'
        ),

    1 => array
        (
            'email'=> 'def@gmail.com',
            'timestamp' => '2013-05-03 09:20:23'
        ),

    2 => array
        (
            'email'=> 'ghi@gmail.com',
            'timestamp' => '2013-05-03 09:20:43'
        )

 );

foreach($array as $key => $data)
{
$array[$key]=$data['email'];
}
  print_r($array);  

&GT;

你会得到

   Array
    (
     [0] => abc@gmai1.com
     [1] => def@gmail.com
     [2] => ghi@gmail.com
    )