1

I have an array similar to this below. I need to find a way to extract values by using a key/value pair for instance by using 'parent_id' => 3. I want to get all values for said array ( id = 2, label = Content Creation, link_url = '', parent_id = 3 ).

I've tried using array_intersect() without any success.

Thank You for your assistance.

Array ( 
    [0] => Array ( [id] => 1 [label] => Web Development [link_url] => [parent_id] => 1 ) 
    [1] => Array ( [id] => 2 [label] => Content Creation [link_url] => [parent_id] => 3 ) 
    [2] => Array ( [id] => 3 [label] => PHP Jobs [link_url] => /simple_link.php [parent_id] => 1 ) 
    [3] => Array ( [id] => 4 [label] => OSCommerce projects [link_url] => /another_link.php [parent_id] => 4 )
)
1
  • the manual page for array_search has some functions that would work Commented Feb 23, 2014 at 20:35

2 Answers 2

2

I thik you can loop in your array and match your desidered parent_id with an if condition

foreach($array as $data)
{
    if($data['parent_id'] == '3')
    {
        echo $data['id'] . ' ' . $data['label'] . ' ' . $data['link_url'];
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can also use array_filter for these sort of problems

$matching_results = array_filter($data, function($item) {
   return ($item['parent_id'] == 3);
});

It will cycle through the data and $matching_results will be an array of each $item that returns true

1 Comment

Yes, this works as well, think it depends on final application, THX

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.