1

I'm trying to do the following:

  1. Grab key/value pairs from an array $post_data...

  2. Only where the key matches a provided list $my_fields...

  3. And create a new array with only the matched data.

For example, from $post_data I'd like to grab the key/value pairs for first_name, last_name, and title while ignoring user_email. I'd then like to create a new array named $clean_data with these key/value pairs.

Below is my failed attempt at looping through the $post_data array and pulling out the matches based on the $my_fields array.

// These are the fields I'd like to get from the $post_data array
$my_fields = array(
    'first_name',
    'last_name', 
    'title'
); 

// This is the raw data. I do not need the 'user_email' key/value pair.
$post_data = array(
    'first_name' => 'foo',
    'last_name'  => 'bar',
    'title'      => 'Doctor',
    'user_email' => '[email protected]'
);

$clean_data = array();

$counter == 0;
foreach ($post_data as $key => $value) 
{
    if (array_key_exists($my_fields[$counter], $post_data)) 
    {
        $clean_data[$key] = $value;
    }
    $counter++;
}

// Incorrectly returns the following: (Missing the first_name field) 
// Array
// (
//     [last_name] => bar
//     [title] => Doctor
// )

3 Answers 3

4

No looping needed - you can have it all done in one line if you want. Here is the magic function:

And if you don't want to modify your $my_fields array you can use array_flip()

And for further reading all other fun you can have with arrays.

Now that MARKY chose the answer, here is the example how it could be done by differently:

$my_fields = array(
    'first_name',
    'last_name', 
    'title'
); 

$post_data = array(
    'first_name' => 'foo',
    'last_name'  => 'bar',
    'title'      => 'Doctor',
    'user_email' => '[email protected]'
);

$clean_data = array_intersect_key($post_data, array_flip($my_fields));

this produces

array (
    'first_name' => 'foo',
    'last_name'  => 'bar',
    'title'      => 'Doctor',
)  
Sign up to request clarification or add additional context in comments.

8 Comments

did you try running your code, does it give the desired result?
You modified the $my_fields array itself to fit your answer. It was a numerically indexed array.
Thanks for your response. I just tried your method but all I'm getting is an empty $clean_data array. I'll reread the documentation to try and figure out what's going wrong. Thanks again.
@Konstantin: Do it more carefully. First of all you are modifying the $my_fields array itself. That's not a solution. May be in this case this array is hard-coded, but in actual scenario it may be coming as input from somewhere. So you can't change it's structure and the problem statement for that matter. Moreover, even after running your modified answer, result is not what is required.
if you really can't modify $my_fields there is array_flip() i just run this code and got array ( 0 => 'foo', 1 => 'bar', 2 => 'Doctor', )
|
-1

You should use this.

foreach($post_data as $key=>$value){
    if(in_array($key,$my_fields)){
    $clean_data[$key]=$value;
    }
}
print_r($clean_data);

You are trying in the right direction, just the matching of key in the array has to be in a different way.

1 Comment

Horrible answer, because it loops through $post_data instead of $my_fields. Consider the case where $my_fields has length 2, and $post_data length 200,000 - it runs nearly 100,000 times slower than necessary!
-1

you can replace it with your foreach section no counter needed

foreach ($post_data as $key => $value) 
{
    if (in_array($key,$my_fields)) 
    {
        $clean_data[$key] = $value;
    }
}

3 Comments

just go through the already submitted answers before posting yours. Though your answer is correct, but I have already submitted the same answer already.
sorry !! I see it after submit
Horrible answer, because it loops through $post_data instead of $my_fields. Consider the case where $my_fields has length 2, and $post_data length 200,000 - it runs nearly 100,000 times slower than necessary!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.