0

So I have a php script for PDO I have been using, not big but the line that's giving me trouble is:

while ($row = $try->fetchAll(PDO::FETCH_ASSOC)) {
            var_dump($row);

This outputs the data in the intended manner:`

array(5) {
  [0]=>
  array(2) {
    ["DataID"]=>
    string(1) "1"
    ["Description"]=>
    string(3) "!!!"
  }
  [1]=>
  array(2) {
    ["DataID"]=>
    string(1) "2"
    ["Description"]=>
    string(18) "This is document 2"
  }

BUT when I change the final line to:

echo implode(" - ", $row);

It outputs:

Array
Array

May I know why this is the case and how I will be able to fix this? Thank you!

2 Answers2

1

Your are fetching all the rows at he same time, so your while statement code executes once with all the rows:

while ($row = $try->fetchAll(PDO::FETCH_ASSOC)) {
            var_dump($row);

Thats why your var_dump prints all the content (but just once!).

And while imploding you are imploding only an array.

If you do a fetch row by row:

while ($row = $try->fetch(PDO::FETCH_ASSOC)) {
            var_dump($row);

You'll print as arrays as rows you have and your implode will succeed as you expect

javier_domenech
  • 5,995
  • 6
  • 37
  • 59
  • Ah, thank mate that makes sense. My only question is now the implode does work, however it doesn't seem to take into account linebreak or anything in quotes as in the following: while ($row = $try->fetch(PDO::FETCH_ASSOC)) { echo implode("\n", $row); – user324879 Apr 10 '15 at 10:10
  • It depends, if you are reading the output from the browser maybe you need a html tag instead: – javier_domenech Apr 10 '15 at 10:11
  • I've tried , \n,
     but they all seem to output the same way (in one line). :x
    – user324879 Apr 10 '15 at 10:18
  • But if you put for instance a dot '.' is all concatenated by the dot or never concatenates whatever you put?. Also, I would like to see that output and if possible one var_dump output to compare – javier_domenech Apr 10 '15 at 10:24
  • var_dump: array(1) { ["firstname"]=> string(4) "jake" } array(1) { ["firstname"]=> string(6) "steven" } – user324879 Apr 10 '15 at 10:34
  • ahh, seems weird but I've fixed it. for some reason in echo implode("\n", $row); the linebreak doesn't get recognized so it outputs in 1 line but if i set a variable to implode and then separately create a line break it works - don't know why. – user324879 Apr 10 '15 at 10:41
  • Better, I didn't know what else to say :) – javier_domenech Apr 10 '15 at 10:43
  • Last question I have is it seems when grabbing one column it's fine but grabbing multiple columns, the array as a whole gets concatenated and at the end 1 record pulled (i.e. firstname,lastname), then it starts the linebreak. do you know how to solve this? – user324879 Apr 10 '15 at 10:48
  • I'm lost at this point, I suggest you to open a new question showing: your code, var_dump output, and expected result. – javier_domenech Apr 10 '15 at 10:50
0

There will be array_column function is PHP 5.5, you will be able to do this

 $myHiddenField = implode(',', array_column($yourMainArray, 'code'));

 For now you have to use your own loop

  $values = array();
  foreach ($yourMainArray as $address)
  {
   $values[] = $address['code'];
  }
  $myHiddenField = implode(',', $values);

kindly go through two links

Multidimensional Array Implode

Implode data from a multi-dimensional array

Community
  • 1
  • 1
kailash sharma
  • 356
  • 1
  • 12