如何根据PHP数组的长度声明多个会话变量

session_start();
$imgList = $_REQUEST['imgList']; //comes through post (jquery)
$i = 0;
$a = explode(',', $imgList); 
$abc = count($a);

for ($e = 0; $e < $abc; $e++)
{
  echo $_SESSION['c+$i']=$a[$e];
  $i++;
}

echo $_SESSION['c+$i']; //this returns the last value
echo count($_SESSION['c+$i']); //returns only length 1
echo count($i); // returns only length 1

I don't know why more than one variable are not initilizing. can anyone please tell me about htis problem or fix this.i"ll be very thankful to you for this favor

Change your code completely like below:-

session_start();
$_SESSION = array();
$imgList = $_REQUEST['imgList']; //comes through post (jquery)
$a = explode(',', $imgList); 
$_SESSION['c'][] = $a;
print_r($_SESSION['c']);

Now on other page if you need to access this SESSION data, do like below:-

session_start();
print_r($_SESSION['c']);
foreach($_SESSION['c'] as arr){
  echo $arr."
";
}
 echo $_SESSION['c+$i']=$a[$e];

please use "c+$i" instead of 'c+$i'.

As I said in my comment, you are using single quotes in your $SESSION array key, hence, it will always literally be c+$i, no matter what value $i has.

You need to use either double quotes :

$_SESSION["c+$i"]=$a[$e];

Or the PHP concatenating operator :

$_SESSION['c+' . $i] = $a[$e];

BTW, there's no need for an echo there, as there's no need for a $i variable, since $e already ranges from 0 to $abc and is equal to $i in each for iteration.

Also, count($i) will always return 1 , since $i is an int.

session_start();

// More important evalutate vars to avoid log errors en your server
if( isset( $_REQUEST['imgList'] ) && !empty( $_REQUEST['imgList'] ) )
{
    // $imgList = $_REQUEST['imgList']; remove, make direct,
    // $i = 0; remove, optimize recourses, 
    $a = explode(',', trim( $_REQUEST['imgList'], "," ) );  // trim to remove empty positions in array
    // $abc = count($a); remove, optimize recourses, free memory

    for ($e = 0; $e < count( $a ); $e++)
    {
        $_SESSION[ "c_".$e ] = $a[ $e ]; // remove echo, "+" what is ?? separator?? if it is, change by underscope
    }   
    // echo $_SESSION["c_0"]; // 0,1,2 at length $a -1
    // echo count($_SESSION['c+$i']); // you count string ??? or array???, I don't understand this
    // echo count($i); // returns only length 1 // count a one number????, I don't understand this

    // to print
    for ($e = 0; $e < count( $a ); $e++)
    {
        echo $_SESSION[ "c_".$e ]; // print value of session in position $e
    }

    echo count( $e ); // Number of vars sesions created!

    // to remove
    for ($e = 0; $e < count( $a ); $e++)
    {
        unset( $_SESSION[ "c_".$e ] ); // remove var session in position $e
    }
}