Solved

php weird json result array

Posted on 2014-03-26
9
1,120 Views
Last Modified: 2014-03-26
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
0
Comment
Question by:justaphase
  • 4
  • 3
  • 2
9 Comments
 
LVL 1

Author Comment

by:justaphase
Comment Utility
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

0
 
LVL 9

Expert Comment

by:rfportilla
Comment Utility
Try this:

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

Open in new window

0
 
LVL 9

Expert Comment

by:rfportilla
Comment Utility
Nevermind, ignore last comment.  Not paying attention.  Will reread post.
0
 
LVL 9

Expert Comment

by:rfportilla
Comment Utility
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

0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
This is a compound statement:

json_encode($query->fetchAll(PDO::FETCH_ASSOC));

Open in new window

Compound statements are PHP AntiPractice #9.  Don't write code like that - it's a sure signal that a programmer is not very experienced, and it's very hard to debug.   Instead, write one line of code on each line of the program script.  You probably want something like this so you can visualize the data that is returned from the PDOStatement object.

while ($row = $query->fetchAll(PDO::FETCH_OBJ))
{
    // ROW BY ROW PROCESSING IS DONE HERE
    foreach ($row as $key => $obj)
    {
        echo "$key: ";
        print_r($obj);
        echo PHP_EOL;
    }
}

Open in new window

Once you see the return data you will immediately have a better idea how to use it with JSON_Encode().   If you can make those changes and show us the output of the foreach() loop, then we can move right on to getting the JSON string right.
0
 
LVL 1

Author Comment

by:justaphase
Comment Utility
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.
0
 
LVL 1

Author Closing Comment

by:justaphase
Comment Utility
Amazing! Works.
Like a boos Ray.

Thx
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Great!  Glad we got it pointed in the right direction :-)
0
 
LVL 1

Author Comment

by:justaphase
Comment Utility
Like boss lol... not boos
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now