0

I have an array like this.

    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
            )

    )

I want this to be as simple as like this.

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

I have tried unset function but it still doesn't work as i expected.

I am not big into array concept and hence my stupid questions !!! :(

4

6 回答 6

6

I think that it'd be better to use array_map instead of unset:

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

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

This basically will map your input array into output array using filter function.

于 2013-05-03T11:06:31.393 回答
3
foreach($foo as $key=>$value)
{
  $foo[$key] = $value['email'];
}
于 2013-05-03T11:06:52.077 回答
1

Simply pick from your old array what you want and put it in a new one.

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

var_dump($newArray);
于 2013-05-03T11:06:56.400 回答
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);
于 2013-05-03T11:14:47.437 回答
0

There is no need to unset anything. Just assign the appropriate value:

<?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);
于 2013-05-03T11:10:00.190 回答
0

Try this,

 <?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);  

?>

You will get

   Array
    (
     [0] => abc@gmai1.com
     [1] => def@gmail.com
     [2] => ghi@gmail.com
    )
于 2013-05-03T11:22:09.863 回答