json - Grabbing Array Index with PHP -
i trying value array in php.
the json returned is
string(3017)"[{"userid":"1","0":"1","order":"[ { \"image\":\"undefined\",\"song\":\"alwaysnew.mp3\",\"album\":\"oh no!\"},{\"image\":\"undefined\",\"song\":\"always1.mp3\",\"album\":\"fly\"},{\"image\":\"undefined\",\"song\":\"always2.mp3\",\"album\":\"album 2\"},{\"image\":\"undefined\",\"song\":\"always3.mp3\",\"album\":\"album 1\"},{\"image\":\"undefined\",\"song\":\"always.mp3\",\"album\":\"album 4\"},{\"image\":\"undefined\",\"song\":\"alwaysnew2.mp3\",\"album\":\"album title unavailable\"},{\"image\":\"undefined\",\"song\":\"alwaysnew3.mp3\",\"album\":\"album title unavailable\"}]", "1": "[{\"image\":\"undefined\",\"song\":\"alwaysnew.mp3\",\"album\":\"oh no!\"},{\"image\":\"undefined\",\"song\":\"always1.mp3\",\"album\":\"fly\"},{\"image\":\"undefined\",\"song\":\"always2.mp3\",\"album\":\"album 2\"},{\"image\":\"undefined\",\"song\":\"always3.mp3\",\"album\":\"album 1\"},{\"image\":\"undefined\",\"song\":\"always.mp3\",\"album\":\"album 4\"},{\"image\":\"undefined\",\"song\":\"alwaysnew2.mp3\",\"album\":\"album title unavailable\"},{\"image\":\"undefined\",\"song\":\"alwaysnew3.mp3\",\"album\":\"album title unavailable\"}]", "user_email": null, "2": null }, { "userid": "2", "0": "2", "order": "[{\"image\":\"undefined\",\"song\":\"alwaysnew.mp3\",\"album\":\"oh no!\"},{\"image\":\"undefined\",\"song\":\"always1.mp3\",\"album\":\"fly\"},{\"image\":\"undefined\",\"song\":\"always2.mp3\",\"album\":\"album 2\"},{\"image\":\"undefined\",\"song\":\"always3.mp3\",\"album\":\"album 1\"},{\"image\":\"undefined\",\"song\":\"always.mp3\",\"album\":\"album 4\"},{\"image\":\"undefined\",\"song\":\"alwaysnew2.mp3\",\"album\":\"album title unavailable\"},{\"image\":\"undefined\",\"song\":\"alwaysnew3.mp3\",\"album\":\"album title unavailable\"}]", "1": "[{\"image\":\"undefined\",\"song\":\"alwaysnew.mp3\",\"album\":\"oh no!\"},{\"image\":\"undefined\",\"song\":\"always1.mp3\",\"album\":\"fly\"},{\"image\":\"undefined\",\"song\":\"always2.mp3\",\"album\":\"album 2\"},{\"image\":\"undefined\",\"song\":\"neville-peter-always3.mp3\",\"album\":\"album 1\"},{\"image\":\"undefined\",\"song\":\"always.mp3\",\"album\":\"album 4\"},{\"image\":\"undefined\",\"song\":\"alwaysnew2.mp3\",\"album\":\"album title unavailable\"},{\"image\":\"undefined\",\"song\":\"alwaysnew3.mp3\",\"album\":\"album title unavailable\"}]", "user_email": null, "2": null } ]"
i'm decoding results returned database
$order = json_decode($json,true);
and returns
array(2){ [ 0 ]=>array(6){ [ "userid" ]=>string(1)"1"[ 0 ]=>string(1)"1"[ "order" ]=>string(622)"[{"image":"undefined","song":"alwaysnew.mp3","album":"ohno!"},{"image":"undefined","song":"always1.mp3","album":"fly"},{"image":"undefined","song":"always2.mp3","album":"album2"},{"image":"undefined","song":"always3.mp3","album":"album1"},{"image":"undefined","song":"always.mp3","album":"album4"},{"image":"undefined","song":"alwaysnew2.mp3","album":"albumtitleisunavailable"},{"image":"undefined","song":"alwaysnew3.mp3","album":"albumtitleisunavailable"}]"[ 1 ]=>string(622)"[{"image":"undefined","song":"alwaysnew.mp3","album":"ohno!"},{"image":"undefined","song":"always1.mp3","album":"fly"},{"image":"undefined","song":"always2.mp3","album":"album2"},{"image":"undefined","song":"always3.mp3","album":"album1"},{"image":"undefined","song":"always.mp3","album":"album4"},{"image":"undefined","song":"alwaysnew2.mp3","album":"albumtitleisunavailable"},{"image":"undefined","song":"alwaysnew3.mp3","album":"albumtitleisunavailable"}]"[ "user_email" ]=>null[ 2 ]=>null }[ 1 ]=>array(6){ [ "userid" ]=>string(1)"2"[ 0 ]=>string(1)"2"[ "order" ]=>string(622)"[{"image":"undefined","song":"alwaysnew.mp3","album":"ohno!"},{"image":"undefined","song":"always1.mp3","album":"fly"},{"image":"undefined","song":"always2.mp3","album":"album2"},{"image":"undefined","song":"always3.mp3","album":"album1"},{"image":"undefined","song":"always.mp3","album":"album4"},{"image":"undefined","song":"alwaysnew2.mp3","album":"albumtitleisunavailable"},{"image":"undefined","song":"alwaysnew3.mp3","album":"albumtitleisunavailable"}]"[ 1 ]=>string(622)"[{"image":"undefined","song":"alwaysnew.mp3","album":"ohno!"},{"image":"undefined","song":"always1.mp3","album":"fly"},{"image":"undefined","song":"always2.mp3","album":"album2"},{"image":"undefined","song":"always3.mp3","album":"album1"},{"image":"undefined","song":"always.mp3","album":"album4"},{"image":"undefined","song":"alwaysnew2.mp3","album":"albumtitleisunavailable"},{"image":"undefined","song":"alwaysnew3.mp3","album":"albumtitleisunavailable"}]"[ "user_email" ]=>null[ 2 ]=>null } }
when use $song = $obj[0]['order'];
full order string:
string(622) "[{"image":"undefined","song":"alwaysnew.mp3","album":"oh no!"},{"image":"undefined","song":"always1.mp3","album":"fly"},{"image":"undefined","song":"always2.mp3","album":"album 2"},{"image":"undefined","song":"always3.mp3","album":"album 1"},{"image":"undefined","song":"always.mp3","album":"album 4"},{"image":"undefined","song":"alwaysnew2.mp3","album":"album title unavailable"},{"image":"undefined","song":"alwaysnew3.mp3","album":"album title unavailable"}]"
which great there way first object {"image":"undefined","song":"alwaysnew.mp3","album":"oh no!"}
?
my php code:
include_once($_server['document_root'].'/wp-load.php' ); $current_user = wp_get_current_user(); $user_email = $current_user->user_email; $db = new pdo("mysql:hostname=db","root","root"); $st = $db->prepare("select * wp_songs order userid"); $st->execute(); $obj = $st->fetchall(); $json = json_encode($obj); $order = json_decode($json,true); $song = $obj[0]['order']; if ($_post['submit']) { var_dump($song); }
Comments
Post a Comment