新的PHP。从数组中检索信息

时间:2014-11-20 13:47:03

标签: php json

嘿伙计们试图从这个数组中检索信息。

Array
(
    [userid] => 1
    [username] => dunkers
    [password] => 699e5fae54df4c82314e42dd86c4d383
    [email] => a.dunkerley7462@student.leedsb
)
Array
(
    [userid] => 2
    [username] => mantis
    [password] => 699e5fae54df4c82314e42dd86c4d383
    [email] => ad481993@hotmail.com
)
Array
(
    [userid] => 190
    [username] => helenanderson94
    [password] => 699e5fae54df4c82314e42dd86c4d383
    [email] => helenandersonnn@hotmail.co.uk
)
Array
(
    [userid] => 191
    [username] => dd
    [password] => 699e5fae54df4c82314e42dd86c4d383
    [email] => dd@dd.com
)

我试图为每个循环选择,只选择用户名的值,我已经研究了几个小时,不管怎么说我尝试它想出非法字符串等

 foreach($encodedResults as $row){

    $decode = json_decode($row, true);

    print_r($decode);

这只是一些初学者代码,我只是为了从数组中提取所有信息,但我只想要用户名的值

2 个答案:

答案 0 :(得分:0)

这样的事情:

foreach($encodedResults as $row){
    $decode = json_decode($row, true);
    echo $decode['username'];
}

答案 1 :(得分:0)

很棒的工作让他们输出,这是一个回显用户名的例子。

// Loop through JSON encoded entries
foreach($encodedResults as $row)
{
    // Decode result to an array
    $decode = json_decode($row, true);

    // Echo out the username key in the decode array.
    echo $decode['username'];
}

如果您有任何疑问,请与我联系!