dre75230 2013-03-29 08:01
浏览 33
已采纳

将多维数组拆分为两个新数组

I have this multidimensional array:

Array
(
    [userId] => 35
    [fieldId] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [4] => 4
        )

    [educationTitle] => Array
        (
            [0] => School1
            [1] => School2
            [2] => 3
            [4] => 
        )

    [educationDegree] => Array
        (
            [0] => Degree1
            [1] => Degree2
            [2] => 3
            [4] => 
        )

    [startDate] => Array
        (
            [0] => 2013-03-01
            [1] => 2013-03-03
            [2] => 1970-01-01
        )

    [endDate] => Array
        (
            [0] => 2013-03-02
            [1] => 2013-03-04
            [2] => 1970-01-01
        )

    [educationDescription] => Array
        (
            [0] => Description1
            [1] => Description2
            [2] => 
        )

)

And I have an array of ids called matches:

    [matches] => Array
        (
            [0] => 1
            [1] => 2

        )

I need to split the main array into two:

$eduAdd = array()
$eduUpdate = array()

$eduAdd will contain the non-matching fieldId's and $eduUpdate will contain the matching fieldId's.

$eduAdd Would look like this:

Array
    (
        [userId] => 35
        [fieldId] => Array
            (
                [2] => 3
                [4] => 4
            )

        [educationTitle] => Array
            (
                [2] => 3
                [4] => 
            )

        [educationDegree] => Array
            (

                [2] => 3
                [4] => 
            )

        [startDate] => Array
            (

                [2] => 1970-01-01
            )

        [endDate] => Array
            (

                [2] => 1970-01-01
            )

        [educationDescription] => Array
            (

                [2] => 
            )

    )

I tried this, but found out in_array does not work on multidimensional arrays:

foreach($filteredSubmittedData as $filteredUpdates){
    if(in_array($filteredUpdates['fieldId'], $matches)){
        echo "yup";
    }
}

How can I do this?

  • 写回答

2条回答 默认 最新

  • dopgl80062 2013-03-29 08:32
    关注

    Solution

    Considering $main to be your main array and $matches to be your matches array:

    $eduAdd = array();
    $eduUpdate = array();
    $itodel = array();
    foreach ($main['fieldId'] as $i => $v)
        if (isset($matches[$i]) and $matches[$i] == $v)
            $itodel[] = $i;
    
    foreach ($main as $key => $arr) {
        if (!is_array($arr)) continue;
        foreach ($arr as $i => $v) {
            if (in_array($i, $itodel))
                $eduUpdate[$key][$i] = $v;
            else
                $eduAdd[$key][$i] = $v;
        }
    }   
    

    Explanation

    First of all we need to populate an array of indexes that are matched inside $main['fieldId']. Those are the indexes that will be moved to $eduUpdate and that will not be inserted into $eduAdd:

    $itodel = array();
    foreach ($main['fieldId'] as $i => $v)
        if (isset($matches[$i]) and $matches[$i] == $v)
            $itodel[] = $i;
    

    Then we run another foreach loop that will actually split the $main array into the other two. The main condition is if (in_array($i, $itodel)) because if we are watching an index that is inside the ones that should go into $eduUpdate then we should add it to it, otherwise we just insert it into $eduAdd.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 luckysheet
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误