Ingeoding Array ...同时IGNORING嵌套数组?

Ingeoding Array ...同时IGNORING嵌套数组?

问题描述:

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?

我环顾四周,看到很多人都在询问如何使用嵌套数组来破坏数组。 但是,这些人通常也希望包含嵌套数组。 我不想包含嵌套数组...我想抛弃嵌套数组... p>

这是我的数组: p>

   [tag] => 数组
(
 [0] =>一个
 [1] =>两个
 [0_attr] =>数组
(
 [类别] =>三
 [锁] =&gt  ; 4 
)
 
 [2] =>五
)
  code>  pre> 
 
 

如果我内爆此数组,逗号分隔,我希望结果为 是: p>

 一,二,五
  code>  pre> 
 
 

请注意三个和四个不包括在内。 因为它们是嵌套数组,所以我不想要它。 我只想要立即的价值观。 我究竟怎么做到这一点? p> div>

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