Multi-dimentional array - how to?
Posted: Mon Aug 09, 2010 7:06 pm
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:
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:
But I need to add the values inside a loop, like this:
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:
It only seems to accept a fixed string or integer as the key, like this:
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:
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
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>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,
);Code: Select all
while($row=0; $row<=10; $row++) {
$sm_array[$row] = array( "col_".$row => $td->innertext);
}Code: Select all
while($row=0; $row<=10; $row++) {
$sm_array[$row] = array( "<?php echo $row; ?>" => $td->innertext);
}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);
}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
}Thanks in advance
Grant