Jump to content

Auto-populating an array


dappa600

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.