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.
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.
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));
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.
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.