adrianhb Posted August 16, 2022 Share Posted August 16, 2022 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! Quote Link to comment https://forums.phpfreaks.com/topic/315190-best-way-to-get-an-array-slice-starting-from-current/ Share on other sites More sharing options...
Barand Posted August 16, 2022 Share Posted August 16, 2022 Why don't you json_encode the array and post it here so we can see what you are talking about. Quote Link to comment https://forums.phpfreaks.com/topic/315190-best-way-to-get-an-array-slice-starting-from-current/#findComment-1599450 Share on other sites More sharing options...
Solution kicken Posted August 16, 2022 Solution Share Posted August 16, 2022 49 minutes ago, adrianhb said: Is there an elegant way to get the next N elements of an array starting from the current element? Just looking for a name element and then taking the next N elements is not really a good solution. Unless you've previously sorted the array into a specific order, you can't guarantee the next N elements are actually the ones you want. If your structure is name0 matches with date0 then the thing to do would be build a new array that maps the name entries to the date entries and then you will have a simple array of date entries that you can slice off what you want. Even better if you can would be to rename your form elements so that PHP will essentially do that mapping for you. Give your form elements names like name[0] and date[0][A], then you could just match the indexes between the name and date arrays. Quote Link to comment https://forums.phpfreaks.com/topic/315190-best-way-to-get-an-array-slice-starting-from-current/#findComment-1599451 Share on other sites More sharing options...
adrianhb Posted August 16, 2022 Author Share Posted August 16, 2022 (edited) 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. Edited August 16, 2022 by adrianhb got it wrong Quote Link to comment https://forums.phpfreaks.com/topic/315190-best-way-to-get-an-array-slice-starting-from-current/#findComment-1599454 Share on other sites More sharing options...
mac_gyver Posted August 16, 2022 Share Posted August 16, 2022 if this is data you have control over the form markup for, just use array names for the form fields. you will 'automatically' get arrays in the php $_POST data. the reason @Barandasked you to json encode an example of the data and post it is so that we would have something to work with, rather than a print_r output that no one here is likely to take the time to overtype into usable input data. Quote Link to comment https://forums.phpfreaks.com/topic/315190-best-way-to-get-an-array-slice-starting-from-current/#findComment-1599456 Share on other sites More sharing options...
adrianhb Posted August 16, 2022 Author Share Posted August 16, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/315190-best-way-to-get-an-array-slice-starting-from-current/#findComment-1599461 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.