简体   繁体   中英

How to get array of strings from output php

I do a print_r($my_var)

and get a result like:

Array ( [0] => Array ( [email] => maa@hotmail.com ) [1] => Array ( [email] => mee@gmail.com ) )

How to get a new array like

$mails=    array( 'maa@hotmail.com','mee@gmail.com' );
$mails = array_map(function ($x){
    return $x['email'];
}, $my_var);

Codepad Demo

Update

Using a foreach loop instead of array_map :

$mails = array();
foreach($my_var as $emailArr){
    $mails[] = $emailArr['email'];
}

Codepad Demo

array_values($myvar[0]);

// Will return the values inside you array ( I noticed your array was nested, so I did the values of he 0 element).

http://www.php.net/manual/en/function.array-values.php

Highly recommend just having a little read of all the available array functions just quickly so you can see what is possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM