ultimatemonty Posted November 8, 2006 Share Posted November 8, 2006 I've got two arrays that are being compared in a function. The function is supposed to loop through all the values one array, looking for matches in another array. If it finds a match, it sets an element in the third array to 1. If it finds no matches, it sets the element to 0.Here's the code.[code]function doClassTakenList($classes,$courses){ $i=0; $count = count($classes); do { $class = $classes[$i]; if(array_search($class,$courses)) { $list[$class][0] = '1'; } else { $list[$class][0] = '0'; } $i++; } while($i<$count); return($list);} [/code]$classes is a one-dimensional, numerically indexed array containing a list of all classes for a program. IE:[code]$classes = array('SPEC 3110','SPEC3120','SPEC3130','SPEC3140','INTP4010');[/code]$courses is a one-dimensional, numerically indexed array containing a list of classes a student has taken. IE:[code]$courses = array('SPEC 3110','SPEC3180','INTP4010');[/code]$list is an array containing each class and a '1' (for taken) or a '0' (for not taken).instead of returning a numerically indexed, multi-dimensional array (which makes it a real PITA to access the classnames in the $list array), I'd like to build the array something like:[code]if(array_search($class,$courses)) { $list[$class => '1']; } else { $list[$class => '0'] } [/code]this way it's easier to get the class and the associated value.using that particular example, I get a [b]Parse error: syntax error, unexpected T_DOUBLE_ARROW, expecting ']'[/b]anyone have any ideas of how to build what I'm needing? Link to comment https://forums.phpfreaks.com/topic/26595-solvedbuilding-an-associative-array-by-comparing-2-arrays/ Share on other sites More sharing options...
ultimatemonty Posted November 8, 2006 Author Share Posted November 8, 2006 SOLVED!solution was very simple once I cleared my head and tackled it again...[code]function doClassTakenList($classes,$courses){ $i=0; $count = count($classes); do { $class = $classes[$i]; if(array_search($class,$courses)) { $list[$class] = '1'; } else { $list[$class] = '0'; } $i++; } while($i<$count); return($list);}[/code] Link to comment https://forums.phpfreaks.com/topic/26595-solvedbuilding-an-associative-array-by-comparing-2-arrays/#findComment-121641 Share on other sites More sharing options...
Psycho Posted November 8, 2006 Share Posted November 8, 2006 Here's a much simpler method[code]<?phpfunction doClassTakenList($classes,$courses){ foreach ($classes as $class) { $list[$class] = (in_array($class,$courses))?'1':'0'; } return $list;}[/code]However, I followed your example of setting the value as the string of 0 or 1. It would be better to set the values as true/false so that you can do a true false tests on it. This is what I would use:[code]<?phpfunction doClassTakenList($classes,$courses){ foreach ($classes as $class) { $list[$class] = in_array($class,$courses); } return $list;}[/code] Link to comment https://forums.phpfreaks.com/topic/26595-solvedbuilding-an-associative-array-by-comparing-2-arrays/#findComment-121645 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.