我正在使用关联数组,并将索引作为参数传递给函数。如果我尝试将它们作为参数传递并使用“ print_r”,则该函数将输出数组值及其所有索引而没有问题,我什至可以调用将这些索引的值存储到变量中并在浏览器中输出。>
我的问题是,如果我尝试这样编码:
$arr_specimen[$sel_ind1]
浏览器即使先前输出索引及其值,也显示未定义索引错误。该错误的可能解释是什么?如何纠正该错误?这是我的代码:
function array_picker($sel_ind1, $sel_ind2, $sel_ind3, $arr_specimen)
{
print "$sel_ind1 <br>";
print "$sel_ind2 <br>";
print "$sel_ind3 <br>";
echo "<pre>";
print_r($arr_specimen);
echo "</pre>";
}
$selection = array(
array('fruit' => 'apple', 'normal_price' => 3.75, 'status' => 'discounted'),
array('fruit' => 'orange', 'normal_price' => 4.15, 'status' => 'non-discounted'),
array('fruit' => 'grapes', 'normal_price' => 8.35, 'status' => 'discounted'),
array('fruit' => 'mango', 'normal_price' => 6.65, 'status' => 'discounted'),
array('fruit' => 'peach', 'normal_price' => 5.45, 'status' => 'non-discounted'),
array('fruit' => 'kiwi', 'normal_price' => 3.75, 'status' => 'non-discounted'),
array('fruit' => 'melon', 'normal_price' => 9.05, 'status' => 'non-discounted'),
array('fruit' => 'pomegranate', 'normal_price' => 7.95, 'status' => 'discounted')
);
array_picker('fruit', 'normal_price', 'status', $selection);
答案 0 :(得分:0)
如代码中所示访问$arr_specimen[$sel_ind1]
将访问'fruit'
中的索引$arr_specimen
,该索引位于下面的$selection
中。 $selection
不是关联数组,因此没有索引'fruit'
。您需要访问数组的第二维,如下所示:$arr_specimen[0][$sel_ind1]
,$arr_specimen[1][$sel_ind1]
。看看下面的修改后的代码。
function array_picker($sel_ind1, $sel_ind2, $sel_ind3, $arr_specimen)
{
print "$sel_ind1 <br>";
print "$sel_ind2 <br>";
print "$sel_ind3 <br>";
echo "<pre>";
print_r($arr_specimen[0][$sel_ind1]); //will get fruit index from first array
print_r($arr_specimen[1][$sel_ind2]); //will get price index from second array...
echo "</pre>";
}
答案 1 :(得分:0)
正如@AbraCadaver所暗示的,您正在尝试访问整数索引数组(而不是关联数组)的fruit
元素。
// your current array
$selection = array(
array('fruit' => 'apple', 'normal_price' => 3.75, 'status' => 'discounted'),
array('fruit' => 'orange', 'normal_price' => 4.15, 'status' => 'non-discounted'),
array('fruit' => 'grapes', 'normal_price' => 8.35, 'status' => 'discounted'),
array('fruit' => 'mango', 'normal_price' => 6.65, 'status' => 'discounted'),
array('fruit' => 'peach', 'normal_price' => 5.45, 'status' => 'non-discounted'),
array('fruit' => 'kiwi', 'normal_price' => 3.75, 'status' => 'non-discounted'),
array('fruit' => 'melon', 'normal_price' => 9.05, 'status' => 'non-discounted'),
array('fruit' => 'pomegranate', 'normal_price' => 7.95, 'status' => 'discounted')
);
// to do what you imply by your code
$selection = array(
'apple'=>array('normal_price' => 3.75, 'status' => 'discounted'),
'orange'=>array('normal_price' => 4.15, 'status' => 'non-discounted'),
'grapes'=>array('normal_price' => 8.35, 'status' => 'discounted'),
'mango'=>array('normal_price' => 6.65, 'status' => 'discounted'),
'peach'=>array('normal_price' => 5.45, 'status' => 'non-discounted'),
'kiwi'=>array('normal_price' => 3.75, 'status' => 'non-discounted'),
'melon'=>array('normal_price' => 9.05, 'status' => 'non-discounted'),
'pomegranate'=>array('normal_price' => 7.95, 'status' => 'discounted')
);
function array_picker($fruit, $sel_ind2, $sel_ind3, $arr_specimen)
{
print "{$arr_specimen[$fruit][$sel_ind2]} <br>";
print "{$arr_specimen[$fruit][$sel_ind3]} <br>";
echo "<pre>";
print_r($arr_specimen);
echo "</pre>";
}
array_picker('apple', 'normal_price', 'status', $selection);