简体   繁体   中英

Returning Json Data To PHP Variables

I know there are a lot of similar question as this one on here but the problem I keep running into is that the method in which those other json file arrays are setup is not the same as mine.

What I am trying to do should just be a simple process but as I am not as versed in json arrays and I am other things, the solution is eluding me completely.

I just want to take the data display in a local json file and create PHP variables for each item returned.

The json file is simple and looks something like this...

[
    {
        "titleOne": "Foo",
        "textOne": "Bar",
        "titleTwo": "Foo",
        "textTwo": "Bar"
    }
]

It will always consist of just these 4 items. Then I use the following PHP to read and decode the file...

$data = file_get_contents ('./data.json');
$json = json_decode($data, true);
foreach ($json as $key => $value) {
    foreach ($value as $key => $val) {
            echo $key . '====>' . $val . '<br/>';
    }
}

but this simply outputs the data. I am trying to get each one of these 4 items to become variables. Example...

$titleOne
$textOne
$titleTwo
$textTwo

...so that the variables can be used in a form.

I have found many similar questions as this but the json data is always setup differently resulting in errors as results.

Why not simply define if it's always only 4:

$titleOne = $json[0]['titleOne'];
$textOne = $json[0]['textOne'];
$titleTwo = $json[0]['titleTwo'];
$textTwo = $json[0]['textTwo'];

You can use list to extract elements into variables. Keep in mind, that it only works with numerical arrays.

$json = '[
    {
        "titleOne": "Foo",
        "textOne": "Bar",
        "titleTwo": "Foo",
        "textTwo": "Bar"
    }
]';

$json = json_decode($json, true);
foreach ($json as $object) {
    list($titleOne, $textOne, $titleTwo, $textTwo) = array_values($object);
}

With php you can't use a variable to name another variable. You could however use an associative array to do this.

The true in json_decode($data, true); already returns the data in the form of an associative array. In order to access the value of titleOne, you would just do $json['titleOne'] . This would give you Foo .

With php you CAN use a variable to name another variable. Just use Variable variables :

$a = 'var';
$$a = 'hello world';
echo $var;
// output : hello word

In your case :

You don't have to wrap your json data in array and make 2 loops :

{
    "titleOne": "Foo",
    "textOne": "Bar",
    "titleTwo": "Foo",
    "textTwo": "Bar"
}

You can create your dynamic variables this way :

$data = file_get_contents ('./data.json');
$json = json_decode($data, true);
foreach ($json as $key => $val) {
    $$key = $val;
}
echo $titleOne;
// output : Foo

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