开发者

Find value assoaction in array

i have a multi dimension array with sub array having repeated values of 'eduHisRowId' like:

Array
(
    [0] => Array
        (
            [eduHisRowId] => 4
            [repOrderId] => 15
        )

    [1] => Array
        (
            [eduHisRowId] => 5
            [repOrderId] => 16
        )

    [2] => Array
        (
            [eduHisRowId] => 5
            [repOrderId] => 17
        )

    [3] => Array
        (
            [eduHisRowId] => 6
            [repOrderId] => 18
        )

    [4] => Array
        (
            [eduHisRowId] => 7
            [repOrderId] => 19
        )

    [5] => Array
        (
            [eduHisRowId] => 7
            [repOrderId] => 20
        )

    [6] => Array
        (
            [eduHisRowId] => 8
            [repOrderId] => 21
        )

)

Now i want sort out these repeated values such that i could be able to check that the record present on index '[1] => Array' is associated with the record which is present on index '[2] => Array' & this associated relation will also be in array format like:

Array
    (
        [0] => Array
            (    
                [0] => 4
                [1] => Array
                    (
                        [0] => 15
                    ) 
            )

        [1] => Array
            (
                [0] => 15
                [1] => Array
                    (
                        [0] => 16
                        [0] => 17
                    )
            )

        [2] => Array
            (
                [0] => 6
                [1] => Array
                    (
                        [0] => 18
                    )
            )

        [3] => Array
            (
                [0] => 7
                [1] => Array
                    (
                        [0] => 19
                        [0] => 20
                    )
            )

        [4] => Array
            (
                [0] => 8
                [1] => Array
                    (
                        [0] =>开发者_如何学C 21
                    )
            )

    )

where 0th index of innre mos array will contain 'eduHisRowId' value & the array on 1st index will contain 'repOrderId' values.

Thanks in advance...


Can I suggest a different solution? What about an array structure that looks like:

Array
(
    [4] => Array
           (
              [0] => 15
           ) 

    [5] => Array
           (
               [0] => 16
               [1] => 17
           )
 )

The keys are the eduHisRowId values and the value is an array of corresponding repOrderId values.

Creating this array would go like follows:

function consolidate($item, $key, $array) {
     $rowId = $item['eduHisRowId'];
     if(!array_key_exists($rowId, $array)) {
         $array[$rowId] = array();
     }
     $array[$rowId][] = $item['repOrderId'];
}

$result = array();

array_walk($dataArray, 'consolidate', &$result);

$dataArray is your multidimensional array, the resulting array is in $result.

Reference: array_walk(), array_key_exists()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜