Jump to content

Recommended Posts

$_SESSION['amounts'] = Array("first" => 70,"second" => 30);

 

Ok, that line makes my array and it work when numbers are put manually. Now I want to auto-populate it from values like:

 

$item1 = "first";

$volume1 = 70;

 

$item2 = "second";

$volume2 = 30;

 

Item and volume variables times ten from $item1 to $item10.

 

This does not work:

$_SESSION['amounts'] = Array($item1 => $volume1,$item2 => $volume2);

 

If someone could help me to correct syntax.

Link to comment
https://forums.phpfreaks.com/topic/134436-auto-populating-an-array/
Share on other sites

I'm not sure if keys can be variables.

Try

<?php
$_SESSION['amounts'] = array("$item1" => "$volume1" , "$item2" => "$volume2");
?>

 

That did not work either. var_dump($_SESSION); gave me this line:

 

array(1) { ["amounts"]=>  array(1) { [""]=>  string(0) "" } }

 

Not really making allot of sense as to why this would be usefull but...

 

$_SESSION['amounts'] = array();
for ($i=1;$i<=10;$i++) {
  $_SESSION['amounts'][${'item' . $i}] = ${'volume' . $i};
}

 

Thank you very much, that syntax worked. If you wonder what is this for, so first I read 2xten variables from data-file, and then I build array with this code enter it to graph building function.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.