国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

如何使用 PHP 從 JSON 中提取和訪問數(shù)據(jù)?
P粉099985373
P粉099985373 2023-08-23 20:52:16
0
1
608
<p><br /></p><blockquote> <p>這是一個通用參考問題和答案,涵蓋許多永無休止的“如何訪問 JSON 中的數(shù)據(jù)?”問題。它在這里處理在 PHP 中解碼 JSON 并訪問結(jié)果的廣泛基礎(chǔ)知識。</p> </blockquote> <p>我有 JSON:</p> <pre class="brush:php;toolbar:false;">{ "type": "donut", "name": "Cake", "toppings": [ { "id": "5002", "type": "Glazed" }, { "id": "5006", "type": "Chocolate with Sprinkles" }, { "id": "5004", "type": "Maple" } ] }</pre> <p>如何在 PHP 中對其進(jìn)行解碼并訪問結(jié)果數(shù)據(jù)?</p>
P粉099985373
P粉099985373

全部回復(fù)(1)
P粉388945432
<?php
$jsonData = '{
    "type": "donut",
    "name": "Cake",
    "toppings": [
        { "id": "5002", "type": "Glazed" },
        { "id": "5006", "type": "Chocolate with Sprinkles" },
        { "id": "5004", "type": "Maple" }
    ]
}';

// Decode the JSON
$data = json_decode($jsonData, true);

// Access the data
$type = $data['type'];
$name = $data['name'];
$toppings = $data['toppings'];

// Access individual topping details
$firstTopping = $toppings[0];
$firstToppingId = $firstTopping['id'];
$firstToppingType = $firstTopping['type'];

// Print the data
echo "Type: $type\n";
echo "Name: $name\n";
echo "First Topping ID: $firstToppingId\n";
echo "First Topping Type: $firstToppingType\n";
?>

在此示例中,json_decode() 用于將 JSON 數(shù)據(jù)解碼為 PHP 關(guān)聯(lián)數(shù)組。然后,您可以像訪問任何 PHP 數(shù)組一樣訪問該數(shù)組的各個元素。

最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板