我尝试通过' field‘的值搜索这个数组,然后返回数组中与该字段相关联的' value’的值。
例如,我想说“告诉我与theThirdField相关的值”。
我已经尝试了很多很多类似下面这样的排列:$myVariable =$Array‘’result‘;
为了进一步澄清,我永远不会知道我的搜索字段位于哪个顺序/子数组中,我只知道与' field‘相关的字符串值。
我该如何做到这一点呢?
Array
(
[result] => Array
(
[totalrows] => 1
[rows] => Array
(
[0] => Array
(
[rownum] => 1
[values] => Array
(
[0] => Array
(
[field] => testMeOnce
[value] => 436586498
)
[1] => Array
(
[field] => testMeTwice
[value] => 327698034
)
[2] => Array
(
[field] => theThirdField
[value] => 108760374
)
[3] => Array
(
[field] => theFourthField
[value] => 2458505
)
[4] => Array
(
[field] => fifthField
[value] => -0.0201
)
)
)
)
)
)发布于 2017-05-10 05:11:47
你有没有料到会发生这样的事情?
$needle = 'theThirdField'; // searched field name
$valuesArray = $Array['result']['rows'][0]['values']; //now you have clearer array
array_walk($valuesArray, function($element, $key) use ($needle) {
if ($element['field'] == $needle) {
echo $element['value'];
}
});发布于 2017-05-10 05:28:32
假设您只想在$myVariable维度中搜索,我会这样做:
$myVariable = $Array['result']['rows'][0]['values'];
foreach ($myVariable as $key => $value) {
if( $value['field'] === 'theThirdField' ) {
echo $value['value'];
break;
}
}发布于 2017-05-10 05:36:06
当我运行你的代码时,我得到了一些语法错误。我已经将您的数组改为这样(仅更改了语法):
$a = Array
(
'result' =>
[
'totalrows' => 1,
'rows' =>
[
0 =>
[
'rownum' => 1,
'values' =>
[
0 =>
[
'field' => 'testMeOnce',
'value' => 436586498
],
1 =>
[
'field' => 'testMeTwice',
'value' => 327698034
],
2 =>
[
'field' => 'theThirdField',
'value' => 108760374
],
3 =>
[
'field' => 'theFourthField',
'value' => 2458505
],
4 =>
[
'field' => 'fifthField',
'value' => -0.0201
]
]
]
]
]
);现在您可以获得如下所示的值:
print($a['result']['rows'][0]['values'][2]['value']); // --> 108760374我希望这就是你正在寻找的!
https://stackoverflow.com/questions/43879656
复制相似问题