dappa600 Posted November 27, 2008 Share Posted November 27, 2008 $_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. Quote Link to comment https://forums.phpfreaks.com/topic/134436-auto-populating-an-array/ Share on other sites More sharing options...
DeanWhitehouse Posted November 27, 2008 Share Posted November 27, 2008 I'm not sure if keys can be variables. Try <?php $_SESSION['amounts'] = array("$item1" => "$volume1" , "$item2" => "$volume2"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/134436-auto-populating-an-array/#findComment-699880 Share on other sites More sharing options...
dappa600 Posted November 27, 2008 Author Share Posted November 27, 2008 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) "" } } Quote Link to comment https://forums.phpfreaks.com/topic/134436-auto-populating-an-array/#findComment-699887 Share on other sites More sharing options...
DeanWhitehouse Posted November 27, 2008 Share Posted November 27, 2008 try manually entering the keys, although i can't see why the keys would be dynamic any way they are for your reference really. Quote Link to comment https://forums.phpfreaks.com/topic/134436-auto-populating-an-array/#findComment-699890 Share on other sites More sharing options...
dappa600 Posted November 27, 2008 Author Share Posted November 27, 2008 try manually entering the keys, although i can't see why the keys would be dynamic any way they are for your reference really. Entering values manually, don't work because I read values from data-file which can be different every time. Quote Link to comment https://forums.phpfreaks.com/topic/134436-auto-populating-an-array/#findComment-699960 Share on other sites More sharing options...
DeanWhitehouse Posted November 27, 2008 Share Posted November 27, 2008 Not the values, the keys. Quote Link to comment https://forums.phpfreaks.com/topic/134436-auto-populating-an-array/#findComment-699962 Share on other sites More sharing options...
trq Posted November 27, 2008 Share Posted November 27, 2008 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}; } Quote Link to comment https://forums.phpfreaks.com/topic/134436-auto-populating-an-array/#findComment-699964 Share on other sites More sharing options...
dappa600 Posted November 27, 2008 Author Share Posted November 27, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/134436-auto-populating-an-array/#findComment-700003 Share on other sites More sharing options...
trq Posted November 27, 2008 Share Posted November 27, 2008 Actually, a better method (and one I wasn't aware of) would be to use compact. Quote Link to comment https://forums.phpfreaks.com/topic/134436-auto-populating-an-array/#findComment-700006 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.