如何从PHP数组中获取单个值

时间:2021-08-12 04:14:23

Example print_r($myarray)

例子print_r(myarray美元)

Array
(
    [0] => Array
    (
        [id] => 6578765
        [name] => John Smith
        [first_name] => John
        [last_name] => Smith
        [link] => http://www.example.com
        [gender] => male
        [email] => email@domain.com
        [timezone] => 8
        [updated_time] => 2010-12-07T21:02:21+0000
    )
)

Question, how to get the $myarray in single value like:

问题是,如何以单个值获取$myarray,如:

echo $myarray['email'];  will show email@domain.com

7 个解决方案

#1


75  

Look at the keys and indentation in your print_r:

查看print_r中的键和缩进:

echo $myarray[0]['email'];

echo $myarray[0]['gender'];

...etc

#2


34  

Use array_shift function

使用array_shift函数

$myarray = array_shift($myarray);

This will move array elements one level up and you can access any array element without using [0] key

这将把数组元素提升到一个级别,您可以在不使用[0]键的情况下访问任何数组元素。

echo $myarray['email'];  

will show email@domain.com

将显示email@domain.com

#3


17  

I think you want this:

我想你想要的是:

foreach ($myarray as $key => $value) {
    echo "$key = $value\n";
}

#4


1  

The first element of $myarray is the array of values you want. So, right now,

$myarray的第一个元素是您想要的值的数组。所以,现在,

echo $myarray[0]['email']; // This outputs 'email@domain.com'

If you want that array to become $myarray, then you just have to do

如果你想让这个数组变成my$数组,那么你只需要这么做

$myarray = $myarray[0];

Now, $myarray['email'] etc. will output as expected.

现在,$myarray['email']等将按预期输出。

#5


1  

You can also use array_column(). It's available from PHP 5.5: php.net/manual/en/function.array-column.php

还可以使用array_column()。它可以从PHP 5.5获得:php.net/manual/en/function.array-column.php

It returns the values from a single column of the array, identified by the column_key. Optionally, you may provide an index_key to index the values in the returned array by the values from the index_key column in the input array.

它从数组的单个列返回值,由column_key标识。可选地,您可以提供一个index_key,通过输入数组中的index_key列中的值来索引返回数组中的值。

print_r(array_column($myarray, 'email'));

#6


1  

foreach ($newarray as $key => $val) {
    echo "$key = $val\n";
}

#7


0  

echo $myarray[0]->['email'];

Try this only if it you are passing the stdclass object

只有在传递stdclass对象时才尝试此操作

#1


75  

Look at the keys and indentation in your print_r:

查看print_r中的键和缩进:

echo $myarray[0]['email'];

echo $myarray[0]['gender'];

...etc

#2


34  

Use array_shift function

使用array_shift函数

$myarray = array_shift($myarray);

This will move array elements one level up and you can access any array element without using [0] key

这将把数组元素提升到一个级别,您可以在不使用[0]键的情况下访问任何数组元素。

echo $myarray['email'];  

will show email@domain.com

将显示email@domain.com

#3


17  

I think you want this:

我想你想要的是:

foreach ($myarray as $key => $value) {
    echo "$key = $value\n";
}

#4


1  

The first element of $myarray is the array of values you want. So, right now,

$myarray的第一个元素是您想要的值的数组。所以,现在,

echo $myarray[0]['email']; // This outputs 'email@domain.com'

If you want that array to become $myarray, then you just have to do

如果你想让这个数组变成my$数组,那么你只需要这么做

$myarray = $myarray[0];

Now, $myarray['email'] etc. will output as expected.

现在,$myarray['email']等将按预期输出。

#5


1  

You can also use array_column(). It's available from PHP 5.5: php.net/manual/en/function.array-column.php

还可以使用array_column()。它可以从PHP 5.5获得:php.net/manual/en/function.array-column.php

It returns the values from a single column of the array, identified by the column_key. Optionally, you may provide an index_key to index the values in the returned array by the values from the index_key column in the input array.

它从数组的单个列返回值,由column_key标识。可选地,您可以提供一个index_key,通过输入数组中的index_key列中的值来索引返回数组中的值。

print_r(array_column($myarray, 'email'));

#6


1  

foreach ($newarray as $key => $val) {
    echo "$key = $val\n";
}

#7


0  

echo $myarray[0]->['email'];

Try this only if it you are passing the stdclass object

只有在传递stdclass对象时才尝试此操作