访问PHP数组元素 [英] Access PHP array element
问题描述
我很难访问我的PHP数组中的特定项目,该数组是这样创建的: array('expected'=> array('form','title'))
I struggle to access specific item within my PHP array, which is created like this array('expected' => array('form', 'title'))
.Array
(
[expected] => Array
(
[0] => form
[1] => title
)
)
我想访问第一个数组的标题( expected )以及该数组中元素的值( form 和 title )我尝试了诸如 array_values()
或 key
之类的方法,但我从未获得正确的结果.
I would like to access the title of the first array (expected) as well as the value of the element within this array (form and title)
I tried methods such as array_values()
or key
but I never get the right results.
编辑多亏了Aamir,这个问题得以解决.实际上,这是因为我将数组作为参数传递给方法,并且默认情况下将其设置为null.是的,我知道,愚蠢的东西.
EDIT Thanks to Aamir, this problem is solved. In fact, it was because I pass the array as a parameter into a method and I set it to null by default. Yes I know, dumb stuff.
推荐答案
foreach($array as $key => $value){
echo $key; //expected
echo $value[0]; //form
echo $value[1]; //title
//OR if you have more values then
foreach ($value as $key1 => value1){
echo $value1; //form in 1st iteration and title in 2nd iteration
}}
这篇关于访问PHP数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!