删除另一个数组中的重复项

时间:2019-07-28 14:36:53

标签: php arrays wordpress

它看起来像每个用户的数组。每个用户都有一个数组,其中写入了JSON字符串,并且如果某处重复了某个项目,则必须将其删除或更改其类型,因为没有必要输出它,因为最后您会需要保存一切,无论在游戏中选择了谁。主要结果,如果重复两次,则必须将输出写一次。

例如,一个用户拥有一个这样的数组。

  array (size=2)
0 => string '"{"cards1":["Study","Computer",null],"cards2":["Study
","notebook",null],"cards3":["Study","Pen",null]}"' (length=201)
1 => string '"{"cards1":["Sport","Speed",null],"cards2":["Sport","Power",null],"cards3":["Sport","Reaction",null]}"' (length=201)

在第二个用户中。

 array (size=2)
0 => string '"{"cards1":["Study","Book",null],"cards2":["Study
","eraser",null],"cards3":["Study","Pen",null]}"' (length=201)
1 => string '"{"cards1":["Sport","Speed",null],"cards2":["Sport","Power",null],"cards3":["Sport","Endurance",null]}"' (length=201)

有必要这样做,以便在重复某些操作时仅输出一次。例如,对于两个用户,“研究”部分中重复的“笔”,因此,我们只显示一次。在“运动”部分,他们重复了速度和力量=>我们推论了一次。 P.s.用户可以是两个,三个和十个。我的代码看起来像这样

$company_name =  get_user_meta(  $cur_user_id, 'company' );
$params = array(
    'meta_query' => array(
        array( 
            'key'     => 'company',
            'value'   => $company_name[0],

        ),
    )
 );
// threw the necessary users into the array.$uq = new WP_User_Query( $params );
if ( ! empty( $uq->results ) ) {
// take the first user from the array
    foreach ( $uq->results as $k=>$u ) {
        echo '<p>' . $u->ID . 'dddd</p>';
        echo '<p>' . $u->user_email . '</p>';
        $array =  get_user_meta(  $u->ID, 'json' );

// I try to take the second user from the array, but does not go        
           foreach ( $uq->results as $k=>$u_next ) {
            $uq->results[$k+1];
            echo '<p>' . $u_next->ID . 'dsfdsfdfs</p>';
            echo '<p>' . $u_next->user_email . '</p>';
            $array_next =  get_user_meta(  $u_next->ID, 'json' );
            // take the array of the first user and decode this JSON
            foreach ($array as $key => $value) {
                $array_first = (json_decode (trim ($value, '"'), true)); 
               // take an array of the second user and decode this JSON   
                foreach ($array_next as $key => $value) {
                    $array_second = (json_decode (trim ($value, '"'), true)); 
                    // I try to compare with each other and, if it repeats, delete it and then output it, but it seems to me that it is too early to make a conclusion, because it is necessary for him to go through all users ...
                    if($array_first['cards1'][1] == $array_second['cards1'][1]){

                        unset($array_next[$key]);
                    }else if($array_first['cards1'][2] == $array_second['cards1'][2]){
                        unset($array_next[$key]);

                    }else if($array_first['cards2'][1] == $array_second['cards2'][1]){
                        unset($array_next[$key]);

                    }else if($array_first['cards2'][2] == $array_second['cards2'][2]){
                        unset($array_next[$key]);

                    }else if($array_first['cards3'][1] == $array_second['cards3'][1]){
                        unset($array_next[$key]);

                    }else if($array_first['cards3'][2] == $array_second['cards3'][2]){
                        unset($array_next[$key]);

                    }   
                    ?>
  <div class="category-result-title"><?php echo $array_second['cards1'][0] ?></div> 
                        <div class="container-result">
                            <div class="hexagon hexagon-center-result">
                                <div class="wrap"><?php echo $array_second['cards1'][1];?></div>

                                </div>
                            <div class="hexagon hexagon-center-result">
                                <div class="wrap"><?php echo $array_second['cards2'][1];  ?></div>

                            </div>
                            <div class="hexagon hexagon-center-result">
                                <div class="wrap"><?php echo $array_second['cards3'][1];?></div>
                            </div>
                        </div>
                        <?php 
                    }
                }

            }   
    }


我的问题是有用户,每个用户在数组中都有信息。在每个单元格的此数组中存储字符串JSON。我需要将所有这些显示在屏幕上,而无需重复。一家公司有X位用户。他们玩了游戏。例如,在“运动”部分中,我们选择了一对完全相同的结果。有必要显示所有用户的结果,但是事实是用户具有相同的结果,那么只能撤回一次

这是结论

Computer,notebook, pen , speed, power,reaction

Book, eraser,Endurance

1 个答案:

答案 0 :(得分:0)

我可能会建议您遍历数组,将所有重复的内容放入一个简单的数组中,这样您就可以了解该数组的实际含义,假设其名称为$ MyNestedArray;然后启动一个新数组,名称为$ MyCleanArray;遍历$ MyNestedArray,查看当前elemetn是否存在于$ MyCleanArray中,如果不存在,则添加它,如果存在,则跳到下一个元素,类似这样

for($i=0;$i<sizeof($MyNestedArray);$i++)
{
for($j=0;$j<sizeof($MyCleanArray);$j++)
{
if($MyNestedArray[$i] != $MyCleanArray[$j])
$MyCleanArray[] = $MyNestedArray[$i];
}
}

正如其他人可能注意到的那样,您的问题不清楚,我只是回答了我所了解的内容,希望对您有所帮助