adrianhb
-
Posts
3 -
Joined
-
Last visited
Posts posted by adrianhb
-
-
Thanks both
JSON array. Well, if I had a hierarchical structure like JSON to work with I wouldn't need to ask this question. Here's a print_r of the array, which is a bit closer to the structure I've actually got
Array ( [ec] => 3CB220804 [ID] => 4A73-1549 [FullName0] => Walter Mitty [email0] => walt@thurber.com [name0] => true [date00] => true [date10] => true [date20] => true [cost] => 90 )
Kicken, I do know the structure of the data so I know the key order. I just can't easily change it. I do indeed want to build a new array better suited to my needs, but my problem is that I can't find a way that isn't very clunky.
If I include []s in the POST string, e.g.
name[0]=true&date[0][0]=true&date[0][1]=true
[ and ] in fact have to be encoded as %5B and %5D respectively, but I can convert them back. So the question becomes, Is there a way in PHP to take the string above and automatically create the array from it? I can only think of calling eval() on each substring between the &s, and I think that may be frowned on.
-
I have an array created from URL-encoded POST data with repeating groups of elements. The keys are name0, date0A, date0B, name1, date1A, date1B....
I know how many dates follow each name (2 in the example above but can be any number) and I want to create a new array containing the dates following each new name. Using array_slice() is messy as it needs the starting index. Is there an elegant way to get the next N elements of an array starting from the current element?
The code uses foreach with key => value. I could use a for loop with a counter but this seems even clunkier. Maybe I need to use a while loop with next() and an exit condition, but it would be nice to find something that just creates an array with the next N elements. Using a callback function would be fine but I can't see it being any neater.
Yes I know POST data in JSON would have been nice but I'm working with what I've got!
Best way to get an array slice starting from 'current'
in PHP Coding Help
Posted
Thanks mac_gyver
Here's the JSON
{ "ec": "3CB220804", "ID": "1X1291167M031591B", "FullName0": "Walter Mitty", "email0": "walt@thurber.com", "phone0": "", "comments0": "", "member0": "true", "date00": "true", "date10": "true", "date20": "true", "cost": "90" }
Yes I can change the POST keys. I just didn't want to rewrite huge amounts to change to using JSON POST encoding. Faking up the []s, I get
{ "ec": "3CB220804", "ID": "4B965680NN227192S", "FullName": [ "Walter Mitty" ], "email": [ "walt@thurber.com" ], "phone": [ "" ], "comments": [ "" ], "member": [ "true" ], "date": [ [ "true", "true", "true" ] ], "cost": "90" }
Which is what I need. I didn't realise that POST encoding and decoding allowed arrays. Thanks all, I'll mark kicken's reply as the solution.