0

I see some questions but no work to me.

In a for loop i receive an array like that:

array(1) { [0]=> array(1) { [0]=> string(1) "4" } } 

array(1) { [0]=> array(2) { [0]=> string(1) "3" [1]=> string(1) "4" } }

array(1) { [0]=> array(7) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(2) "30" [3]=> string(2) "43" [4]=> string(2) "65" [5]=> string(2) "53" [6]=> string(3) "634" } }

I need implode that values with "-", my desired output isa string:

4

3-4

2-30-43-65-53-634

I try some ways, but no work, some ideia for do it simple?

4
  • 1
    Possible duplicate of Multidimensional Array PHP Implode Commented May 2, 2018 at 23:30
  • @ObsidianAge but the output need to be string, i gona edit Commented May 2, 2018 at 23:31
  • 1
    implode returns a string echo implode('-',$array); Commented May 2, 2018 at 23:37
  • That question is about implode values from a single column, i need implode values from all columns... @ObsidianAge Commented May 2, 2018 at 23:43

1 Answer 1

2

If it is a two dimensional array and would like to output all elements, you could use a foreach loop and output the implode of each like so:

$mainArray = [
    [4],
    [3, 4],
    [2, 30, 43, 65, 53, 634]];

foreach($mainArray as $key => $secArray){
    echo implode('-', $secArray) . '<br/>';
}

PHP Implode

Notice the return type of implode is a string.

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.