0

My initial array is

$employees = array(
array('name' => 'jack',
      'area' => 'crafts'),

array('name' => 'janet',
      'area' => 'aquatics'),

array('name' => 'brad',
      'area' => 'crafts')
);

I am trying to create a new array based on the search results of another array so the new array should look like this if I search for 'crafts':

$employees2 = array(
array('name' => 'jack',
      'area' => 'crafts'),

array('name' => 'brad',
      'area' => 'crafts')
);

What is the simplest solution I can do get get this new result.

7

5 Answers 5

1
foreach($employees as $key => $value){

    if($value['area']=='crafts'){
        $employees2[] = $value;
    }

}

This quite simply loops through the first array and checks the value of "area" in the internal array. If the value is equal to "crafts" you can then put that into a new array which is called $employees2. You can change crafts to whatever you want and add anything you want between the [ ] in employees2 if you wish to customise the key.

Sign up to request clarification or add additional context in comments.

4 Comments

I have tried this method and I get an error "employees2 undefined."
Ha! I got this to work. It usually helps if I move the include link above this code otherwise the script doesn't know where to find the data. How silly of me. Thank you very much for your help. You saved my terrible day.
@user2108555 If you found the answer to be correct, could you please mark it as the correct answer. Thank you.
It won't let me because it says I don't have enough "Reputation". Sorry
0

Try this:

$employees = array(
array('name' => 'jack',
  'area' => 'crafts'),

array('name' => 'janet',
  'area' => 'aquatics'),

array('name' => 'brad',
  'area' => 'crafts')
);

$employees2 = array();

foreach ($employees as $key) {
if($key['name'] == "jack")
{
    array_push($employees2,array('name'=>$key['name'],'area'=>$key['area']));
}
}

var_dump($employees2);

The array_push do all the trick ;)

Saludos.

2 Comments

I have tried this method and I get an error "employees2 undefined variable".
I run the code and gives me no error....its var_dump the array fine, and $employees2 is defined in this line $employees2 = array();
0

You could simplify the syntax (but not the algorythmic complexity) by using a utility-belt library Underscore.php (http://brianhaveri.github.com/Underscore.php/)

There's a number of array-"plucking" methods that saves you the need to write loops, but under the bonnet it does much of the same as decribed in answers above.

Comments

0

I will assume that the possible result set can be large. In which case you would want to process the array with as little extra memory as possible. For this I suggest iterating through the array by reference and unsetting the items that do not match your criteria. Possibly less overhead than creating a new array to store the items that match your filter. Then you can check if the array is empty or not to determine if the filter returns any results. Like so:

<?php
// maybe this will be set through an option from the UI
$area_filter = 'crafts';

// fetched results
$employees = array(
    array('name' => 'jack',
          'area' => 'crafts'),

    array('name' => 'janet',
          'area' => 'aquatics'),

    array('name' => 'brad',
          'area' => 'crafts')
);

// filter out the items that match your filter
foreach($employees as $i => &$employee){
    if($employee['area'] != $area_filter){
        unset($employees[$i]);
    }
}

// do something with the results
if(!empty($employees)){
    print_r($employees);
} else {
    echo "Sorry, your filter '$area_filter' did not match any results\n";
}
?>

Comments

0

Try this :

$employees = array(
array('name' => 'jack',
  'area' => 'crafts'),
array('name' => 'janet',
  'area' => 'aquatics'),
array('name' => 'brad',
  'area' => 'crafts')
);

$employees       = array_filter($employees, function($employee) {
   return ($employee['area'] == 'crafts' );
});
echo "<pre>";
print_r($employees);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.