Stoffer Posted March 24, 2020 Share Posted March 24, 2020 Hi, I am trying to organise some data from a form which is transferred to me via jQuery. Right now I can transfer the data to my php function as either serialised data. Here I need to figure out to structure my data in arrays that allows me to work with them before I save to the database.. But how do I do this? I have been fiddling with explode, and trying to flatten it and do an array_push using a "while" for every 6'th item.. What I am looking for is this: Array[$i] ( Weight=>, repetition=>, field_id=>, set_id=> exercise_id=>, planned_workout_id= ) Array[$i++] ( Weight=>, repetition=>, field_id=>, set_id=> exercise_id=>, planned_workout_id= ) The structure I can have the data in Serialised form: Weight=&repetition=&field_id=h&set_id=1&exercise_id=1&planned_workout_id=1&Weight=&repetition=&field_id=h&set_id=2&exercise_id=1&planned_workout_id=1&Weight=&repetition=&field_id=h&set_id=3&exercise_id=1&planned_workout_id=1&Weight=&repetition=&field_id=h&set_id=4&exercise_id=1&planned_workout_id=1&Weight=&repetition=&field_id=h&set_id=1&exercise_id=13&planned_workout_id=1&Weight=&repetition=&field_id=h&set_id=2&exercise_id=13&planned_workout_id=1&Weight=&repetition=&field_id=h&set_id=3&exercise_id=13 or in array form.. which is 200+ subarrays Array ( [0] => Array ( [name] => Weight [value] => ) [1] => Array ( [name] => repetition [value] => ) [2] => Array ( [name] => field_id [value] => h ) [3] => Array ( [name] => set_id [value] => 1 ) [4] => Array ( [name] => exercise_id [value] => 1 ) [5] => Array ( [name] => planned_workout_id [value] => 1 ) [6] => Array ( [name] => Weight [value] => ) [7] => Array ( [name] => repetition [value] => ) Quote Link to comment https://forums.phpfreaks.com/topic/310367-organise-a-multidimensional-array-into-chunks/ Share on other sites More sharing options...
ginerjm Posted March 24, 2020 Share Posted March 24, 2020 Hmmm.... For a 'newbie' this is an awfully complex design you are working on. May I ask how you got yourself into this pickle? Quote Link to comment https://forums.phpfreaks.com/topic/310367-organise-a-multidimensional-array-into-chunks/#findComment-1575745 Share on other sites More sharing options...
Barand Posted March 24, 2020 Share Posted March 24, 2020 What is the structure of the table you want to write this data to? That could well have an influence on the best way to structure the data. What does the raw data coming from JQuery look like? Quote Link to comment https://forums.phpfreaks.com/topic/310367-organise-a-multidimensional-array-into-chunks/#findComment-1575747 Share on other sites More sharing options...
Stoffer Posted March 24, 2020 Author Share Posted March 24, 2020 Hi Brand, The data is a dynamic created form, which is contains a number of input-fields which are dublicated X amount of times, so that the user can enter the progress. It is transferred to my PHP page via jQuery, using the serialise function. The raw data which is transferred can be delivered in 2 different formats, whichever I choose - See the snips below. How I save the data in my database is going to be multiple different tables,I want to normalise my data as much as possible when I store it. Furthermore the data will be enriched by other data sources before I save it the database. Serialised form: Weight=&repetition=&field_id=h&set_id=1&exercise_id=1&planned_workout_id=1&Weight=&repetition=&field_id=h&set_id=2&exercise_id=1&planned_workout_id=1&Weight=&repetition=&field_id=h&set_id=3&exercise_id=1&planned_workout_id=1&Weight=&repetition=&field_id=h&set_id=4&exercise_id=1&planned_workout_id=1&Weight=&repetition=&field_id=h&set_id=1&exercise_id=13&planned_workout_id=1&Weight=&repetition=&field_id=h&set_id=2&exercise_id=13&planned_workout_id=1&Weight=&repetition=&field_id=h&set_id=3&exercise_id=13 or in array form.. which is 200+ subarrays Array ( [0] => Array ( [name] => Weight [value] => ) [1] => Array ( [name] => repetition [value] => ) [2] => Array ( [name] => exercise_id [value] => ) [3] => Array ( [name] => set_id [value] => ) [4] => Array ( [name] => field_id [value] => ) [5] => Array ( [name] => planned_id [value] => ) --- The array continues looping through all the fields about giving me 200+ subarrays [2xx] => Array ( [name] => Weight [value] => ) [2xx] => Array ( [name] => repetition [value] => ) [2xx] => Array ( [name] => exercise_id [value] => ) [2xx] => Array ( [name] => set_id [value] => ) [2xx] => Array ( [name] => field_id [value] => ) [2xx] => Array ( [name] => planned_id [value] => ) Quote Link to comment https://forums.phpfreaks.com/topic/310367-organise-a-multidimensional-array-into-chunks/#findComment-1575751 Share on other sites More sharing options...
Stoffer Posted March 24, 2020 Author Share Posted March 24, 2020 So far I can only transfer the data from jQuery in one of the 2 ways mentioned above Quote Link to comment https://forums.phpfreaks.com/topic/310367-organise-a-multidimensional-array-into-chunks/#findComment-1575752 Share on other sites More sharing options...
Stoffer Posted March 24, 2020 Author Share Posted March 24, 2020 24 minutes ago, Stoffer said: So far I can only transfer the data from jQuery in one of the 2 ways mentioned above I managed to create multiple flat arrays, but struggling to map them into arrays and order them together based on exercise_id > set_id. In this case I would end up with 18 arrays, that contains data from each of the arrays below. Array ( [field_id] => Array ( [0] => h [1] => h [2] => h [3] => h [4] => h [5] => h [6] => h [7] => h [8] => h [9] => h [10] => h [11] => h [12] => h [13] => h [14] => h [15] => h [16] => h [17] => h ) [planned_id] => Array ( [0] => 1 [1] => 1 [2] => 1 [3] => 1 [4] => 1 [5] => 1 [6] => 1 [7] => 1 [8] => 1 [9] => 1 [10] => 1 [11] => 1 [12] => 1 [13] => 1 [14] => 1 [15] => 1 [16] => 1 [17] => 1 ) [exercise_id] => Array ( [0] => 1 [1] => 1 [2] => 1 [3] => 1 [4] => 13 [5] => 13 [6] => 13 [7] => 13 [8] => 13 [9] => 14 [10] => 14 [11] => 14 [12] => 15 [13] => 15 [14] => 15 [15] => 16 [16] => 16 [17] => 16 ) [set_id] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 1 [5] => 2 [6] => 3 [7] => 4 [8] => 5 [9] => 1 [10] => 2 [11] => 3 [12] => 1 [13] => 2 [14] => 3 [15] => 1 [16] => 2 [17] => 3 ) [weight] => Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => ) [reps] => Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => ) ) Quote Link to comment https://forums.phpfreaks.com/topic/310367-organise-a-multidimensional-array-into-chunks/#findComment-1575753 Share on other sites More sharing options...
Barand Posted March 24, 2020 Share Posted March 24, 2020 One way is reorganise your form's field naming convention. Here is an example which will send the data in the format you want, IE Array ( [1] => Array ( [weight] => W1 [repetition] => R1 [field_id] => F1 [exercise] => E1 [planned_workout_id] => P1 ) [2] => Array ( [weight] => W2 [repetition] => R2 [field_id] => F2 [exercise] => E2 [planned_workout_id] => P2 ) [3] => Array ( [weight] => W3 [repetition] => R3 [field_id] => F3 [exercise] => E3 [planned_workout_id] => P3 ) [4] => Array ( [weight] => W4 [repetition] => R4 [field_id] => F4 [exercise] => E4 [planned_workout_id] => P4 ) [5] => Array ( [weight] => W5 [repetition] => R5 [field_id] => F5 [exercise] => E5 [planned_workout_id] => P5 ) ) Example code <?php if ($_SERVER['REQUEST_METHOD']=='POST') { // handle the AJAX request if (isset($_POST['data'])) { exit(print_r($_POST['data'], 1)); // and return the data as the response } } ?> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript"> $().ready( function() { $("#btnSend").click( function() { $.post( "", // send ajax request to self $("#form1").serialize(), function(resp) { $("#output").html(resp) }, "TEXT" ) }) }) </script> </head> <body> <form id="form1"> <?php for ($i=1; $i<=5; $i++) { echo "Weight : <input type='text' name='data[$i][weight]' value='W$i'><br> Repetition : <input type='text' name='data[$i][repetition]' value='R$i'><br> Field_id : <input type='text' name='data[$i][field_id]' value='F$i'><br> Exercise : <input type='text' name='data[$i][exercise]' value='E$i'><br> Planned workout : <input type='text' name='data[$i][planned_workout_id]' value='P$i'><hr> "; } ?> </form> <button id="btnSend">Send</button> <br> <h3>Data received from form:</h3> <textarea cols="50" rows="50" id="output"></textarea> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/310367-organise-a-multidimensional-array-into-chunks/#findComment-1575755 Share on other sites More sharing options...
gizmola Posted March 25, 2020 Share Posted March 25, 2020 Please consider using json for all transfer between your javascript and php. It is the de facto standard way of doing this, and the main way anyone does REST/AJAX etc. these days. With json_decode and json_encode, you can naturally and easily convert between json objects and arrays and php objects and/or arrays. Quote Link to comment https://forums.phpfreaks.com/topic/310367-organise-a-multidimensional-array-into-chunks/#findComment-1575764 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.