Jump to content

Variables as elements in arrays?


TwistedBalloon

Recommended Posts

Hi Folks,

I'm a newbie, so brace yourself.

I've got a project where I'm trying to access data from an array. The array data consists of text
strings and variable names. Does PHP allow variables to be stored as elements in arrays? I didn't see anything
in the arrays section of the PHP manual that forbids it, but it doesn't seem to work.

Here's some of the code:

//first I define the array.
$ChartF = array(
array('Apartment',$cell1,$cell2,$cell3),
array('Hotel or Motel', $cell4,$cell5,$cell6),
array('Hospital',$cell7, $cell8, $cell9),
array('Industrial',$cell10, $cell11, $cell12),
array('Municipal',$cell13,$cell14,$cell15)
);

//Then I give values to the various $cell variables:

                      $cell1 = $splits[0]."<p>".$splits[1]."<p>".$splits[2];
$cell2 = $splits[1]."<p>".$splits[2]."<p>".$splits[5];
                        //etc etc -- the values for these $splits are defined elsewhere

I can echo out the value of a $cell and it displays correctly. So far so good. I can also echo out
the values from the "chart F" array, but only as long as they are the text strings. If I try to access
something like $ChartF[0][1], I get nothing. If I put quotations around the variable name, I can
echo out the variable name but not its value. Surely my newbieness is painfully obvious at this point.Won't somebody rescue me from my ignorance and tell me what I've done wrong?

Dave


Link to comment
https://forums.phpfreaks.com/topic/34046-variables-as-elements-in-arrays/
Share on other sites

I am sorry Dave, I am a little confused by your question.

Are you trying to create the array and then later assign values to that array? What part of the array are you trying to display? Here is some example code for a multi-dimensional array. Hopefully it will help you a little.

Scot

[code]
<?php
// Set up initial values for the array
$test1 = 'test1';
$test2 = 'test2';
$test3 = 'test3';
$test4 = 'test4';
$test5 = 'test5';
$test6 = 'test6';
$test7 = 'test7';
$test8 = 'test8';
$test9 = 'test9';

// Create the array
$ChartF = array(
            array('First Test',$test1,$test2,$test3),
            array('Second Test', $test4,$test5,$test6),
            array('Third Test',$test7, $test8, $test9),
            );

// Display the initial values of some items
echo $ChartF[0][0].'<br>';
echo $ChartF[0][1].'<br>';

// Set new values for some items
$ChartF[0][1] = 'New Value';
echo $ChartF[0][1];
?>
[/code]
Scot,
thanks for your fast reply. I figured out what I was doing wrong based on what you said--not the way you might have expected, though.

Yeah, basically what I'm trying to do is to populate an array with variables, and in
turn the values of those variables are created through combinations of other variable values, generated by a formula, and so on and so on. The whole thing should be done with a database, but I can't do that for this job.

Anyway, your post made me see the stupid noob mistake I was making. I put the code for the array _before_ the code defining those variables, so the variables didn't contain any values. Doh!

Your response, written in the correct order, made me realize my mistake. Everything works as expected now.

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.