justaphase
asked on
php weird json result array
I'm returning a json result out of a pdo query like this:
json_encode($query->fetchA ll(PDO::FE TCH_ASSOC) );
And when i receive i do it like this:
I try to get the value of each packstamp like this:
..." 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
json_encode($query->fetchA
And when i receive i do it like this:
$array = json_decode($output,true);
print_r($array);
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
)
)
And the json string is this:[{"packstamp":"pmarketing"},{"packstamp":"peventos"},{"packstamp":"psystem"},{"packstamp":"ploja"},{"packstamp":"pproject"}]
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']);
}
And the result is always the same, the value of the first column "pmarketing,pmarketing,etcI 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
Try this:
foreach ($array as $row) {
print $row['packstamp'] . "\n";
}
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.
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']);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
ASKER
Amazing! Works.
Like a boos Ray.
Thx
Like a boos Ray.
Thx
Great! Glad we got it pointed in the right direction :-)
ASKER
Like boss lol... not boos
ASKER
it gives all results, the only problem is that repeats the results 5 times...
like this
Open in new window