简体   繁体   English

将Json数据返回到PHP变量

[英]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. 我知道这里有很多类似的问题,但是我一直遇到的问题是设置其他json文件数组的方法与我的不同。

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. 我想做的事情应该只是一个简单的过程,但是由于我不熟悉json数组,而我还有其他事情,因此解决方案使我完全无法理解。

I just want to take the data display in a local json file and create PHP variables for each item returned. 我只想将数据显示在本地json文件中,并为返回的每个项目创建PHP变量。

The json file is simple and looks something like this... json文件很简单,看起来像这样...

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

It will always consist of just these 4 items. 它始终仅由这4个项目组成。 Then I use the following PHP to read and decode the file... 然后,我使用以下PHP读取和解码文件...

$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. 我发现了很多类似的问题,但是json数据的设置总是不同,导致结果出错。

Why not simply define if it's always only 4: 为什么不简单地定义它是否总是只有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. 使用php时,您不能使用变量来命名另一个变量。 You could however use an associative array to do this. 但是,您可以使用关联数组来执行此操作。

The true in json_decode($data, true); json_decode($data, true);true 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'] . 为了访问titleOne的值,您只需执行$json['titleOne'] This would give you Foo . 这会给你Foo

With php you CAN use a variable to name another variable. 使用php,您可以使用一个变量来命名另一个变量。 Just use Variable variables : 只需使用Variable变量

$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 : 您不必将json数据包装在数组中并进行2个循环:

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

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

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