简体   繁体   English

从键/值对的数组中获取信息

[英]getting information out of an array with key/value pairs

I have the following snippet of code that is creating the following array... 我有以下代码片段创建以下数组...

while ($stmt->fetch()) {
  foreach($row as $key => $val) {
    $x[$key] = $val;
  }
  $results[] = $x;
}

Results in the follow array: 结果如下:

Array ( [0] => Array ( [cap_login] => master [cap_pword] => B-a411dc195b1f04e638565e5479b1880956011badb73361ca ) ) 

Basically I want to extract the cap_login and cap_pword values for testing. 基本上我想提取cap_login和cap_pword值以进行测试。 For some reason I can't get it! 出于某种原因,我无法得到它! I've tried this kind of thing: 我试过这种事:

echo $results[$cap_login]; 

but I get the error 但我得到了错误

Undefined variable: cap_login

Can someone put me right here? 有人能把我放在这儿吗? Thanks. 谢谢。

cap_login在$ results中的数组中,所以你必须做$results[0]['cap_login']

You would have to do the following: 您必须执行以下操作:

echo $x[0]['cap_login'] . '<br />';
echo $x[0]['cap_pword'];

The reson $results[$cap_login] wont work is because there isn't a variable called $cap_login , there is a string called cap login. $results[$cap_login]无法工作是因为没有名为$cap_login的变量,有一个名为cap login的字符串。 In addition, there isn't a key in $results called $cap_login. 另外,$ result_login中的$ result中没有关键字。 There is a value in $results called 'cap_login' $ result中有一个名为'cap_login'的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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