1

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?

2
  • 2
    You might want to add a tag for the language. Commented Nov 17, 2010 at 10:48
  • are you just looking for an algorithm, or do you want code? If you want code, you'll need to let us know the language. Commented Nov 17, 2010 at 10:58

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.