talknerdy2mee Posted September 30, 2009 Share Posted September 30, 2009 I'm not sure what I'm doing wrong here. I tried searching but didn't really come up with anything that helped, but if you can point me to a thread that addresses this, that'd be awesome! So basically I have this code: function someFunc($someInt) { ...do some things with $someInt... return $someArray; } $someArray is numerically indexed. If I do: $myVar=someFunc($myInt); echo $myVar[3]; I get the correct output. But if I do: $myVar[$myInt]=someFunc($myInt); echo $myVar[$myInt][3]; I get: Array[3] I even tried: $myVarTemp=someFunc($myInt); echo $myVarTemp[3]/n; $myVar[$myInt][3]=$myVarTemp[3]; echo $myVar[$myInt][3]; Which outputs: 33 Array[3] What am I missing?? Quote Link to comment https://forums.phpfreaks.com/topic/176014-solved-multidimensional-array-what-am-i-doing-wrong/ Share on other sites More sharing options...
RussellReal Posted September 30, 2009 Share Posted September 30, 2009 try this.. I know this looks like a trivial solution but you really DO need to specify that a variable will be used as an array <?php $myInt = 10; $myVar = array(); $myVar[$myInt] = someFunc($myInt); ?> That SHOULD solve the problem Quote Link to comment https://forums.phpfreaks.com/topic/176014-solved-multidimensional-array-what-am-i-doing-wrong/#findComment-927472 Share on other sites More sharing options...
talknerdy2mee Posted September 30, 2009 Author Share Posted September 30, 2009 Nope . Tried: $myVar=array(); $myVar[]=array(); $myVar[$myInt]=array(); None of which changed the behavior. Quote Link to comment https://forums.phpfreaks.com/topic/176014-solved-multidimensional-array-what-am-i-doing-wrong/#findComment-927479 Share on other sites More sharing options...
talknerdy2mee Posted September 30, 2009 Author Share Posted September 30, 2009 Okay, so I sanitized my code, and am posting the whole routine here. I've added Russell's $myVar=array(); and still get the same output. Obviously I'm using an included function file in the actual app I'm using, but for the sake of being able to post this easily, I've included everything here. Hopefully someone can help me - I know this should be easy, but for whatever reason, I'm getting tripped up by this. <html> <body> <?php function someFunc($someInt) { $i=1; while ($i<5) { //some specific code here, but it doesn't really matter now $redAssoc[$i]=5+$i; //actually set to an integer calculated above $whiteAssoc[$i]=6-$i; //ditto $i++; } return array($redAssoc, $whiteAssoc); } $x=1; $redArr=array(); $whiteArr=array(); while($x<5) { list($red, $white)=someFunc($x); list($redArr[$x], $whiteArr[$x])=someFunc($x); $y=1; while ($y<5) { //doing some more calculations here //but for the time being just echoing to troubleshoot variables. echo "Red: $red[$y], White: $white[$y]<br>"; echo "Red Array: $redArr[$x][$y], White Array: $whiteArr[$x][$y]<br>"; $y++; } $x++; } ?> </body> </html> I've also attached a .php file with the above code if anyone wants to download and play. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/176014-solved-multidimensional-array-what-am-i-doing-wrong/#findComment-927485 Share on other sites More sharing options...
RussellReal Posted September 30, 2009 Share Posted September 30, 2009 you don't return [3] in that array.. you don't even get to 2 oh I'm sorry I didn't read the rest of the code I just looked @ the function if you're judging off of the echos maybe surround the $whatever[$x][$r] with curly braces { } Quote Link to comment https://forums.phpfreaks.com/topic/176014-solved-multidimensional-array-what-am-i-doing-wrong/#findComment-927486 Share on other sites More sharing options...
corbin Posted September 30, 2009 Share Posted September 30, 2009 list($red, $white)=someFunc($x); list($redArr[$x], $whiteArr[$x])=someFunc($x); You're using that incorrectly for how you're using it later. The syntax: list(a, b, c, d) = array(0, 1, 2, 3) Would set: a = 0 b = 1 c = 2 d = 3 In other words, it matches value to variable. So, the variables do not contain what you think they do. Do var_dump() on the vars to see what's in them. Quote Link to comment https://forums.phpfreaks.com/topic/176014-solved-multidimensional-array-what-am-i-doing-wrong/#findComment-927494 Share on other sites More sharing options...
RussellReal Posted September 30, 2009 Share Posted September 30, 2009 corbin, hes using it right.. hes echoing the variables encased in quotes.. for example: echo "$array[$a][$b]"; however, php doesn't evaluate these correctly so he gets Array[4] instead of the values.. he needs to echo them like this: echo "{$array[$a][$b]}"; or.. you know.. echo $array[$a][$b]; (^^ was my edit so people don't judge me without reading the context of my reply lol) but in terms of his use of list() it works I've tested it.. Quote Link to comment https://forums.phpfreaks.com/topic/176014-solved-multidimensional-array-what-am-i-doing-wrong/#findComment-927497 Share on other sites More sharing options...
talknerdy2mee Posted September 30, 2009 Author Share Posted September 30, 2009 echo "{$array[$a][$b]}"; works! Thank you! p.s. - This "he" is a "she", lol. p.p.s - how do I mark this as solved? (found it ) Quote Link to comment https://forums.phpfreaks.com/topic/176014-solved-multidimensional-array-what-am-i-doing-wrong/#findComment-928011 Share on other sites More sharing options...
RussellReal Posted September 30, 2009 Share Posted September 30, 2009 he she big difference I could blame it on missing a key Quote Link to comment https://forums.phpfreaks.com/topic/176014-solved-multidimensional-array-what-am-i-doing-wrong/#findComment-928040 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.