Page 1 of 1

Multi-dimentional array - how to?

Posted: Mon Aug 09, 2010 7:06 pm
by grantp22
Hi can anybody help me out with the following problem I am having? I need to populate the key values in the multi-dimentional array structure shown below and the values will be used later in the following way:

Code: Select all

<tr>
  <td><?php echo $sm_array[1]['col_1']; ?></td>
  <td><?php echo $sm_array[1]['col_2']; ?></td>
  <td><?php echo $sm_array[1]['col_3']; ?></td>
  <td><?php echo $sm_array[1]['col_4']; ?></td>
  //and so on, up to 10
</tr>
<tr>
  <td><?php echo $sm_array[2]['col_1']; ?></td>
  <td><?php echo $sm_array[2]['col_2']; ?></td>
  <td><?php echo $sm_array[2]['col_3']; ?></td>
  <td><?php echo $sm_array[2]['col_4']; ?></td>
  //and so on, up to 10
</tr>
This is the array structure, $td->innertext is just a different value each time and is not important for this example, it could essentially be anything:

Code: Select all

sm_array[$row] = array( "col_1" => $td->innertext,
                        "col_2" => $td->innertext,
                        "col_3" => $td->innertext,
                        "col_4" => $td->innertext,
                        "col_5" => $td->innertext,
                        "col_6" => $td->innertext,
                        "col_7" => $td->innertext,
                        "col_8" => $td->innertext,
                        "col_9" => $td->innertext,
                        "col_10" => $td->innertext,
);
But I need to add the values inside a loop, like this:

Code: Select all

while($row=0; $row<=10; $row++) {
  $sm_array[$row] = array( "col_".$row => $td->innertext);
}
But this doesn't allow me to change the key name inside the loop, so for sm_array[1] there will be 10 keys and their values, and the same for sm_array[2], it will also have 10 keys and different values for each key and so on! But for some reason the key value will not work this way inside a loop, it doesn't allow "col_".$row and I even tried to echo the key name, like this:

Code: Select all

while($row=0; $row<=10; $row++) {
  $sm_array[$row] = array( "<?php echo $row; ?>" => $td->innertext);
}
It only seems to accept a fixed string or integer as the key, like this:

Code: Select all

while($row=0; $row<=10; $row++) {
  $sm_array[$row] = array( "col_1" => $td->innertext);
  // or like this
  $sm_array[$row] = array( 1 => $td->innertext);
}
But then I have a problem, I can't change the key name dynamically! I have tried doing this with conditional staments like this, but it only accepts the first key name and the 2nd, 3rd etc, etc all fail:

Code: Select all

while($row=0; $row<=10; $row++) {
  if($row==1){
     $sm_array[$row] = array( "col_1" => $td->innertext);
  }else if($row==2){
     $sm_array[$row] = array( "col_2" => $td->innertext);
  } //and so on
}
Can somebody please tell me what I am doing wrong or can't this be done or is their some other way of doing this?

Thanks in advance
Grant

Re: Multi-dimentional array - how to?

Posted: Mon Aug 09, 2010 11:22 pm
by axcrow
i dont quite understood you, but do you need something like this?

Code: Select all

for($row=0; $row<=10; $row++) {
    for($col=0; $col<=10; $col++) {
        $sm_array[$row]["col_".$col] = 'something';
    }
}
this outputs

Code: Select all

Array
(
    [0] => Array
        (
            [col_0] => something
            [col_1] => something
            [col_2] => something
            [col_3] => something
            [col_4] => something
            [col_5] => something
            [col_6] => something
            [col_7] => something
            [col_8] => something
            [col_9] => something
            [col_10] => something
        )

    [1] => Array
        (
            [col_0] => something
            [col_1] => something
            [col_2] => something
            [col_3] => something
            [col_4] => something
            [col_5] => something
            [col_6] => something
            [col_7] => something
            [col_8] => something
            [col_9] => something
            [col_10] => something
        )
...and etc

Re: Multi-dimentional array - how to?

Posted: Tue Aug 10, 2010 3:16 am
by grantp22
Thanks for the reply axcrow,

That is exactly what I have been trying to do, sorry, my previous post was just a little incorrect, the loop should have been for $col and not $row! The column loop is inside the row loop. You would expect your code to work, right, but it doesn't! Below is a variation of your solution, and it too doesn't work, nothing gets echoed, just a blank screen.

Code: Select all

<?php

$n = array(1 => "aa", 2 => "ab", 3 => "ac", 4 => "ad", 5 => "ae", 6 => "af", 7 => "ag", 8 => "ah", 9 => "aj", 10 => "ak");

$row = 1;
$col = 1;

while($row<=10) {
  while($col<=10) {
    $key = 'col_' . $col;
    $sm_array[$row][$key] = $n[$col];
    $col++;
  }
  $row++;
}

echo sm_array[1]['col_1'];

?>
Does anybody have any ideas why this is not working?

Re: Multi-dimentional array - how to?

Posted: Tue Aug 10, 2010 7:01 am
by axcrow
echo sm_array[1]['col_1'];
should be
echo $sm_array[1]['col_1'];

Re: Multi-dimentional array - how to?

Posted: Tue Aug 10, 2010 12:14 pm
by grantp22
Awesome! Thanks axcrow, how stupid of me!