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
1 Answer 1
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
answered Dec 7, 2018 at 5:26
Sign up to request clarification or add additional context in comments.
4 Comments
chancj
When i put 0 and i got the following error?Notice : Undefined offset: 0
Sean
why would it be
$value[0]
, when they are [1]=> string(5) ...
,[10]=> string(4) ...
, [6]=> string(7) ...
, etc?chancj
I got the result as below but not the expected one??0-vpoor 1-poor 2-average 3-good 4-vgood 5-good
chancj
thanks for everyone for the valuable knowledge to me.
lang-php