-3

I'm looking to implode my array into a string that looks like this:

1,a,v,v|2,b,v,v|3,c,v,v|4,d,v,v|5,d,v,v

Here's my array:

array (size=5)
  0 => 
    array (size=4)
      0 => string '1' (length=1)
      1 => string 'a' (length=1)
      2 => string 'v' (length=1)
      3 => string 'v' (length=1)
  1 => 
    array (size=4)
      0 => string '2' (length=1)
      1 => string 'b' (length=1)
      2 => string 'v' (length=1)
      3 => string 'v' (length=1)
  2 => 
    array (size=4)
      0 => string '3' (length=1)
      1 => string 'c' (length=1)
      2 => string 'v' (length=1)
      3 => string 'v' (length=1)
  3 => 
    array (size=4)
      0 => string '4' (length=1)
      1 => string 'd' (length=1)
      2 => string 'v' (length=1)
      3 => string 'v' (length=1)
  4 => 
    array (size=4)
      0 => string '5' (length=1)
      1 => string 'd' (length=1)
      2 => string 'v' (length=1)
      3 => string 'v' (length=1)

Any help would be greatly appreciated.

Thanks.

KeepCool
  • 497
  • 1
  • 6
  • 24
  • 2
    Start writing code. We will not do it for you. – u_mulder Oct 19 '16 at 20:04
  • 1
    It's the same approach as imploding a multidimensional array with _same_ delimiters, only instead of same, different. – Don't Panic Oct 19 '16 at 20:06
  • How do I downvote u_mulder? It's people like this that bring absolutely nothing to this site. Don't say anything at all if you're not willing to help a new beginner. Thanks "Don't Panic" for trying to help! – KeepCool Oct 19 '16 at 20:15
  • 1
    Just reverse your answer here http://stackoverflow.com/questions/39983414/how-to-create-a-multi-dimensional-array-from-string – AbraCadaver Oct 19 '16 at 20:16
  • Thanks AbraCadaver. I tried something similar to that, but I'll take a look at the answers there and give a shot. Thanks for your help! – KeepCool Oct 19 '16 at 20:21
  • 1
    u_mulder has answered hundreds of questions. I don't think it's appropriate to say that people like that bring nothing to the site just because they don't like your question. – Don't Panic Oct 19 '16 at 20:37

1 Answers1

1

If you were starting with an array of strings, like this:

$strings = ['1,a,v,v', '2,b,v,v', '3,c,v,v', '4,d,v,v', '5,d,v,v'];

It would be easy:

$result = implode('|', $strings);

You probably already know how to do that. So the question is how to get this array of arrays

$arrays = [
    ['1','a','v','v'],
    ['2','b','v','v'],
    ['3','c','v','v'],
    ['4','d','v','v'],
    ['5','d','v','v']
];

into the same format as $strings.

Fortunately that's easy too. Just do the same thing, only in a loop.

foreach ($arrays as $array) {
    // implode each item and add the result to a new array
    $strings[] = implode(',', $array);
}

It can also be done using the array_map function.

$strings = array_map(function($array) {
    return implode(',', $array);
}, $arrays);

For something basic like this, it really doesn't matter which way you do it.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
  • Thanks for your answer. But I'm getting just "Array" when I try to echo the string. It should output: 1,a,v,v|2,b,v,v|3,c,v,v|4,d,v,v|5,d,v,v. – KeepCool Oct 19 '16 at 20:41
  • 1
    Don't forget, the second part is just to show how to get from `$arrays` to `$strings`. You'll still have to do `$result = implode('|', $strings);` to get the final result. – Don't Panic Oct 19 '16 at 20:43
  • `$strings` is still an array, and 'Array' is just what PHP gives you when it converts an array to a string, which it must do when you try to `echo` it. (_Any_ array, regardless of what's in it.) – Don't Panic Oct 19 '16 at 20:45
  • Thank you thank you thank you! I really appreciate your time for helping me out on this. Arrays are so confusing to me right now. – KeepCool Oct 19 '16 at 20:47