PHP:获取关联数组的第n个项目 [英] PHP: Get n-th item of an associative array
本文介绍了PHP:获取关联数组的第n个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果您有关联数组:
Array
(
[uid] => Marvelous
[status] => 1
[set_later] => Array
(
[0] => 1
[1] => 0
)
[op] => Submit
[submit] => Submit
)
您想访问第二项,该怎么做? $arr[1]
似乎不起作用:
And you want to access the 2nd item, how would you do it? $arr[1]
doesn't seem to be working:
foreach ($form_state['values']['set_later'] as $fieldKey => $setLater) {
if (! $setLater) {
$valueForAll = $form_state['values'][$fieldKey];
$_SESSION[SET_NOW_KEY][array_search($valueForAll, $form_state['values'])] = $valueForAll; // this isn't getting the value properly
}
}
该代码应产生:
$_SESSION[SET_NOW_KEY]['status'] = 1
但是它只会产生一个空白条目.
But it just produces a blank entry.
推荐答案
使用 array_slice
$second = array_slice($array, 1, 1, true); // array("status" => 1)
// or
list($value) = array_slice($array, 1, 1); // 1
// or
$blah = array_slice($array, 1, 1); // array(0 => 1)
$value = $blah[0];
这篇关于PHP:获取关联数组的第n个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文