0

I have an array like this.

    Array
    (
        [0] => Array
            (
                [email] => [email protected]
                [timestamp] => 2013-05-03 09:20:01
            )

        [1] => Array
            (
                [email] => [email protected]
                [timestamp] => 2013-05-03 09:20:23
            )

        [2] => Array
            (
                [email] => [email protected]
                [timestamp] => 2013-05-03 09:20:43
            )

    )

I want this to be as simple as like this.

    Array
    (
        [0] => [email protected]
        [1] => [email protected]
        [2] => [email protected]
    )

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 !!! :(

6 Answers 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.

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

@hjaffer2001: you should consider to accept walkhand answer. Filter is better and more elegant respect this "flat-foreach" :)
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);

Comments

1
<?php
 $array = Array
    (
        Array
            (
                email => '[email protected]',
                timestamp => '2013-05-03 09:20:01'
            ),

        Array
            (
                email => '[email protected]',
                timestamp => '2013-05-03 09:20:23'
            ),

        Array
            (
                email => '[email protected]',
                timestamp => '2013-05-03 09:20:43'
            ),

);


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

var_dump($newArray);

Comments

0

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

<?php
$array = array(
      array(
            "email" => '[email protected]',
            "timestamp" => 2,
            ),
      array(
            "email" => '[email protected]',
            "timestamp" => 3,
            ),
);

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

var_dump($array);

Comments

0

Try this,

 <?php
  $array= array(
    0 => array
        (
           'email' => '[email protected]',
            'timestamp' =>' 2013-05-03 09:20:01'
        ),

    1 => array
        (
            'email'=> '[email protected]',
            'timestamp' => '2013-05-03 09:20:23'
        ),

    2 => array
        (
            'email'=> '[email protected]',
            'timestamp' => '2013-05-03 09:20:43'
        )

 );

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

?>

You will get

   Array
    (
     [0] => [email protected]
     [1] => [email protected]
     [2] => [email protected]
    )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.