Search code examples
phparrayssessionnotice

Notice: Array to string conversion convert array to string


I want to convert all session's keys and values to the string. When I run this code:

<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);
$array['language'] = 'Asdfgh';
$array['currency'] = 'Asdfgh';
$array['cart'][] = 'Asdfgh';
$array['cart'][] = 'Asdfgh';
$array['facebookpagelikeshowed'] = 'Asdfgh';
$array['vid_yMHe4j2Ouko']['uid'] = '170785079';
$array['vid_yMHe4j2Ouko']['vid'] = '170785079';

    $text = '';
    foreach ($array as $key => $value)
    { 
        $text .= ''.$key.'='.$value.';';
    }
    echo $text;
?>

I got this error:

Notice: Array to string conversion in /var/www/index.php on line 16

Why? How can I resolve it?


Solution

  • The problem is when foreach reach the $key vid_yMHe4j2Ouko $value is of type array so you can't use the concat operator (. $value)