数组内爆...同时忽略嵌套数组?

发布于 2024-10-03 02:37:30 字数 440 浏览 4 评论 0原文

我环顾四周,发现很多人询问如何使用嵌套数组来内爆数组。然而,这些人通常也希望包含嵌套数组。我不想包含嵌套数组...我想扔掉嵌套数组...

这是我的数组:

[tag] => Array
(
    [0] => one
    [1] => two
    [0_attr] => Array
        (
            [category] => three
            [lock] => four
        )

    [2] => five
)

如果我内爆此数组(以逗号分隔),我希望结果为:

one, two, five

注意三和四是怎样的不包括在内。因为它们是嵌套数组,所以我不想要它。我只想要即时值。我究竟该如何完成这件事?

I have looked around and I see a lot of people asking how to implode arrays with nested arrays. However, these people usually want to include the nested array as well. I do not want to include the nested array... I want to throw out the nested array...

This is my array:

[tag] => Array
(
    [0] => one
    [1] => two
    [0_attr] => Array
        (
            [category] => three
            [lock] => four
        )

    [2] => five
)

If I implode this array, comma delimited, I want the result to be:

one, two, five

Notice how three and four are NOT included. Since they are a nested array, I don't want it. I only want immediate values. How exactly would I get this done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魄砕の薆 2024-10-10 02:37:30

您需要迭代 $tag 中的所有值并过滤掉那些 is array
比如

$tags = array();
foreach ($tag as $index=>$value)
{
  if (!is_array($value))
  {
     $tags[$index] = $value;
  }
}
implode(',', $tags);

我发现上面的内容有点乏味,
这是改进版本的

$arr = array(0 => "one", 1 => "two", 2 => array(1,2,3), 3=>4, 4=>new stdClass);
echo implode(",", array_filter($arr, "is_scalar"));

输出:

one,two,4

You would need to iterate all the values in $tag and filter out those is array
such as

$tags = array();
foreach ($tag as $index=>$value)
{
  if (!is_array($value))
  {
     $tags[$index] = $value;
  }
}
implode(',', $tags);

I found the above is a bit tedious,
here is the improved version

$arr = array(0 => "one", 1 => "two", 2 => array(1,2,3), 3=>4, 4=>new stdClass);
echo implode(",", array_filter($arr, "is_scalar"));

output :

one,two,4
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文