这是名为multi
的多维嵌套数组: -
Array
(
[0] => Array
(
[id] => 4d4b7104d754a06370d81259
[name] => Arts & Entertainment
[pluralName] => Arts & Entertainment
[shortName] => Arts & Entertainment
[icon] => Array
(
[prefix] => https://ss3.4sqi.net/img/categories_v2/arts_entertainment/default_
[suffix] => .png
)
[categories] => Array
(
[0] => Array
(
[id] => 56aa371be4b08b9a8d5734db
[name] => Amphitheater
[pluralName] => Amphitheaters
[shortName] => Amphitheater
[icon] => Array
(
[prefix] => https://ss3.4sqi.net/img/categories_v2/arts_entertainment/default_
[suffix] => .png
)
[categories] => Array
(
[0] => Array
(
[id] => 4bf58dd8d48988d1f8931735
[name] => Bed & Breakfast
[pluralName] => Bed & Breakfasts
[shortName] => B & B
[icon] => Array
(
[prefix] => https://ss3.4sqi.net/img/categories_v2/travel/bedandbreakfast_
[suffix] => .png
)
[categories] => Array
(
)
)
[1] => Array
(
[id] => 4f4530a74b9074f6e4fb0100
[name] => Boarding House
[pluralName] => Boarding Houses
[shortName] => Boarding
[icon] => Array
(
[prefix] => https://ss3.4sqi.net/img/categories_v2/travel/hotel_
[suffix] => .png
)
[categories] => Array
(
)
)
)
)
[1] => Array
(
[id] => 4fceea171983d5d06c3e9823
[name] => Aquarium
[pluralName] => Aquariums
[shortName] => Aquarium
[icon] => Array
(
[prefix] => https://ss3.4sqi.net/img/categories_v2/arts_entertainment/aquarium_
[suffix] => .png
)
[categories] => Array
(
)
)
)
[1] => Array
(
[id] => 4d4b7105d754a06372d81259
[name] => College & University
[pluralName] => Colleges & Universities
[shortName] => College & Education
[icon] => Array
(
[prefix] => https://ss3.4sqi.net/img/categories_v2/education/default_
[suffix] => .png
)
[categories] => Array
(
[0] => Array
(
[id] => 4bf58dd8d48988d198941735
[name] => College Academic Building
[pluralName] => College Academic Buildings
[shortName] => Academic Building
[icon] => Array
(
[prefix] => https://ss3.4sqi.net/img/categories_v2/education/academicbuilding_
[suffix] => .png
)
[categories] => Array
(
[0] => Array
(
[id] => 4bf58dd8d48988d199941735
[name] => College Arts Building
[pluralName] => College Arts Buildings
[shortName] => Arts
[icon] => Array
(
[prefix] => https://ss3.4sqi.net/img/categories_v2/arts_entertainment/default_
[suffix] => .png
)
[categories] => Array
(
)
)
)
)
[1] => Array
(
[id] => 4bf58dd8d48988d197941735
[name] => College Administrative Building
[pluralName] => College Administrative Buildings
[shortName] => Administrative Building
[icon] => Array
(
[prefix] => https://ss3.4sqi.net/img/categories_v2/education/administrativebuilding_
[suffix] => .png
)
[categories] => Array
(
)
)
)
)
)
这是名为single
的单维数组: -
array(
[0] => 'Aquarium',
[1] => 'Amphitheater'
)
我想创建一个函数,它会递归迭代multidimesional数组,当single
的值匹配" name" multi
的键,将形成一个新数组,其中包含" id"和"名称"每个节点与single
匹配。
我该怎么做?
以下是我尝试做的事情,但不知何故无法弄清楚如何进行递归调用: -
public static function array_flatten($fs_array, $cat_array) {
if (!is_array($fs_array)) {
return FALSE;
}
$result = array();
foreach ($fs_array as $key => $value)
{
if(in_array($value['name'], $cat_array))
$result[] = $value['id'];
}
return $result;
}
答案 0 :(得分:1)
我根据你的例子构建了一个解决方案。我没有测试它,但背后的想法是:
检查是否存在一个“类别”键,它是一个数组,如果有,则采用该子数组并再次将其传递给flatten_array。之后,递归调用的返回值将合并到$ result数组中。
public static function array_flatten($fs_array, $cat_array) {
if (!is_array($fs_array)) {
return FALSE;
}
$result = array();
foreach ($fs_array as $key => $value)
{
if(in_array($value['name'], $cat_array)) {
$result[] = $value['id'];
}
// ----- here starts what I added -----------
if (is_array($value['categories'])) {
$result = array_merge($result, self::array_flatten($value['categories'], $cat_array));
}
// ----- here ends what I added -----------
}
return $result;
}