如何选择我使用PHP的JSON文件的条形码部分?

问题描述 投票:-1回答:1

我试图在JSON文件中提取我的某些信息但无法获取我想要的部分。目前我专注于获取条形码,然后可以从那里找出如何做到这一点。

我尝试了几种不同的方法来尝试选择我想要的数组部分,但无法解决为什么它与未定义索引的错误:每次条形码

<?php
$host="localhost";
$username="root";
$password="root";
$dbname="demo";
//create a Connection
$conn=mysqli_connect($host,$username,$password,$dbname);

//check the connection

if(!$conn)
{
 die("connection does not established successfully :".mysqli_connect_error());
}
//read the json file using php method   file_get_contents('filename.json')
$jsondata=file_get_contents('file.json');

//convert json into php array
$data=json_decode($jsondata,true);

//get the details of student from JSON file and store it in the variable one by one.
$id=$data['data']['barcode'];

print_r($id)
?>

我希望它能从我的JSON文件中显示条形码部分。

{
    "data": [
        {
            "baseSku": "71JNDAZA",
            "sku": "71JNDAZA08",
            "additionalSku": [],
            "barcode": "889042766774",
            "additionalBarcode": [],
            "model": "71JND",
            "title": "Arta Lace Ruffle Dress"
        }
    ]
}

但此时我似乎无法从这里选择任何东西。

php arrays json
1个回答
1
投票

正如Chris White提到的那样(只是正式回答)data部分是一个数组,所以访问它的正确方法是:

echo $data["data"][0]["barcode"];
© www.soinside.com 2019 - 2024. All rights reserved.