Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #19230
| From | "J.O. Aho" <user@example.net> |
|---|---|
| Newsgroups | comp.lang.php |
| Subject | Re: JSON and PHP |
| Date | 2022-12-28 22:09 +0100 |
| Message-ID | <k13pjjF4sbjU4@mid.individual.net> (permalink) |
| References | <tog2vc$tnl$21@gallifrey.nk.ca> <k12emkF4sbjU3@mid.individual.net> <tohkk5$f64$1@gallifrey.nk.ca> |
On 28/12/2022 15.42, The Doctor wrote:
> In article <k12emkF4sbjU3@mid.individual.net>,
> J.O. Aho <user@example.net> 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
Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
JSON and PHP doctor@doctor.nl2k.ab.ca (The Doctor) - 2022-12-28 00:34 +0000
Re: JSON and PHP "J.O. Aho" <user@example.net> - 2022-12-28 09:56 +0100
Re: JSON and PHP doctor@doctor.nl2k.ab.ca (The Doctor) - 2022-12-28 14:42 +0000
Re: JSON and PHP "J.O. Aho" <user@example.net> - 2022-12-28 22:09 +0100
Re: JSON and PHP H H H H H H H H H H H H H H H H H H E <angel00000100000@icloud.com> - 2023-01-04 09:10 -0800
Stop spamming (Was: JSON and PHP) "J.O. Aho" <user@example.net> - 2023-01-04 19:24 +0100
Re: JSON and PHP Arno Welzel <usenet@arnowelzel.de> - 2022-12-28 19:00 +0100
Re: JSON and PHP ThE sUn <angel00000100000@mail.ee> - 2022-12-28 21:28 -0800
csiph-web