1

I'm trying to implode a multi-dimensional array, but I'm missing something.

This is the array:

Array
(
    [0] => Array
        (
            [0] => Brand 1
        )
    [1] => Array
        (
            [0] => Brand 2
        )
)

So after some google-ing I found a couple of solutions but they all gave me different errors.

I read that array_map() should work, but I can't figure out what the second parameter should be.

I'm getting the following error:

Warning: array_map() expects at least 2 parameters, 1 given in

This is my code:

$imploded = implode("','",array_map($brand_array));

$brand_array is a newly created array constructed from an in_array function

The goal is to get a comma seperated string like:

brand1, brand2
Rizier123
  • 58,877
  • 16
  • 101
  • 156
Interactive
  • 1,474
  • 5
  • 25
  • 57
  • you can get reference form http://stackoverflow.com/questions/12050892/array-mapping-in-php-with-keys – Bushra Shahid Mar 25 '15 at 10:27
  • I think that what you are describing would be to implode the array after you flatten it (as it is called in underscore for example). If you google something like "php array flatten" you will find several solutions like http://davidwalsh.name/flatten-nested-arrays-php or https://gist.github.com/kohnmd/11197713 – mishu Mar 25 '15 at 10:28
  • Yeah but I need an comma seperated list like: `brand1, brand2` – Interactive Mar 25 '15 at 10:28

3 Answers3

2

array_map() expects you to add a callback function as first parameter and the array as second parameter.

You can also see this if you look at the signature of the function:

array array_map ( callable $callback , array $array1 [, array $... ] )

Parameters:

  • Callback:

    • Callback function to run for each element in each array.
  • Array:

    • An array to run through the callback function.

You have only the array in array_map You need something like this:

array_map('functionname', $array);
Rizier123
  • 58,877
  • 16
  • 101
  • 156
Loko
  • 6,539
  • 14
  • 50
  • 78
2

Well it seems like you're a bit confused how to use array_map(). But this should work for you:

echo implode(",", array_map(function($v){
    return implode(",", $v);
}, $arr));

So as an example:

$arr = [
        [1,2],
        [1,2],
    ];

With array_map() you go through each innerArray and implode() it by a comma, so you end up with following array:

Array ( [0] => 1,2 [1] => 1,2 )

Now you have a one dimensional array and you have to implode the return value of array_map() again and you implode it again by a comma and you will end up with:

1,2,1,2
Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • This answer is aimed at the OP with better explanation. +1. I was 9 seconds earlier, so I have that going for me which is nice. – Loko Mar 25 '15 at 10:32
  • 1
    Thank you for this detailed explanation. Very clear now and I understand why I couldn't directly use `implode()`. +1 – Interactive Mar 25 '15 at 10:40
0

Try below code

$input = array(
  array(
    'brand 1'
  ),
  array(
    'brand 2'
  )
);


echo implode(', ', array_map(function ($entry) {
  return $entry[0];
}, $input));
Lalit Sharma
  • 555
  • 3
  • 12