Jump to content

Recommended Posts

I'm more or less trying to get $items[$i] to equal $item_nameX with X being $i. If you look at the code you should be able to understand what I'm trying to do.

<?PHP
$item_name1 = "Armor";
$item_name2 = "Pants";
$item_name3 = "Gloves";
$item_name4 = "Helmet";
$item_name5 = "Shield";
$num_cart = 5 +1;

for ($i=1; $i < $num_cart; $i++)
{
[color=red]/*********************************
All I need this line of code todo
is for $items[$i] = $item_name$i
*********************************/
[b]$items[$i] = `echo \$item_name$i`;[/b][/color]
}

$num = count($items);
$num = $num -1;

for ($i=1; $i < $num_cart; $i++)
{
echo $items[$i];
echo '<br>';
}


?>
Link to comment
https://forums.phpfreaks.com/topic/13914-simple-array-help/
Share on other sites

Try this:
[code]<?PHP
$item_name1 = "Armor";
$item_name2 = "Pants";
$item_name3 = "Gloves";
$item_name4 = "Helmet";
$item_name5 = "Shield";
$num_cart = 6;

for ($i=1; $i < $num_cart; $i++)
$items[] = ${'item_name'.$i}; // this is the line you're looking for -- it's using variable variables
echo implode('<br>',$items);
?>[/code]

Variable variables are explained in the manual: http://us3.php.net/manual/en/language.variables.variable.php

Ken
Link to comment
https://forums.phpfreaks.com/topic/13914-simple-array-help/#findComment-54193
Share on other sites

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.