Re: JSON and PHP
"J.O. Aho" <[email protected]>
| Newsgroups | comp.lang.php |
|---|---|
| Message-ID | <[email protected]> |
On 28/12/2022 15.42, The Doctor wrote: > In article <[email protected]>, > J.O. Aho <[email protected]> wrote: >> On 28/12/2022 01.34, The Doctor wrote: >>> Trying to implement a JSON array in PHP >>> Using >> >> <too much code so removed, use a code repository and link to it> >> >>> Just wonder how to supress repeats. >> >> Don't insert the same data multiple times into an array. >> >> Here is a short code demonstrating what you are doing >> --- example.php --- >> <?PHP >> for($i = 0; $i < 3; $i++) { // this replaces the foreach in the original >> code >> $MyObj[] = array("same each time", "this from round $i"); >> var_dump($MyObj); >> } >> ?> >> --- eof --- >> >> You can see how the array grows each iteration of the loop. >> > > Just that we are converting from php to php/json. The issue ain't related to the conversion, it's you logic that is flawed. > Further, the foreach is due to a line that is coming is from > a previous session being passed on. But it's your foreach loop that makes you to have duplicates in your example in the same way as in the small example file. Take some time and think about what happens in each iteration, when you know what "$MyObj[] =" means, then you can make the proper changes in your own code. I would also think you could simplify the whole thing for example a lot of the data is static, just set that data once not each time in a foreach loop and also as Arno already pointed out, don't add a single value as an array, you just make things more complex than they need to be. Remember to not use input type="hidden" for information a user already have posted to you, those goes into the session, the only thing would be an identifier that is unique for this transaction. Prices you should never take from post values, only the amount of items, for if you start trust the posted values, I will be able to buy everything you have for 0 bucks. -- //Aho