简体   繁体   中英

How to select the barcode part of the JSON file I have using PHP?

I am trying to pull certain parts of information I have in a JSON file but unable to get the parts I want. At the moment I am focusing on getting barcode and can then work out how to do it from there.

I have tried several different ways to trying to select the part of the array I want but cannot work out why it errors with Undefined index: barcode every time

<?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)
?>

I expect it to show the barcode section from my JSON file.

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

But at this moment I seem unable to select anything from in here.

正如克里斯·怀特(Chris White)提到的(只是正式回答), data部分是一个数组,因此访问它的正确方法是:

echo $data["data"][0]["barcode"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM