foreach 键 -> foreach 外的值
foreach key -> value outside foreach
我如何才能访问特定的 'keys' 及其关联的 'values' 以便稍后在 foreach 循环之外使用?
这是数组;
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> ';
}
}
我的查询在 foreach 循环内运行并正确打印 Phone 和 City 值 - 但我希望能够在此循环外打印这些值。
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> ';
我如何才能访问特定的 'keys' 及其关联的 'values' 以便稍后在 foreach 循环之外使用?
这是数组;
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> ';
}
}
我的查询在 foreach 循环内运行并正确打印 Phone 和 City 值 - 但我希望能够在此循环外打印这些值。
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> ';