0

I have an array similar to this but could be more or less pet names:

  Array ( [0] => Array ( [pet_name] => Bella ) [1] => Array ( [pet_name] => Zoey ) [2] => Array ( [pet_name] => Pooky ) ) 

And I'm trying to get a string like this:

Bella,Zoey,Pooky

I've tried to implode the array but I get a php error notice. Ive tried:

call_user_func_array('array_merge',$array);

But it only returns the first sub array.

How do I go about iterating through this array and creating a string from the pet names? I'm still learning how to work with complex arrays.

1 Answer 1

1

You need to select the correct array key before imploding:

<?php
$pet_names = array();
foreach($array as $current) {

    $pet_names[] = $current['pet_name'];

}

echo implode(',', $pet_names);

// Bella,Zoey,Pooky

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