Omzy Posted October 1, 2010 Share Posted October 1, 2010 public function getEventTypes() { return array( 1=>array('Exhibition', 1), 2=>array('Wedding', 1), 3=>array('Business', 1), 4=>array('School Trip', 0), 5=>array('Birthday', 0), 6=>array('Concert', 1), 7=>array('Sporting Event', 1), 8=>array('Training', 0), 9=>array('Conference', 1), 10=>array('Other', 1), ); } I now want to generate a new array from this array as follows: 1) must retain the existing key 2) must be single dimensional and the value of each element will be value[0] 3) must only contain the array elements where value[1] = 1 So for example the generated array should be as follows: public function getActiveEventTypes() { return array( 1=>'Exhibition', 2=>'Wedding', 3=>'Business', 6=>'Concert', 7=>'Sporting Event', 9=>'Conference', 10=>'Other', ); } Link to comment https://forums.phpfreaks.com/topic/214939-generate-a-new-array-from-another-array/ Share on other sites More sharing options...
kenrbnsn Posted October 1, 2010 Share Posted October 1, 2010 Something like this would work: <?php function af($ary) { $na = array(); if (!is_array($ary)) { return (false); } foreach($ary as $k=>$e) { if ($e[1]) { $na[$k] = $e[0]; } } return($na); } $orig = array( 1=>array('Exhibition', 1), 2=>array('Wedding', 1), 3=>array('Business', 1), 4=>array('School Trip', 0), 5=>array('Birthday', 0), 6=>array('Concert', 1), 7=>array('Sporting Event', 1), 8=>array('Training', 0), 9=>array('Conference', 1), 10=>array('Other', 1), ); $new = af($orig); print_r($new); ?> Ken Link to comment https://forums.phpfreaks.com/topic/214939-generate-a-new-array-from-another-array/#findComment-1118115 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.