Передача индексов ассоциативного массива в функции в качестве аргументов - PullRequest
0 голосов
/ 05 июля 2018

Я работаю с ассоциативными массивами и передаю индексы в качестве аргументов в функцию. Если я попытаюсь передать их в качестве аргументов и использовать «print_r», функция выводит значения массива и все его индексы без проблем, я даже могу вызвать сохранение значений этих индексов в переменных и вывести их в браузере.

Моя проблема в том, что если я попытаюсь закодировать это так:

$arr_specimen[$sel_ind1]

браузер показывает ошибку неопределенного индекса, даже если он ранее выводил индексы и их значения. Каково возможное объяснение этой ошибки и как я могу исправить эту ошибку? вот мой код:

<code>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 "
"; } $ selection = array ( массив ('fruit' => 'apple', 'normal_price' => 3.75, 'status' => 'discount'), массив ('fruit' => 'orange', 'normal_price' => 4.15, 'status' => 'non-Discounts'), массив ('fruit' => 'grapes', 'normal_price' => 8.35, 'status' => 'Discounts'), массив ('fruit' => 'mango', 'normal_price' => 6.65, 'status' => 'Discounts'), массив ('fruit' => 'peach', 'normal_price' => 5.45, 'status' => 'non-Discounts'), массив ('fruit' => 'kiwi', 'normal_price' => 3.75, 'status' => 'non-discount'), массив ('fruit' => 'melon', 'normal_price' => 9.05, 'status' => 'non-Discount'), массив ('fruit' => 'pomegranate', 'normal_price' => 7.95, 'status' => 'discount') ); array_picker ('fruit', 'normal_price', 'status', $ selection);

Ответы [ 2 ]

0 голосов
/ 05 июля 2018

Как уже упоминалось в @AbraCadaver, вы пытаетесь получить доступ к элементу fruit целочисленного индекса (не к ассоциативному массиву).

<code>// 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 "
"; } array_picker ('apple', 'normal_price', 'status', $ selection);
0 голосов
/ 05 июля 2018

Доступ к $arr_specimen[$sel_ind1], как показано в вашем коде, приведет к доступу к индексу 'fruit' в $arr_specimen, который $selection ниже. $selection не является ассоциативным массивом, поэтому не имеет индекса 'fruit'. Вам необходимо получить доступ ко второму измерению массивов, как показано ниже: $arr_specimen[0][$sel_ind1], $arr_specimen[1][$sel_ind1]. Посмотрите на модифицированный код ниже.

<code>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 "
"; }
...