Jump to content

[SOLVED] Multidimensional Array - what am I doing wrong?


Recommended Posts

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??

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

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]

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 :P

 

if you're judging off of the echos maybe surround the $whatever[$x][$r] with curly braces { }

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.

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

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.