Php foreach键->;外部价值
我如何访问特定的“键”及其关联的“值”,以便以后在foreach循环之外使用 这是数组Php foreach键->;外部价值,php,arrays,foreach,custom-fields,Php,Arrays,Foreach,Custom Fields,我如何访问特定的“键”及其关联的“值”,以便以后在foreach循环之外使用 这是数组 Array ( [0] => CustomFields [1] => stdClass Object ( [Key] => Phone [Value] => 5555555 ) [2] => stdClass Object ( [Key] => City [Value] => New York) [3] => stdClass
Array ( [0] =>
CustomFields
[1] => stdClass Object
( [Key] => Phone [Value] => 5555555 )
[2] => stdClass Object
( [Key] => City [Value] => New York)
[3] => stdClass Object
( [Key] => State [Value] => NY)
[4] => stdClass Object
( [Key] => Cellphone [Value] => 222444555 )
这是我使用的查询
$cf = array();
foreach($result->response->CustomFields as $data) {
$cf [] = $data;
if ($cf [] = ($data->Key == 'Phone' ) ) {
echo 'Your Phone number is:'.$data->Value.'<br> ';
}
if ($cf [] = ($data->Key == 'City' ) ) {
echo 'Your City is: '.$data->Value.'<br> ';
}
}
$cf=array();
foreach($result->response->CustomFields作为$data){
$cf[]=$data;
如果($cf[]=($data->Key=='Phone')){
echo“您的电话号码是:”。$data->Value。“
”;
}
如果($cf[]=($data->Key=='City')){
echo“您的城市是:”。$data->Value。“
”;
}
}
我的查询在foreach循环中工作,并正确打印Phone和City值——但我希望能够在这个循环之外打印这些值 函数findByKeyInCollection($key,$collection){
function findByKeyInCollection($key, $collection){
foreach($collection as $data) {
if ($data->Key == $key) {
return $data;
}
}
}
$phone = findByKeyInCollection("Phone", $result->response->CustomFields);
echo 'Your Phone number is:'.$phone->Value.'<br> ';
foreach($收集为$数据){
如果($data->Key==$Key){
返回$data;
}
}
}
$phone=findByKeyInCollection(“phone”,$result->response->CustomFields);
回显“您的电话号码是:”。$Phone->Value。“
”;
Wow-谢谢你-效果很好-你让它看起来很简单!!非常感谢;-)