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?
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