Why is my implode statement outputting array,array,array

I am a new coder and am trying to use ‘implode’ for the first time. When I try using it, the result is:

Array, Array, Array, Array, Array, Array, Array, Array

Could you please tell me where I have gone wrong. I’m using the same exact syntax as the php manual so I don’t know why this is not working. My Array does contain valid data.

$sql="SELECT * FROM TagMinis WHERE mini_id = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$id]);
$result_tags = $stmt->fetchAll();
$stmt = null;
$tagString = "";
$tagString = implode(', ', $result_tags);
echo $tagString;

Because fetchAll will return an array of arrays

foreach($result_tags as $result_tag) {
    echo implode(', ',$result_tag) . "\n";
}

Will get you closer but I suspect you really want to put all the tags into one line. Gonna take a bit more code.

I understand that the multi-dimensional array is why it is not working. I can’t figure out a way to make this work with a multi-dimensional array. I suspect if I set a loop with a counter checking for the end it would work but I only consider counters as a last resort.

Close also on my last resort list is doing this:

foreach ($result_tags as $row_tags) {
  if (next($result_tags) === false) {
    echo $row_tags['tags_name'];
  }
  else {
    echo $row_tags['tags_name'] . ', ';
  }
}

This almost works but the last result is not separated with a comma. In example the above will output:

apple, banana, orangepear

Where-as is should be: apple, banana, orange, pear

Any other ideas how I can do this? I would prefer the implode method, but after 3 hours I have pretty much given up on it. I’ll just be happy if I can get the ‘next’ working in my above example.

You should list the column(s) you are SELECTing in the query. This will get your query/code to document itself, result in only the data you want being transferred, and would tell us what you are trying to do.

If all you want are the values from a single column, just SELECT that_column. You can then use the PDO::FETCH_COLUMN fetch mode and get an array of those values, rather than an array of rows from the fetchAll() method.

If you want other data from this query as well, use php’s array_column() to get just the tag column values from the fetched data.

Your reasons are your own; if what you really want to do is smash all of the values together into a single line, your code is this:
implode(",",array_map(implode,$result_tag));

Oh wow it was so simple… I found this link which references array_column. (While I was searching, I see mabismad also mentioned this – thank you.)

No loop – no conditional tests – just 1 single function:

echo implode(', ', array_column($result_tags, 'tags_name'));

Here’s hoping I never forget this now :slight_smile:

Mab’s other point is valid too - don’t SELECT * unless you need to. If all you want is tags_name, only select tags_name.

Thanks m_hutley.

I just found a link about this as well. The link seems to indicate that array_column would be better for this than array_map. Is one more system resource intensive than the other – or are both ways fine?

array_column targets a specific column in the sub-arrays. array_map says “Apply this function to every member of the array” - in other words, it would work across the entire sub-array, rather than the specific column.

It’s not really a question of resource intensivity (at that step - SELECT * is more system resource intensive than not!) at that point - it’s different functions for different use cases.

Great, thanks for walking me through that m_hutley. That’s very helpful information.

If you’re a new coder, don’t lose any sleep worrying about whether you’ll forget stuff - if you don’t use it much, you probably will. As long as you can remember somewhere you used it, or where you keep a note of stuff you rarely use, that’ll be enough.

Or, like most of us, you’ll go “It’s something to do with an array…” open php.net instinctively, and search for “array”, before scrolling down the function list until something rings a bell or sounds vaguely like what you’re trying to do.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.