1

I am new to php and trying to use map array. I would like my $arr2 to hold values of different types of fruits. But when I execute the following code, it tells me "apple" is an undefined index. Can you please show me how can I correct the following code so that I can accesses different values?

<?php
main_fun ();
function main_fun ()
{
    $arr2 = array();
    num2_data ($arr2); 

    echo "<br>.".$arr2["apple"]["q1"];    // quadrant 1 value of apple
    echo "<br>.".$arr2["orange"]["q2"];   // quadrant 2 value of orange      
}

function num2_data (&$arr2)
{       
    $arr2 = array("apple" => array("q1" => 1.1380711874577, "q2" => 1.7625118400083, "q3" => 1.8957417329238, "q4" => 2.4453545524829));        
    $arr2 = array("orange" => array("q1" => 1.1380711874577, "q2" => 2.5045795667914, "q3" => 1.8957417329238, "q4" => 2.7192512120757));
}   

?>
1
  • Thanks for all of your answers and showing me with example codes. that really helped
    – Justin k
    Commented Aug 7, 2013 at 11:36

1 Answer 1

8

In your function, the second assignment to $arr2 is overwriting the previous (with apple)

To add an element, you can set it like this...

$arr2["orange"] = array("q1" => 1.1380711874577, "q2" => 2.50457956679 ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.