Link to home
Start Free TrialLog in
Avatar of justaphase
justaphaseFlag for Portugal

asked on

php weird json result array

I'm returning a json result out of a pdo query like this:
json_encode($query->fetchAll(PDO::FETCH_ASSOC));

And when i receive i do it like this:
$array	= json_decode($output,true);
print_r($array);

Open in new window

and gives me this:
Array
(
    [0] => Array
        (
            [packstamp] => pmarketing
        )

    [1] => Array
        (
            [packstamp] => peventos
        )

    [2] => Array
        (
            [packstamp] => psystem
        )

    [3] => Array
        (
            [packstamp] => ploja
        )

    [4] => Array
        (
            [packstamp] => pproject
        )

)

Open in new window

And the json string is this:
[{"packstamp":"pmarketing"},{"packstamp":"peventos"},{"packstamp":"psystem"},{"packstamp":"ploja"},{"packstamp":"pproject"}]

Open in new window

i can't parse this right....
I try to get the value of each packstamp like this:
foreach($array as $row) {
	echo trim($row['packstamp']);
}

Open in new window

And the result is always the same, the value of the first column "pmarketing,pmarketing,etc..." instead of "pmarketing,peventos,etc..."

I thought the foreach would the same as i do with the parse of the pdo array result, but i guess not...

Can someone help me?

Thx in advanced,
Miguel
Avatar of justaphase
justaphase
Flag of Portugal image

ASKER

correction... i was doing a return during my foreach in my production code..
it gives all results, the only problem is that repeats the results 5 times...
like this
pmarketing
peventos
psystem
ploja
pproject
pmarketing
peventos
psystem
ploja
pproject
pmarketing
peventos
psystem
ploja
pproject
pmarketing
peventos
psystem
ploja
pproject
pmarketing
peventos
psystem
ploja
pproject

Open in new window

Try this:

foreach ($array as $row) {
	print $row['packstamp'] . "\n";	
}

Open in new window

Nevermind, ignore last comment.  Not paying attention.  Will reread post.
I am confused how you got that output without "\n".  

Regarding repeating results, did you put a foreach inside a foreach?  Can you give a little more code?  

I'm testing with this and it works fine for me.  

<?php
$array = json_decode('[{"packstamp":"pmarketing"},{"packstamp":"peventos"},{"packstamp":"psystem"},{"packstamp":"ploja"},{"packstamp":"pproject"}]', true);

foreach($array as $row) {
	echo trim($row['packstamp']);
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ray,

Sorry for not telling the all story.
I don't use the "PDO::FETCH_ASSOC", in fact i don't use it at all.
I do like this:
$pdo->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$query->execute();
and
$query->fetchAll();
And as always worked fine for me.
But since with json_encode was giving me this kind of array, i tried with "PDO::FETCH_ASSOC" (i saw it in stackoverflow). I'm only using this with json results.

I'm gonna try as you say.
Amazing! Works.
Like a boos Ray.

Thx
Great!  Glad we got it pointed in the right direction :-)
Like boss lol... not boos