3

I have a question about filtering a multidimensional array in PHP. What i would like to achieve is to pull a specific array element (the value of mediacontent) from an existing array. This is what i got so far:

Existing multidimensional array:

$multidimensional_array = array(
 'entry' => array(
 0 => array(
 'width' => array(
 '$t' => '1536'
 ),
 'height' => array(
 '$t' => '2048'
 ),
 'id' => array(
 '$t' => '878974'
 ),
 'mediagroup' => array(
 'mediacontent' => array(
 'url' => 'http://website/urltotheobject.png',
 'width' => 384,
 'medium' => 'image',
 'type' => 'image/jpeg',
 'height' => 512
 )
 )
 ),
 1 => array(
 'width' => array(
 '$t' => '5486'
 ),
 'height' => array(
 '$t' => '1144'
 ),
 'id' => array(
 '$t' => '485435'
 ),
 'mediagroup' => array(
 'mediacontent' => array(
 'url' => 'http://website/urltotheobject.png',
 'width' => 512,
 'medium' => 'image',
 'type' => 'image/jpeg',
 'height' => 384
 )
 ) 
 )
 )
);

A (not properly working) function to filter the array

function filterResponseByKey($keyToFilter,$array){
 foreach ($array as $key => $value) 
 {
 if($key == $keyToFilter)
 {
 return $value;
 }
 elseif(is_array($value))
 {
 $result = filterResponseByKey($keyToFilter, $value);
 if($result != false)
 {
 return $result; 
 }
 }
 }
 return false;
}

I'm fairly new to PHP and hope you guys could point me in the right direction and tell me what i am doing wrong here.

I did research on the following (alternate)websites but i couldn't find an answer fitting my needs.

understanding recursion , PHP filter_array

mickmackusa
49.1k13 gold badges97 silver badges163 bronze badges
asked Dec 23, 2014 at 15:25
4
  • 1
    Do not need recurion, just simple loop through. So you want to clear those, where, for example mediacontent width is bigger then a specific value? Commented Dec 23, 2014 at 15:33
  • True, don't use recursion and if you just want a few data from the original array, just build a new one and return with that. Commented Dec 23, 2014 at 15:34
  • I would actually use objects and a class approach but if you really want recursion check my answer Commented Dec 23, 2014 at 15:44
  • This question is unclear because it doesn't include a minimal reproducible example. We don't know if you want the first found match or all found matches. We don't know if recursion is actually necessary. This page is not a good fit for Stack Overflow. Commented Sep 11, 2022 at 20:24

4 Answers 4

1

For your specific case I would reccomend you to have an array of objects, as elements 0 and 1 are similar, and have mediacontent as a internal variable.

If your really want to do it you can get an array of mediacontent only by using a map

 array_map($multidimensional_array['entry'],function($obj){
 return $obj['mediagroup']['mediacontent'];
 });

If what you want is something more dynamic and recursive

 function recursive_filter_by_key($keyname,$list){
 $result=[];
 foreach($list as $key=>$obj){
 if($key==$keyname) 
 array_push($result,$obj);
 else if(gettype($obj)=='array')//not necesary to check if its equal to the $key as this wouldn't run if it was
 $result = array_merge($result,recursive_filter_by_key($obj));
 }
 return $result;
 }

This function can return complete arrays within its results array as long as they are the value of the key you are searching

I would like you to comment though as I think I haven't understood your question very much.

Also there is another similar post in the site in case you haven't crossed it How to run array_filter recursively in a PHP array?

answered Dec 23, 2014 at 15:42
Sign up to request clarification or add additional context in comments.

Comments

1

The most practical solution for this uses iterators:

$iter = new RecursiveIteratorIterator(
 new RecursiveArrayIterator($multidimensional_array),
 RecursiveIteratorIterator::SELF_FIRST
);
foreach($iter as $key => $value) {
 if(ctype_alpha($key) && $key == 'mediacontent') {
 echo "Media Content: ".print_r($value, true)."\n";
 }
}

This will output:

Media Content: Array
(
 [url] => http://website/urltotheobject.png
 [width] => 384
 [medium] => image
 [type] => image/jpeg
 [height] => 512
)
Media Content: Array
(
 [url] => http://website/urltotheobject.png
 [width] => 512
 [medium] => image
 [type] => image/jpeg
 [height] => 384
)

Iterators are a much underappreciated feature of PHP, although they do have a tricky syntax at times, and in this case you're interested in nodes instead of leaves, requiring the extra mode parameter. The recursive iterators however do simplify a lot of recursive operations.

answered Dec 23, 2014 at 15:48

Comments

0

Your can try this way also for just mediacontent....

$multidimensional_array = array(
 'entry' => array(
 0 => array(
 'width' => array(
 '$t' => '1536'
 ),
 'height' => array(
 '$t' => '2048'
 ),
 'id' => array(
 '$t' => '878974'
 ),
 'mediagroup' => array(
 'mediacontent' => array(
 'url' => 'http://website/urltotheobject.png',
 'width' => 384,
 'medium' => 'image',
 'type' => 'image/jpeg',
 'height' => 512
 )
 )
 ),
 1 => array(
 'width' => array(
 '$t' => '5486'
 ),
 'height' => array(
 '$t' => '1144'
 ),
 'id' => array(
 '$t' => '485435'
 ),
 'mediagroup' => array(
 'mediacontent' => array(
 'url' => 'http://website/urltotheobject.png',
 'width' => 512,
 'medium' => 'image',
 'type' => 'image/jpeg',
 'height' => 384
 )
 )
 )
 )
 );
 function getMediaContent($item){
 return $item['mediagroup']['mediacontent'];
 }
 $mediacontent = array_map('getMediaContent', $multidimensional_array['entry']);
 print '<pre>';
 print_r($mediacontent);
answered Dec 23, 2014 at 15:46

Comments

0
function filterResponseByKey($keyToFilter,$array){
 foreach ($array as $key => $value) 
 {
 if($key === $keyToFilter)
 {
 return $value;
 }
 elseif(is_array($value))
 {
 $result = filterResponseByKey($keyToFilter, $value);
 if($result != false)
 {
 return $result; 
 }
 }
 }
 return false;
}

Now working!

edit: to explain:

Using "==" checks to find if equal regardless of type, so when it encounters a interger, it will cast both values to check as a interger int('anystring') results in 0 , so the key 0 = string 'mediacontent'

answered Dec 23, 2014 at 16:37

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.