简体   繁体   English

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

[英]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. 我正在尝试提取JSON文件中某些部分的信息,但无法获取所需的部分。 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. 我希望它显示我的JSON文件中的条形码部分。

{
    "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"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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