提问人:Walrus 提问时间:9/20/2023 最后编辑:Walrus 更新时间:9/20/2023 访问量:61
PHP 获取多维数组中每个键出现的值
PHP Get the value of every occurrence of a key in a multidimensional array
问:
您将如何提取键的每个值,无论它在数组中的深度或深度如何。
下面是一个示例 JSON 数组。我想抓住每一个出现的“内容”,并将它们连接成一个字符串。
{
"foo": [
"content": "hi",
"inner": []
],
"bar": [
"content": "hi2",
"inner": [
{
"bat": [
"content": "hi3",
"inner": []
],
}
]
]
}
从理论上讲,这可以永远持续下去,内在的内在。我们如何获取所有内容值?
答:
3赞
GrumpyCrouton
9/20/2023
#1
我会使用 array_walk_recursive(),
因为它非常简单。
//convert JSON to an array
$data = json_decode($json_data, true);
//create an empty array to hold all of the values we are collecting
$content_values = [];
//this `function` is called for every key in the array that does not contain another array as it's value
//We also pass $content_values as a reference, so we can modify it inside the function
array_walk_recursive($data, function ($value, $key) use (&$content_values) {
if ($key === 'content') {
$content_values[] = $value;
}
});
//Lastly, implode all of the array elements into a string with the given separator
$concatenated_content = implode(', ', $content_values);
echo $concatenated_content;
输出
hi, hi2, hi3
评论
1赞
Walrus
9/20/2023
完善。。。。。。。。。。
1赞
Md Rashedul Islam
9/20/2023
#2
您必须创建一个将采用数组值的函数。它将检查每个数组元素。如果数组元素是数组,则它将再次调用自身,否则该值将存储在另一个数组中。
新数组将是预期结果。对于数组的任何维度,它都可以工作。
function extractKeyValues($data, $key)
{
$values = [];
foreach ($data as $element) {
if (is_array($element)) {
$values = array_merge($values, extractKeyValues($element, $key));
} elseif (is_string($element) && $key === 'content') {
$values[] = $element;
}
}
return $values;
}
$jsonData = '{
"foo": [
{
"content": "hi",
"inner": []
}
],
"bar": [
{
"content": "hi2",
"inner": [
{
"bat": [
{
"content": "hi3",
"inner": []
}
]
}
]
}
]
}';
$dataArray = json_decode($jsonData, true);
$contentValues = extractKeyValues($dataArray, 'content');
$result = implode(' ', $contentValues);
评论
0赞
hanshenrik
9/20/2023
我认为is_string检查是一个错误:OP 什么也没说i don't want to capture it if content is an int
0赞
Md Rashedul Islam
9/20/2023
仅供参考,您可以删除条件,也可以放置自己的条件。
2赞
ArtisticPhoenix
9/20/2023
#3
给你:
输入:
$array = [
"foo" => [
"content" => "hi",
"inner" => []
],
"bar" => [
"content" => "hi2",
"inner" => [
"bat" => [
"content" => "hi3",
"inner" => []
]
]
],
"biz" => [
"content" => "hi4",
"inner" => [
"bat" => [
"content" => "hi5",
"inner" => [
"bong" => [
"content" => "bong",
"inner" => [
"bat" => [
"content" => "bong0",
"inner" => []
]
]
],
"bong1" => [
"content" => "bong1",
"inner" => [
"bong2" => [
"content" => "bong2",
"inner" => []
]
]
]
]
]
]
]
];
递归函数
function getContent($array){
$content = [];
foreach($array as $row){
foreach($row as $key => $value){
switch($key){
case 'content':
$content[] = $value;
break;
case 'inner':
$content = array_merge($content, getContent($value)); //recurse
break;
}
}
}
return $content;
}
print_r( getContent($array) );
结果
Array
(
[0] => hi
[1] => hi2
[2] => hi3
[3] => hi4
[4] => hi5
[5] => bong
[6] => bong0
[7] => bong1
[8] => bong2
)
您可以使用“内爆”和任何您喜欢的分隔符。我更喜欢构建一个数组,然后内爆它。
PS. 有人已经拿走了 - 我认为另一个答案是不完整的。array_walk_recursive
评论
0赞
Nick
9/21/2023
好久不见!
0赞
ArtisticPhoenix
9/22/2023
@Nick - 我很忙。我的公司被一家更大的公司收购了,我也一直专注于我的艺术作品。facebook.com/profile.php?id=100090584328817
0赞
Nick
9/23/2023
恭喜你买断,工作愉快!你有一双大眼睛。
评论