如何基于php中的数组键将数组拆分为子数组 [英] how to break an array into sub arrays based on array key in php

查看:74
本文介绍了如何基于php中的数组键将数组拆分为子数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,可以从下面的表单中进行多种表单提交,我必须根据键将该数组拆分为子数组

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] => sam@gmail.com
    [adress_1] => #224,us-west
    [phone_1] => 0144875954
    [city_1] => sanfransico
    [state_1] => us
    [country_1] => us
    [name_4] => ram
    [email_4] => ram@gmail.com
    [adress_4] => #444,india
    [phone_4] => 9844875954
    [city_4] => delhi
    [state_4] => delhi
    [country_5] => india
    [name_5] => jam
    [email_5] => jam@gmail.com
    [adress_5] => #224,cannada
    [phone_5] => 0344875954
    [city_5] => sanfransico
    [state_5] => cannada
    [country_5] => cannada
    [name_7] => kam
    [email_7] => kam@gmail.com
    [adress_7] => #224,us-east
    [phone_7] => 0144875954
    [city_7] => california
    [state_7] => us
    [country_7] => us

)

我想将上面的数组分解成下面的子数组,我的意思是从name_1到country_1一个数组,然后再从name_4到country_4另一个数组,依此类推。.我正在从多个表单提交动态获取此数组

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] => sam@gmail.com
            [adress] => #224,us-west
            [phone] => 0144875954
            [city] => sanfransico
            [state] => sanfransico
            [country] => us
        )

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

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

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

这是我尝试过的:

foreach ($arr as $k_fmt => $v_fmt) { 
    $arr_fetch = explode("_", $k_fmt, 2); 
    $ele_key = $arr_fetch[0]; 
}


推荐答案

您正确启动了,但随后拆分密钥后,您再也不会做任何事情。设置变量不会将其添加到结果数组中。

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
}

这篇关于如何基于php中的数组键将数组拆分为子数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆