TwistedBalloon Posted January 13, 2007 Share Posted January 13, 2007 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 textstrings and variable names. Does PHP allow variables to be stored as elements in arrays? I didn't see anythingin 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 elsewhereI can echo out the value of a $cell and it displays correctly. So far so good. I can also echo outthe values from the "chart F" array, but only as long as they are the text strings. If I try to accesssomething 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 More sharing options...
scotmcc Posted January 13, 2007 Share Posted January 13, 2007 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] Link to comment https://forums.phpfreaks.com/topic/34046-variables-as-elements-in-arrays/#findComment-160054 Share on other sites More sharing options...
TwistedBalloon Posted January 13, 2007 Author Share Posted January 13, 2007 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. Link to comment https://forums.phpfreaks.com/topic/34046-variables-as-elements-in-arrays/#findComment-160060 Share on other sites More sharing options...
scotmcc Posted January 13, 2007 Share Posted January 13, 2007 Glad that it helped :) Link to comment https://forums.phpfreaks.com/topic/34046-variables-as-elements-in-arrays/#findComment-160064 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.