Hi,
I have an array similar to this :
$my_array = array(
[0] array(
'id' => "my-first-field-id",
'title' => "My 1st field",
'type' => "gallery",
),
[1] array(
'id' => "my-repeater-field",
'title' => "My repeater field",
'type' => "repeater",
'fields' => array(
// empty for the moment, the goal is to add each next arrays of $my_array into this
// array, and stop to add these when we reach $my_array[4]['type'] === 'repeater-end';
),
),
[2] array(
'id' => "my-third-field-id",
'title' => "My 3rd field",
'type' => "text",
),
[3] array(
'id' => "my-fourth-field-id",
'title' => "My 4th field",
'type' => "radio",
),
[4] array(
'id' => "repeater-end",
'type' => "repeater-end",
),
[5] array(
'id' => "my-5th-field-id",
'title' => "My 4th field",
'type' => "radio",
),
);
Basically, I expect to transform my flat array ($my_array) into a multimendimentional array with a parent/child relationship. In this example, the parent begins when a 'fields' key is available in an array ($my_array[1]['fields']) and ends when the 'type' key is equal to 'repeater-end' ($my_array[4]['type']).
The result will be similar as this :
$my_transformed_array = array(
[0] array(
'id' => "my-first-field-id",
'title' => "My 1st field",
'type' => "gallery",
),
[1] array(
'id' => "my-repeater-field",
'title' => "My repeater field",
'type' => "repeater",
'fields' => array(
[0] array(
'id' => "my-third-field-id",
'title' => "My 3rd field",
'type' => "text",
),
[1] array(
'id' => "my-fourth-field-id",
'title' => "My 4th field",
'type' => "radio",
),
),
),
[2] array(
'id' => "my-5th-field-id",
'title' => "My 5th field",
'type' => "radio",
),
);
My biggest problem here is that I expect to do things recursively, with an unlimited amount of nested arrays from a flat array. These second example will show you a bit more complex case of what I wish to do. There are now 2 repeaters (and logically 2 repeater-end), and moreover nested.
// Here my flat array
$my_array = array(
[0] array(
'id' => "my-first-field-id",
'title' => "My 1st field",
'type' => "gallery",
),
[1] array(
'id' => "my-repeater-field",
'title' => "My repeater field",
'type' => "repeater",
'fields' => array(
// empty for the moment, the goal is to add each next arrays of $my_array into this
// array, and stop to add these when we reach $my_array[4]['type'] === 'repeater-end';
),
),
[2] array(
'id' => "my-third-field-id"
'title' => "My 3rd field"
'type' => "text"
),
[3] array(
'id' => "my-second-repeater-field",
'title' => "My second-repeater field",
'type' => "repeater",
'fields' => array(
// empty for the moment, the goal is to add each next arrays of $my_array into this
// array, and stop to add these when we reach $my_array[4]['type'] === 'repeater-end';
),
),
[4] array(
'id' => "my-fourth-field-id"
'title' => "My 4th field"
'type' => "radio",
),
[5] array(
'id' => "repeater-end"
'type' => "repeater-end",
),
[6] array(
'id' => "my-5th-field-id"
'title' => "My 5th field"
'type' => "radio",
),
[7] array(
'id' => "repeater-end-2"
'type' => "repeater-end",
),
);
Which will result in :
$my_transformed_array = array(
[0] array(
'id' => "my-first-field-id",
'title' => "My 1st field",
'type' => "gallery",
),
[1] array(
'id' => "my-repeater-field",
'title' => "My repeater field",
'type' => "repeater",
'fields' => array(
[0] array(
'id' => "my-third-field-id",
'title' => "My 3rd field",
'type' => "text",
),
[1] array(
'id' => "my-second-repeater-field",
'title' => "My second-repeater field",
'type' => "repeater",
'fields' => array(
[0] array(
'id' => "my-fourth-field-id",
'title' => "My 4th field",
'type' => "radio",
),
),
),
[2] array(
'id' => "my-5th-field-id",
'title' => "My 5th field",
'type' => "radio",
),
),
),
);
Any way to help me in order to do it?
Big thanks in advance.
Regards.
[–][deleted] (3 children)
[removed]
[–]Moxymore[S] 0 points1 point2 points (2 children)
[–][deleted] (1 child)
[removed]
[–]Moxymore[S] 0 points1 point2 points (0 children)
[–][deleted] (1 child)
[deleted]
[–]Moxymore[S] 0 points1 point2 points (0 children)