0

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.

7
  • Just access the array entries in your form, e.g. $json[0]['titleOne'] Commented Aug 27, 2016 at 22:24
  • I am not that educated with json arrays. Could you explain as to how I can do that please? Commented Aug 27, 2016 at 22:25
  • 1
    Just use, e.g. $json[0]['titleOne'] Commented Aug 27, 2016 at 22:26
  • I've updated my comment. For further research, the term you are wanting is associative array, rather than explicitly json arrays. Commented Aug 27, 2016 at 22:27
  • Or consider $json = json_decode($data, true)[0]; and then just $json['titleOne'], $json['textOne'], etc. Commented Aug 27, 2016 at 22:27

4 Answers 4

3

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'];
Sign up to request clarification or add additional context in comments.

Comments

3

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);
}

Comments

0

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.

3 Comments

Are you about $$varname? Or ${'title'}?
Sorry, I don't quite understand what you are asking.
Hmm, sorry. I was thinking of a completely different language. I will update my answer.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.