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
Priyanka khullar
5171 gold badge5 silver badges26 bronze badges
asked Dec 7, 2018 at 5:22

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

answered Dec 7, 2018 at 5:26
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

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.