0

I've the following array$_SESSION['survey_ans'][]=$records; and will get the result withvar_dump($_SESSION['survey_ans']);

array(6) {
    [0]=> array(1) {
        [1]=> string(5) "vpoor" 
    } 
    [1]=> array(1) { 
        [10]=> string(4) "poor" 
    } 
    [2]=> array(1) { 
        [6]=> string(7) "average" 
    } 
    [3]=> array(1) { 
        [11]=> string(4) "good" 
    } 
    [4]=> array(1) { 
        [12]=> string(5) "vgood" 
    } 
    [5]=> array(1) { 
        [13]=> string(4) "good" 
    } 
}

But when I run this

foreach($_SESSION['survey_ans'] as $key=>$value) {
    echo $key."-".$value."<br />";
}

I will get the error "Notice : Array to string conversion in ". So how do I get the result as following?

1, vpoor
10, poor
6, average
11, good
12, vgood
13, good

1 Answer 1

4

The elements of $_SESSION['survey_ans'] are arrays, so you need to iterate through the values in each array to get your desired output. Try this:

foreach($_SESSION['survey_ans'] as $result) {
    foreach ($result as $key => $value) {
        echo $key."-".$value."<br />";
    }
}

Output:

1-vpoor
10-poor
6-average
11-good
12-vgood
13-good

Demo on 3v4l.org

Sign up to request clarification or add additional context in comments.

4 Comments

When i put 0 and i got the following error?Notice : Undefined offset: 0
why would it be $value[0], when they are [1]=> string(5) ...,[10]=> string(4) ..., [6]=> string(7) ..., etc?
I got the result as below but not the expected one??0-vpoor 1-poor 2-average 3-good 4-vgood 5-good
thanks for everyone for the valuable knowledge to me.

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.