1

I got this

$data   = json_decode($json, true)["value1"];

after this I sort the response to name value:

foreach($data as $test) {
echo $test['name'];
}

this shows me all names I need, this worked perfectly.

Output: name1 name2 name3

Question: What have I to do, to get the foreach output in new single variables, so I can use them through the rest of the script ?

Expl.: $name_1 = name1;

it maybe that I completely wrong.

so much thanks from a newbie who writes his 1st project.

4
  • Why do you need individual variables to be able to use them throughout the rest of the script? Why can't you use the array? Numbered variables bad, arrays good! Commented Jan 8, 2018 at 21:31
  • Hi, thanks for answer. At this point I think its the easiest way for me, or I don't know a better way :) I want to use the "new variables" as names in a HTML File, only for echo, to label serval tables through a html file. Expl.: new Variable from foreach loop maybe $name_1 this I want use to label a header of a table Commented Jan 8, 2018 at 21:33
  • A better way to do this would be to use them in the array instead of reassigning them. You can interact with them directly. Ex. $data[0] would echo name1 Commented Jan 8, 2018 at 21:35
  • Hi, thanks yes this I know and runs in a other script in the project. Background I want to have a dynamic script which read out the names from the JSON, this value I need to sort some data and use them to create new requests. Commented Jan 8, 2018 at 21:37

3 Answers 3

5

This should be what you are looking for:

$i = 1;
foreach($data as $test) {
    ${'name_'.$i++} = $test['name'];
}

Although using an array for this is almost always the better choice.

Sign up to request clarification or add additional context in comments.

Comments

0

If you aren't comfortable with the json array, you can push the values to a new array:

$names = array();
foreach($data as $value)
{array_push($names,$value['name']);}

But creating variables dynamically and use them like arrays usually isn't a good way to go. Literally, that's what arrays are used for.

Comments

0

big thanks to you all, I think i solved with your inspiration.

after thinking in view of your answer, I tried a other step and now i got all the data i need in an array which i can access every the single data.

$data = json_decode($json, true)["value1"];

$value_names = array();

foreach($data as $value)
{
    $value_names[]=$value{'name'};
}

so now all data in the array:

array(8) { [0]=> string(6) "name" [1]=> string(6) "name" [2]=> string(10) "name" [3]=> string(12) "name" [4]=> string(12) "name" [5]=> string(9) "name" [6]=> string(13) "name" [7]=> string(4) "name" }

if I resolve this I got more question :) but I study myself before I ask so I practice my skills. Big thanks to the community !!!

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.