猿问

如何根据条件复制多维子数组值?

我有一个array这样的:


Array

(

[05049207256] => Array

    (

        [0] => Array

            (

                [ID_NUMBER] => 05049207256

                [CID] => 29113082

                [GROUP_ID] => 

                [B_CODE] => 10152/6277

            )


        [1] => Array

            (

                [ID_NUMBER] => 05049207256

                [CID] => 29093574

                [GROUP_ID] => 

                [B_CODE] => 10241/6244

            )


    )


[16992375298] => Array

    (

        [0] => Array

            (

                [ID_NUMBER] => 16992375298

                [CID] => 29112537

                [GROUP_ID] => 398242

                [B_CODE] => 10152/6277

            )


        [1] => Array

            (

                [ID_NUMBER] => 16992375298

                [CID] => 29112169

                [GROUP_ID] => 398223

                [B_CODE] => 10152/6276

            )


    )


[21871540019] => Array

    (

        [0] => Array

            (

                [ID_NUMBER] => 21871540019

                [CID] => 29112605

                [GROUP_ID] => 398048

                [B_CODE] => 10152/6277

            )


        [1] => Array

            (

                [ID_NUMBER] => 21871540019

                [CID] => 29113036

                [GROUP_ID] => 398040

                [B_CODE] => 10152/6277

            )


        [2] => Array

            (

                [ID_NUMBER] => 21871540019

                [CID] => 29112733

                [GROUP_ID] => 398039

                [B_CODE] => 10152/6277

            )


        [3] => Array

            (

                [ID_NUMBER] => 21871540019

                [CID] => 29110803

                [GROUP_ID] => 398039

                [B_CODE] => 10152/6275

            )


    )

拉丁的传说
浏览 158回答 3
3回答

沧海一幻觉

我的方法是这样的:$result = […]; // your input dataforeach( $result as $key => $item ) {  // get group IDs for current item  $group_ids = array_column( $item, 'GROUP_ID' );  // if count is 1, remove item  if( count( $group_ids ) == 1 ) {      unset( $result[$key] );  }  else {    // get only unique group IDs    $group_ids_unique = array_unique( $group_ids );    // is there is only one unique group ID, and it is empty?    if( count( $group_ids_unique ) == 1 && !$group_ids_unique[0] ) {      // do nothing    }    else {        // get one “random” group id - either the 1st, or if that is empty, the 2nd one        $group_id_set = $group_ids_unique[0] ? $group_ids_unique[0] : $group_ids_unique[1];        // set that group ID for all entries in current item        foreach( $item as $sub_key => $sub_value ) {            $result[$key][$sub_key]['GROUP_ID'] = $group_id_set;        }    }  }}为了清楚起见,我把 the// do nothing if留在那里——当然,也可以重写它,否定整个条件,并使当前的 else 分支编码为 if 分支。

墨色风雨

这是一个简单的解决方案,仅使用array_columnforeach($result as $key => $array){    if(count($array) >= 2){        $group_ids = array_column($array,'GROUP_ID');//get all group_ids        foreach($group_ids as $k => $v){            if(!empty($v)){//if there is non-empty one change the values and break the loop                foreach($array as $key2 => $array2){                    $result[$key][$key2]['GROUP_ID'] = $v;                }                break;            }        }    }else{        unset($result[$key]);    }}

大话西游666

foreach ($result as $key => $val) {&nbsp; &nbsp; &nbsp; &nbsp; if (count(array_keys($val)) == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unset ($result[$key]);&nbsp; &nbsp; &nbsp; &nbsp; } else if (count(array_keys($val)) >= 2/* && empty($result[$key][$key1]['GROUP_ID'])*/) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /*$assign_value = $val[0]['GROUP_ID'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $result[$key][$key1]['GROUP_ID'] = $assign_value;*/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $GROUP_ID = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ($index = 0; ($GROUP_ID) && ($index < count($val)); $index++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((isset($val["GROUP_ID"])) && (!!$val["GROUP_ID"])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $GROUP_ID = $avl["GROUP_ID"];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!!$GROUP_ID) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ($index = 0; $index < count($val); $index++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $val["GROUP_ID"] = $GROUP_ID;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }}删除了 if 周围的内部 for 循环if 不需要在它周围使用循环,我们可以确定数组内的项目数而无需循环它们。第二个if修改为else-if虽然在这种情况下,如果第一个if为真,,那么第二个将是false,这在形式上是正确的,但最好使其正式化以获得更好的可读性。我不检查 else 中 GROUP_ID 是否为空无论子数组的第一个元素是否具有空 GROUP_ID,第二个元素都可能非空。因此,您不需要检查子数组中的第一个项目是否具有非空 GROUP_ID,而是需要检查子数组中的任何项目是否为非空,因为这是真实条件检查是否有非空GROUP_ID需要循环初始化后,$GROUP_ID我们不知道子数组中是否至少有一个非空的 GROUP_ID。因此,我们循环遍历子数组,如果找到非空 GROUP_ID,则记录下来并停止循环。确定是否需要覆盖 GROUP_ID如果$GROUP_ID为 truey,则意味着我们已经覆盖了它的初始值,即null,或者换句话说,这意味着我们找到了一个非空的 GROUP_ID。覆盖元素在最后一个内部,如果我们将子数组中的所有 GROUP_ID 值覆盖为我们找到的值。
随时随地看视频慕课网APP
我要回答