Jump to content

Generate a new array from another array


Omzy

Recommended Posts

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
Share on other sites

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
Share on other sites

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.