[英]Filter a multidimensional array by key in PHP
I have a question about filtering a multidimensional array in PHP. 我有一个关于在PHP中过滤多维数组的问题。 What i would like to achieve is to pull a specific array element (the value of mediacontent) from an existing array.
我想要实现的是从现有数组中提取特定的数组元素(mediacontent的值)。 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. 我对PHP很新,希望你们能指出我正确的方向并告诉我这里做错了什么。
I did research on the following (alternate)websites but i couldn't find an answer fitting my needs. 我对以下(替代)网站进行了研究,但我找不到符合我需求的答案。
understanding recursion , PHP filter_array 理解递归 , PHP filter_array
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. 对于您的具体情况,我建议您拥有一个对象数组,因为元素0和1是相似的,并且将mediacontent作为内部变量。
If your really want to do it you can get an array of mediacontent only by using a map 如果你真的想要这样做,你只能通过使用地图获得一系列的mediacontent
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? 如果你没有越过它,网站上还有另一个类似的帖子如何在PHP数组中递归运行array_filter?
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. 迭代器是PHP的一个被低估的功能,虽然它们有时会有一个棘手的语法,在这种情况下,你对节点而不是叶子感兴趣,需要额外的模式参数。 The recursive iterators however do simplify a lot of recursive operations.
然而,递归迭代器确实简化了许多递归操作。
Your can try this way also for just mediacontent.... 你可以尝试这种方式也只是为了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);
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' 使用“==”检查查找是否相等而不管类型如何,所以当它遇到一个整数时,它会将两个值都转换为interger int('anystring')得到0,所以键0 = string'mediacontent'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.