在foreach循环中使用array_combine将多个数组转换为单个数组

时间:2017-07-08 01:56:18

标签: php arrays foreach

我正在尝试将array_combine转换为具有唯一值的单个数组。

$arr_ch_no = array();
        $arr_ch_no[] = "'***0***'";
        $sql = mysql_query("SELECT ship_name,voy_no from tb_veh_data_entry WHERE (ETAQ > '".date("Y-m-d")."' or (ETAQ <> '0000-00-00' and stock = 0 and stock_sale_date = '0000-00-00')) and (user_id = 'UI08-00000063' or user_id = 'UI09-00000111') and stock_country = 58 and ((buyer in ('6371','2509','2535') and arica = 0 ) or arica=4)  group by ship_name,voy_no  ");
        while($row_no = mysql_fetch_array($sql))
        {
            $str_no_ch = selFieldCon("tb_veh_data_entry","GROUP_CONCAT(frame_no)"," ship_name = '".$row_no['ship_name']."' and voy_no = '".$row_no['voy_no']."'   and (user_id = 'UI08-00000063' or user_id = 'UI09-00000111') and stock_country = 58   and ((buyer in ('6371','2509','2535') and arica = 0 ) or arica=4) and ETAQ <> '0000-00-00' ".$this->condition."");
            $arr_no_ch = array();
            $arr_no_ch = explode(",",$str_no_ch);
            if($str_no_ch != '')
            foreach($arr_no_ch as $val)
                $arr_ch_no[] = "'".$val."'";
        }

此查询用于 foreach循环

$sql    =    "SELECT * FROM tb_veh_data_entry WHERE 1 ".$this->condition." ";

我在foreach中尝试这个。

foreach loop //start

array_unique(array_combine($arr_ch_no));

如何将它转换为foreach中的单个数组。

Array
    (
        [0] => '***0***'
        [1] => 'NCP100-0027131'
    )
    Array
    (
        [0] => '***0***'
        [1] => 'NCP100-0027131'
    )
    Array
    (
        [0] => '***0***'
        [1] => 'NCP100-0027131'
    )

但我什么都没得到。

1 个答案:

答案 0 :(得分:-1)

这不是好办法。希望它可以帮助你。

$data1 = array_column($array, 0, 1);
$data2 = array_column($array, 1, 0);

$data1Length = count($data1);
$data2Length = count($data2);

$result = [];
if($data1Length > $data2Length) {
    foreach($data1Length as $k => $v) {
        $result[] = [$v, $k];
    }
} else {
    foreach($data1Length as $k => $v) {
        $result[] = [$k, $v];
    }
}