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

时间:2023-01-16 12:42:50

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:

我有一系列称为匹配的ID:

    [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将包含不匹配的fieldId,而$ eduUpdate将包含匹配的fieldId。

$eduAdd Would look like this:

$ eduAdd看起来像这样:

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:

我试过这个,但发现in_array对多维数组不起作用:

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

How can I do this?

我怎样才能做到这一点?

2 个解决方案

#1


1  

Solution

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

考虑$ main是你的主数组,$ match是你的匹配数组:

$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:

首先,我们需要填充$ main ['fieldId']内匹配的索引数组。这些是将移动到$ eduUpdate的索引,并且不会插入到$ 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.

然后我们运行另一个foreach循环,实际上将$ main数组拆分为另外两个。主要条件是if(in_array($ i,$ itodel))因为如果我们正在观察一个应该进入$ eduUpdate的索引,那么我们应该将它添加到它,否则我们只需将其插入到$ eduAdd中。

#2


1  

$filteredUpdates['fieldId'] itself is an array & as in_array needs a haystack it won't work as you expect. Try changing your if condition as,

$ filteredUpdates ['fieldId']本身就是一个数组,因为in_array需要一个干草堆,它不会像你期望的那样工作。尝试将if条件更改为,

if(array_intersect($filteredUpdates['fieldId'], $matches)){

#1


1  

Solution

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

考虑$ main是你的主数组,$ match是你的匹配数组:

$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:

首先,我们需要填充$ main ['fieldId']内匹配的索引数组。这些是将移动到$ eduUpdate的索引,并且不会插入到$ 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.

然后我们运行另一个foreach循环,实际上将$ main数组拆分为另外两个。主要条件是if(in_array($ i,$ itodel))因为如果我们正在观察一个应该进入$ eduUpdate的索引,那么我们应该将它添加到它,否则我们只需将其插入到$ eduAdd中。

#2


1  

$filteredUpdates['fieldId'] itself is an array & as in_array needs a haystack it won't work as you expect. Try changing your if condition as,

$ filteredUpdates ['fieldId']本身就是一个数组,因为in_array需要一个干草堆,它不会像你期望的那样工作。尝试将if条件更改为,

if(array_intersect($filteredUpdates['fieldId'], $matches)){