根据属性值有效地查找嵌套的PHP数组元素 [英] Efficiently find nested PHP array element based on attribute value

查看:83
本文介绍了根据属性值有效地查找嵌套的PHP数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设一个PHP数组在转换为JSON时具有以下格式:

Suppose a PHP array, when cast to JSON, has the following format:

[{
    "key": "width",
    "value": "1200",
    "label": "Width (mm)",
    "choice": ""
},
{
    "key": "height",
    "value": "900",
    "label": "Height (mm)",
    "choice": ""
},
{
    "key": "material",
    "value": "paper",
    "label": "Material",
    "choice": "Paper"
}]

(这是一个

让我们假设我想有效地找到所使用的材料。换句话说,我想搜索一个嵌套数组,该数组的 key material ,并且我想返回,该值将是

Let's suppose I want to efficiently find what material is used. In other words, I want to search for a nested array that has for key the value material, and I want to return the value which would be paper.

I知道这可以通过使用foreach / while循环来完成,但是PHP丰富了我不太熟悉的编译数组函数。在这里使用的最佳功能是什么?

I know this can be done by using a foreach/while loop, but PHP is rich with compiled array functions that I'm not very familiar with. What's the best function to use here?

更新:到目前为止,我已经尝试过了

到目前为止,我尝试过两件事:

Here's two things I've tried so far:

尝试#1:

$json = '[{"key":"width","value":"1200","label":"Width (mm)","choice":""},{"key":"height","value":"900","label":"Height (mm)","choice":""},{"key":"material","value":"paper","label":"Material","choice":"Paper"}]';
$array = json_encode($json, true);
$material = '';
foreach($array as $nestedArray) {
  if($nestedArray['key'] = 'material') {
    $material = $nestedArray['value'];
  }
}

尝试#2:

$json = '[{"key":"width","value":"1200","label":"Width (mm)","choice":""},{"key":"height","value":"900","label":"Height (mm)","choice":""},{"key":"material","value":"paper","label":"Material","choice":"Paper"}]';
$array = json_decode($json, true);
$filteredArray = array_filter($array, function($array) {
    return ($array['key'] == 'material');
});
$arr = array_pop($filteredArray)['value'];






两者都产生正确的值,但#1是混乱,并且#2可能不是PHP数组函数的最佳使用。


Both produce the right value, but #1 is messy, and #2 may not be the best use of PHPs array functions.

推荐答案

这取决于您要除了找到价值之外还可以做

array_filter 很简单,但是它将遍历整个数组。

array_filter is simple, but it will loop through the whole array.

array_search 看起来更快,但是它需要复制源数组,因此实际上是 array_filter 慢(不是很多)。

array_search on a reduced set looks faster, but it needs to make a copy of the source array, so it's actually slower than array_filter (not by much).

foreach <您首先尝试过的/ code>解决方案不会创建额外的数组,它使您无法进行查找:

The foreach solution you tried first will not create extra arrays and it allows you to break on a find:

foreach($array as $nestedArray) {
    if ($nestedArray['key'] == 'material') {
        $material = $nestedArray['value'];
        break; // <--- found!
    }
}

所以在短数组上,我会接受使用 array_column 的解决方案,或者如果您确定材料在那里,则可以进行以下 array_column 调整:

So on short arrays I'd go with the accepted solution using array_column, or if you're sure that the material is there, there is this array_column tweak:

// Transform the records into keypairs
$keypairs = array_column($records, 'value', 'key');

现在的密钥对是[width => 900,material => paper,...],所以:

Now keypairs is [ width => 900, material => paper, ... ], so:

$material = $keypairs['material'];

我要添加 array_key_exists 确定。这样可以节省 array_search (不是一个很大的优势,但是您可能会使用keypair对象)。

I'd add a array_key_exists just to be sure. This saves the array_search (not that great an advantage, but you might have a use for the keypair object).

如果您只需要一个值而不需要其他任何内容,那么性能就非常重要,并且数组很大,那么我不会放弃在JSON内将' material:'作为字符串使用 strpos ,即使它是代码气味。

If you need exactly that one value and nothing else, performance is at a premium, and the array is large, I'd not throw out the idea of looking for '"material":"' inside the JSON as a string with strpos, even if it's a code smell.

这篇关于根据属性值有效地查找嵌套的PHP数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆