0

I have the array below which I get from multiple form submission, i have to split this array into sub arrays based on the keys

My array which i get from multiple form submission is:

Array
(
    [name_1] => sam
    [email_1] => [email protected]
    [adress_1] => #224,us-west
    [phone_1] => 0144875954
    [city_1] => sanfransico
    [state_1] => us
    [country_1] => us
    [name_4] => ram
    [email_4] => [email protected]
    [adress_4] => #444,india
    [phone_4] => 9844875954
    [city_4] => delhi
    [state_4] => delhi
    [country_5] => india
    [name_5] => jam
    [email_5] => [email protected]
    [adress_5] => #224,cannada
    [phone_5] => 0344875954
    [city_5] => sanfransico
    [state_5] => cannada
    [country_5] => cannada
    [name_7] => kam
    [email_7] => [email protected]
    [adress_7] => #224,us-east
    [phone_7] => 0144875954
    [city_7] => california
    [state_7] => us
    [country_7] => us

)

i want to break above array into sub arrays like below,i mean from name_1 to country_1 one array and again name_4 to country_4 another array like so on.. i am getting this array dynamically from multiple form submission

Array
(
    [0] => Array
        (
            [name] => sam
            [email] => [email protected]
            [adress] => #224,us-west
            [phone] => 0144875954
            [city] => sanfransico
            [state] => sanfransico
            [country] => us
        )

    [1] => Array
        (
            [name] => ram
            [email] => [email protected]
            [adress] => #444,india
            [phone] => 9844875954
            [city] => delhi
            [state] => delhi
            [country] => india
        )

    [2] => Array
        (
            [name] => jam
            [email] => [email protected]
            [adress] => #224,cannada
            [phone] => 0344875954
            [city] => sanfransico
            [state] => cannada
            [country] => cannada
        )

    [3] => Array
        (
            [name] => kam
            [email] => [email protected]
            [adress] => #224,us-east
            [phone] => 0144875954
            [city] => california
            [state] => us
            [country] => us
        )
)

This is what I have tried:

foreach ($arr as $k_fmt => $v_fmt) { 
    $arr_fetch = explode("_", $k_fmt, 2); 
    $ele_key = $arr_fetch[0]; 
}
12
  • I assume [Regions] is a typo? Commented Mar 10, 2015 at 16:21
  • 1
    Loop over the array, splitting the key on the _ character with explode. Use the second part as the index into the new array, and the first part as the key of the associative array element. What's the problem? Commented Mar 10, 2015 at 16:22
  • @Barmar yes i have edited it ..i am not getting can you plz show me in coding Commented Mar 10, 2015 at 16:32
  • See stackoverflow.com/questions/26954978/php-group-array-by-key for something kind of similar. Commented Mar 10, 2015 at 16:37
  • @Barmar this is what i tried.. foreach ($arr as $k_fmt => $v_fmt) { $arr_fetch = explode("_", $k_fmt, 2); $ele_key = $arr_fetch[0]; } Commented Mar 10, 2015 at 16:38

2 Answers 2

1

You started correctly, but then you never did anything after splitting up the key. Setting a variable won't add it to the result array.

$new_arr = array();
foreach ($arr as $k_fmt => $v_fmt) {
    $arr_fetch = explode("_", $k_fmt, 2); // Split up they key at the _ character
    $ele_key = arr_fetch[0];
    $ele_index = arr_fetch[1] - 1; // Because original keys start at 1, not 0
    if (!isset($new_arr[$ele_index])) { // Create sub-array if necessary
        $new_arr[$ele_index] = array();
    }
    $new_arr[$ele_index][$ele_key] = $v_fmt; // Use the split up key as the indexes in 2-dimensional result
}
Sign up to request clarification or add additional context in comments.

Comments

0

Ultimately, I feel this question is an XY Problem where the application is missing a good opportunity to better structure the data.

To work around this suboptimal design, parse the keys and use the isolated substrings to construct the new, deeper structure.

PHP arrays do not need to be declared before pushing data into them -- so long as you use [] push syntax.

$result = [];
foreach ($array as $key => $value) {
    sscanf("%[^_]_%d", $key, $word, $id);
    $result[$id][$word] = $value;
}
var_export(array_values($result));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.