Jump to content

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

 

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.