如何将数组重复值添加到单个值

时间:2016-10-27 10:42:49

标签: php arrays

这是我的阵列..

enter image description here

你可以看到0和2数组索引具有相同的年份并且生成值所以我想要它在单个中并且还检查数组是否已经具有相同年份并且生成名称然后模型值将仅添加单个。

我的代码..

<?php
            global $wpdb;
            $get_make_result = $wpdb->get_results("SELECT * FROM `wp_make_details` WHERE `post_id` = ".$post_id."");
    /*      echo "<pre>";
            print_r($get_make_result);
*/


            if(isset($get_make_result)){

             ?>
                <h3 id="head_mmy">Selected Make, Year and Model List.</h3>
                <table class="child_class_make">
                    <tr>
                        <th class="hedi">Make</th>
                        <th class="hedi">Year</th>
                        <th class="hedi">Model</th>
                    </tr>

                    <tr>
                    <?php


                        $results = array();
                        foreach ($get_make_result as $k => $val_make) {
                                  $results[implode($val_make)] = $val_make;

                            $str = preg_replace('/(?<!\d),|,(?!\d{3})/', ', ', $val_make->model_name);
                    ?>
                            <td><?php echo $val_make->make_name; ?></td>
                            <td><?php echo $val_make->year; ?></td>

                            <td><textarea id="text_model" rows="4" cols="30"><?php echo $str;?></textarea></td>
                    <? }
                    $results = array_values($results);
                echo "<pre>";
                print_r($results);
                } ?>

输出

(
    [c_id] => ..
    [post_id] => 15563
    [make_name] => Zero
    [year] => 2017
    [model_name] => S,SR,DSR,DS,FX,FXS
)

2 个答案:

答案 0 :(得分:0)

试试这个

$results = array();

foreach ($array as $k => $v) {
  $results[implode((array)$v)] = $v;
}

$results = array_values($results);
print_r($results);

样本

https://ideone.com/rJZRea

答案 1 :(得分:0)

使用循环,您可以将具有相似值(重复项)的数据捆绑在一起:Quick-Test Here:

<?php

    function sortByNameYear($arr){
        $return = $result = [];
        foreach($arr as $key=>&$val){
            if(!array_key_exists($val->year, $result)){
                $result[$val->year] = [];
                if(!array_key_exists($val->make_name, $result[$val->year])){
                    $result[$val->year][$val->make_name] = [];
                }
            }
            $result[$val->year][$val->make_name][] = $val;
        }

        foreach($result as $year=>&$arrMake){
            foreach($arrMake as $iKey=>&$arrData){
                foreach($arrData as $iK=>$data){
                    if(count($arrData)>1) {
                        $tmp                    = $arrData[0];
                        if($iK !=0){
                            $tmp->c_id          = $arrData[0]->c_id . ", " . $data->c_id;
                            $tmp->post_id       = $arrData[0]->post_id . ", " . $data->post_id;
                            $tmp->model_name    = $arrData[0]->model_name . ", " . $data->model_name;
                            $return[]           = $tmp;
                        }
                    }else{
                        $return[]               = $data;
                    }
                }
            }
        }
        return $return;
    }

    var_dump(sortByNameYear($arr));

上面的var_dump(sortByNameYear($arr))会产生类似的结果:

    array (size=4)
      0 => 
        object(stdClass)[3]
          public 'c_id' => string '89, 78' (length=6)
          public 'post_id' => string '15563, 15563' (length=12)
          public 'make_name' => string 'Zero' (length=4)
          public 'year' => string '2017' (length=4)
          public 'model_name' => string 'S, SR, DSR, DS, FX, FXS, SR, DS' (length=31)
      1 => 
        object(stdClass)[2]
          public 'c_id' => int 90
          public 'post_id' => int 15563
          public 'make_name' => string 'Yamaha' (length=6)
          public 'year' => string '2015' (length=4)
          public 'model_name' => string 'YZF-RIM, YZF-R3, FZ-09, FXZ-012' (length=31)
      2 => 
        object(stdClass)[6]
          public 'c_id' => string '80' (length=6)
          public 'post_id' => int 15563
          public 'make_name' => string 'Zero' (length=4)
          public 'year' => string '2015' (length=4)
          public 'model_name' => string 'S, SR' (length=5)
      3 => 
        object(stdClass)[5]
          public 'c_id' => int 79
          public 'post_id' => int 15563
          public 'make_name' => string 'Yamaha' (length=6)
          public 'year' => string '1999' (length=4)
          public 'model_name' => string 'TTR250L' (length=7)