unxposed Posted November 12, 2008 Share Posted November 12, 2008 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 More sharing options...
Adam Posted November 12, 2008 Share Posted November 12, 2008 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 Link to comment https://forums.phpfreaks.com/topic/132387-solved-conditionally-adding-to-2d-array/#findComment-688298 Share on other sites More sharing options...
unxposed Posted November 12, 2008 Author Share Posted November 12, 2008 Thank you so much, spot on, this works beautifully! Really appreciated! I knew I was going about it completely the wrong way. Thanks gain. Link to comment https://forums.phpfreaks.com/topic/132387-solved-conditionally-adding-to-2d-array/#findComment-688303 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.