我试图从PHP中的数组中获取某个值。
返回的json是
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 is Unavailable\"},{\"image\":\"undefined\",\"song\":\"AlwaysNew3.mp3\",\"album\":\"Album Title is 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 is Unavailable\"},{\"image\":\"undefined\",\"song\":\"AlwaysNew3.mp3\",\"album\":\"Album Title is 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 is Unavailable\"},{\"image\":\"undefined\",\"song\":\"AlwaysNew3.mp3\",\"album\":\"Album Title is 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 is Unavailable\"},{\"image\":\"undefined\",\"song\":\"AlwaysNew3.mp3\",\"album\":\"Album Title is Unavailable\"}]",
"user_email": null,
"2": null
}
]"
我正在使用
解码从我的数据库返回的结果$order = json_decode($json,true);
它返回
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
}
}
当我使用$song = $obj[0]['order'];
时,我得到完整的订单字符串:
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 is Unavailable"},{"image":"undefined","song":"AlwaysNew3.mp3","album":"Album Title is Unavailable"}]"
哪个好,但有没有办法只获得第一个对象{"image":"undefined","song":"AlwaysNew.mp3","album":"Oh No!"}
?
我的PHP代码:
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 * from wp_songs order by 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);
}