Jump to content

[SOLVED] conditionally adding to 2d array


unxposed

Recommended Posts

Hi, I'm creating a 2d array as follow, which works perfectly:

 

while ($row = mysql_fetch_assoc($result)) {
$module = array
    		(
       			'mid' => $row['mid'],
        		'top' => $row['top'],
    		);
$modules[] = $module;
}

 

But further down my script I need to add a couple of elements to the array, conditional to the mid element in the existing array. Something like:

 

while ($row = mysql_fetch_assoc($result)) {
if ($modules[][mid] == $row['mid']) {
$module = array
    		(
       			'col' => $row['col'],
        		'title_en' => $row['top'],
    		);
$modules[] = $module;
}
}

 

I'm sure this is completely wrong, but hopefully it illustrates what I'm trying to achieve.

 

Thanks in advance.

 

Link to comment
https://forums.phpfreaks.com/topic/132387-solved-conditionally-adding-to-2d-array/
Share on other sites

while ($row = mysql_fetch_assoc($result)) {
   if ($modules[][mid] == $row['mid']) {
   $module = array
          (
                'col' => $row['col'],
              'title_en' => $row['top'],
          );
   $modules[] = $module;
   }
}

 

$modules[][mid] isn't right .. you're not specifying an index for the array. Plus you need quotes for "mid"..

 

$modules[0]['mid']

 

That would be valid, but I can't guess as to what you're trying to do? Are you trying to find a match in the $modules array and then add to that particular index?

 

All I can think you're trying to do is something like:

 

while ($row = mysql_fetch_assoc($result)) {
   foreach ($modules as $key => $mod) {
      if ($mod['mid'] == $row['mid']) {
         $modules[$key]['col'] = $row['col'];
         $modules[$key]['title_en'] = $row['top'];
      }
   }
}

 

Not checked..

 

 

Adam

 

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.