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.
4 Answers 4
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?
Comments
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.
Comments
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);
Comments
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'
mediacontent
width is bigger then a specific value?