
在php中处理多维数组时,经常需要检查某个特定键是否存在某个特定值,并可能需要基于此值进一步获取其他关联数据。例如,给定一个包含订单信息的数组,每个订单都是一个子数组,我们可能需要查找是否存在order_type为parent的订单,并获取该订单的order_date。
考虑以下示例数组结构:
$conversion = [
[
'order_id' => 62056,
'order_date' => '21-01',
'total' => 5.5,
'cumulative' => 0,
'order_type' => 'one_time'
],
[
'order_id' => 52937,
'order_date' => '21-02',
'total' => 5.5,
'cumulative' => 0,
'order_type' => 'one_time'
],
[
'order_id' => 45849,
'order_date' => '21-03',
'total' => 7.89,
'cumulative' => 0,
'order_type' => 'parent'
],
[
'order_id' => 228,
'order_date' => '21-10',
'total' => 5.23,
'cumulative' => 0,
'order_type' => 'parent'
]
];直接使用in_array("parent", $conversion)是无效的,因为in_array会尝试在$conversion数组的第一层元素中查找"parent",而第一层元素是完整的子数组,而不是字符串"parent"。为了解决这个问题,我们需要一种方法来“扁平化”特定键的值,然后再进行查找。
PHP提供了array_column函数,它可以从多维数组中提取出指定键的所有值,形成一个一维数组。结合array_search函数,我们就能高效地完成查找任务。
array_column($array, $column_key)函数能够从$array中的每个子数组中取出$column_key对应的值,并返回一个新的一维数组。
立即学习“PHP免费学习笔记(深入)”;
$orderTypes = array_column($conversion, 'order_type'); // $orderTypes 将是: ['one_time', 'one_time', 'parent', 'parent']
现在我们有了一个包含所有order_type值的一维数组$orderTypes,可以使用array_search($needle, $haystack)来查找'parent'是否存在,并获取其在$orderTypes中的第一个索引。
$firstParentIndex = array_search('parent', $orderTypes);如果找到了'parent',$firstParentIndex将是其在$orderTypes中的索引(例如2)。如果未找到,array_search将返回false。
结合上述两个步骤,我们可以轻松地检查是否存在order_type为parent的订单,并获取其order_date:
$orderTypes = array_column($conversion, 'order_type');
$firstParentIndex = array_search('parent', $orderTypes);
if ($firstParentIndex !== false) {
echo "找到 'parent' 类型的订单!\n";
$firstParentOrderDate = $conversion[$firstParentIndex]['order_date'];
echo "第一个 'parent' 订单的日期是: " . $firstParentOrderDate . "\n";
} else {
echo "未找到 'parent' 类型的订单。\n";
}如果我们需要获取所有order_type为parent的订单的关联数据,而不是仅仅第一个,可以使用array_keys()函数。array_keys($array, $search_value)可以返回$array中所有值为$search_value的键。
$orderTypes = array_column($conversion, 'order_type'); $parentIndices = array_keys($orderTypes, 'parent'); // $parentIndices 将是: [2, 3]
通过遍历$parentIndices数组,我们可以访问原始$conversion数组中所有匹配的订单:
$orderTypes = array_column($conversion, 'order_type');
$parentIndices = array_keys($orderTypes, 'parent');
if (!empty($parentIndices)) {
echo "找到以下 'parent' 类型的订单信息:\n";
foreach ($parentIndices as $index) {
echo "订单ID: " . $conversion[$index]['order_id'] . ", 日期: " . $conversion[$index]['order_date'] . "\n";
}
} else {
echo "未找到 'parent' 类型的订单。\n";
}以下是一个完整的示例,展示了如何使用上述方法来查找和提取数据:
<?php
$conversion = [
[
'order_id' => 62056,
'order_date' => '21-01',
'total' => 5.5,
'cumulative' => 0,
'order_type' => 'one_time'
],
[
'order_id' => 52937,
'order_date' => '21-02',
'total' => 5.5,
'cumulative' => 0,
'order_type' => 'one_time'
],
[
'order_id' => 45849,
'order_date' => '21-03',
'total' => 7.89,
'cumulative' => 0,
'order_type' => 'parent'
],
[
'order_id' => 228,
'order_date' => '21-10',
'total' => 5.23,
'cumulative' => 0,
'order_type' => 'parent'
]
];
// 场景一:检查是否存在 'parent' 类型的订单,并获取第一个的日期
echo "--- 查找第一个 'parent' 订单并获取日期 ---\n";
$orderTypes = array_column($conversion, 'order_type');
$firstParentIndex = array_search('parent', $orderTypes);
if ($firstParentIndex !== false) {
echo "找到 'parent' 类型的订单!\n";
$firstParentOrderDate = $conversion[$firstParentIndex]['order_date'];
echo "第一个 'parent' 订单的日期是: " . $firstParentOrderDate . "\n";
} else {
echo "未找到 'parent' 类型的订单。\n";
}
echo "\n--- 查找所有 'parent' 订单的ID和日期 ---\n";
// 场景二:查找所有 'parent' 类型的订单,并获取它们的ID和日期
$parentIndices = array_keys($orderTypes, 'parent');
if (!empty($parentIndices)) {
echo "找到以下 'parent' 类型的订单信息:\n";
foreach ($parentIndices as $index) {
echo "订单ID: " . $conversion[$index]['order_id'] . ", 日期: " . $conversion[$index]['order_date'] . "\n";
}
} else {
echo "未找到 'parent' 类型的订单。\n";
}
?>通过巧妙地结合使用array_column和array_search或array_keys,我们能够以简洁、高效的方式在PHP多维数组中查找特定键的特定值,并进一步提取相关的其他数据。这种方法避免了显式的手动循环,提高了代码的可读性和执行效率,是处理复杂数据结构时非常实用的技巧。理解并掌握这些函数对于编写高质量的PHP代码至关重要。
以上就是PHP 多维数组中特定键值的高效查找与关联数据提取的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号