0

I'm trying to send data via $POST, and have that data passed and pushed into the array, then written back to the json file. My problem is coming within the array push, I think I need a foreach to go above array_push, that way when jsondata is written to the file and, then reimporting that file when the next $POST is sent, it'll all be nested under the same json dict. But as you can see from my check.json file, I'm not having any luck. Thanks in advance...

test11.php

<?php
$code = $_POST['code'];

$cpu = $_POST['cpu'];

$formdata = array($code=> $cpu);

$inp = file_get_contents('results.json');
$tempArray = json_decode($inp, true);
array_push($tempArray, $formdata);
$jsonData = json_encode($tempArray);
file_put_contents('results.json', $jsonData);
echo "This is the formdata = $formdata";
echo "This is the inp = $inp";
echo "This is the jsonData = $jsonData ";

?>

t11.html

<form action="test11.php" method="POST">
CPU Name:<br>
<input type="text" name="cpu">
<br><br/>
Code:<br>
<input type="text" name="code">
<br><br>
<input type="submit" value="Submit">
</form>

check.json

{}

When I run it, my results returned are not in the same JSON DICT.

Output of Check.json

[{"321":"jake"},{"88":"thomas"}]

I'm wanting it to look like this:

[{321:"jake",88:"thomas"}]
4
  • 1
    Instead of pushing the array into the array you want to merge it. Commented Aug 4, 2016 at 19:44
  • 1
    its a sticky wicket using numbered keys that mean something, things like array_merge will renumber them if they are not strings. array_merge::Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array. Commented Aug 4, 2016 at 19:52
  • 1
    Just add it into the existing array $tempArray[$code] = $cpu, the reason you get a nested array is because you are pushing an array on to the end or the other array $tempArray Commented Aug 4, 2016 at 20:07
  • Really, that's probably my entire issue, as I want to rearrange it anyway. THanks! Commented Aug 4, 2016 at 20:09

2 Answers 2

2

Join the form data array and the decoded JSON array using +:

$tempArray = $tempArray + $formdata;
$jsonData  = json_encode($tempArray);

Or just add the new key:

$tempArray[$code] = $cpu;
$jsonData  = json_encode($tempArray);

Or as I think the other answer is trying to suggest, use an object:

$tempObj = json_decode($inp);
$tempObj->$code = $cpu;
$jsonData = json_encode($tempObj);
Sign up to request clarification or add additional context in comments.

3 Comments

Id be careful with array merge and keys like this array($code=>$cpu) or array(321=>"jake")
@ArtisiticPhoenix: Good catch.
@ArtisiticPhoenix: Don't know about better, but it's less code if the form data is larger. Added anyway.
1

I hope this helps, but you're appending an item to an array when what you are looking to do is add a property to an object.

In the "JSON you want", you show an array with ONE object with two properties, 321 and 88. Those two properties have the values "jake" and "thomas" respectively.

So you can change just one line:

array_push($tempArray, $formdata);

which adds an item to the array, to something like

$tempArray->$code = $cpu;

or even

$tempArray[$code] = $cpu;

to just append the property and value to the existing object.

That allows you to delete:

$formdata = array($code=> $cpu);

Thanks, Wayne

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.