user3832367
user3832367

Reputation: 13

Saparate array data with comma

I have an function that return an array

  [0]=>
  array(2) {
    [0]=>
    string(2) "22"
    [1]=>
    string(9) "Plantroom"
  }
  [1]=>
  array(2) {
    [0]=>
    string(2) "22"
    [1]=>
    string(4) "dfdf"
  }
}

sometime my array have one object or multiple .

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

now I want to show my array data by comma separated.

like

for first array => Plantroom,dfdf

for second array =>sec

I am using this code but not work

my function

function area_name($game_id)
{
    include_once('Model/Game.php');
    $c2 = new Game();

    $cd2 = $c2->Select_area($game_id);


return $cd2;

} 

and call my function as

implode(", ", area_name($cd[$i][0]))

But my output show text Array

Upvotes: 0

Views: 38

Answers (1)

Havenard
Havenard

Reputation: 27864

Because area_name() is not just returning an array, its returning an array of arrays. implode() will join the elements of the array that area_name() returns assuming they are strings, but those elements are also arrays and as such they are stringified to text "Array".

To obtain the desired output from implode() you would have to first generate an array with only the values you want from the structure returned by area_name().

For instance:

$data = array_map(function ($a) { return $a[1]; }, area_name($cd[$i][0]));
echo implode(', ', $data);

Upvotes: 1

Related Questions