Categories:
Cloud (204)
Entertainment (11)
Facebook (43)
General (50)
Life (31)
Programming (64)
Technology (430)
Testing (4)
Tools (488)
Twitter (5)
Wearable (26)
Web Design (44)
Collections:
Other Resources:
php json array problems
I am creating a json array in PHP like this (it's inside a class):
$STH = $DBH->query("SELECT ID,title,content FROM table WHERE ID= '".$ID."' ");
$querycount = $STH->rowCount();
if($querycount!=0):
$STH->setFetchMode(PDO::FETCH_OBJ);
while( $row = $STH->fetch()) :
$thread[$row->ID]['title'] = $row->title;
$thread[$row->ID]['content'] = $row->content;
endwhile;
$this->querycount = $querycount;
$this->thread = json_encode($thread);
else:
$this->querycount = $querycount;
endif;
But I seem to get "strange" decodes the other end. I cannot actually reference the array by key's;
So far it will let me do this: (Where af_threads is the class name and $key id the ID in the table query)
$threads = new af_threads($key,$DBH);
$threads = json_decode($threads->thread);
foreach($threads as $key1=>$thread):
foreach($thread as $key2=>$val):
echo $key2;
echo $val;
endforeach;
echo '
';
endforeach;
But what it will not let me do is something like this
$threads[$key]['title']
I'm not sure why. As the class builds i.e. with more rows etc. I will need to call specific rows in specific places. i.e. $threads[$key]['newrow'], $threads[$key]['newrow2'], and or to manipulate a row with say substr(); etc.
Yes I know I could just echo out an array without json, but eventually I want to add cross domain functionality with JQuery getJSON() ...
✍: Guest
By default, json_decode() returns stdClass, modify second line with
$threads = json_decode($threads->thread, true)
and you will get an associative array and be able to reference it by keys, $threads[$key]['title'] for example.
2015-05-21, 1627🔥, 0💬
Popular Posts:
Why am I getting the "Spawn Failed: Server at http://127.0.0.1:47353/u ser/fyicenter/didn't respond ...
Where to find answers to frequently asked questions about Adobe Flash Player? I want to understand m...
How to troubleshoot Fitbit device sync issue? My Fitbit device is connected to my Windows computer, ...
How to edit Mozilla Firefox configuration file? Mozilla Firefox has a configuration file that contro...
How to remove Microsoft Teams form my Windows 7 system? I don't want it any more. You can remove Mic...