Jump to content

Multi-dimentional array - how to?


grantp22

Recommended Posts

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:

 

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

 

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:

 

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:

 

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:

 

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:

 

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

Link to comment
https://forums.phpfreaks.com/topic/210270-multi-dimentional-array-how-to/
Share on other sites

Thanks for the suggestion DavidAM,

 

You would expect that 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!

 

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

  • 2 weeks later...

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.