PHP多维数组到某些数据的唯一数组但返回多维及其相关数据

时间:2017-05-15 09:35:49

标签: php arrays codeigniter multidimensional-array codeigniter-2

我有一系列农民及其相关作物以及作物相关图像。我希望该数组与农民数组及其相关作物与图像。我必须将独特的农民和相关作物在农民阵列中作为不同的对象。 我使用以下查询:

GIT_WORK_TREE

,结果是

$q = $this->db->select("farmer_master.*, state_master.name as state_name, 
                        district_master.name as district_name, 
                        taluka_master.name as taluka_name, farmer_crop.crop_id 
                        AS crops, farmer_crop.acre AS acres, crop_master.name as 
                        crop_name, GROUP_CONCAT(farmer_crop_images.crop_image) 
                        as crops_images")
             ->from("farmer_master")
            ->join("farmer_crop", "farmer_crop.farmer_id = 
                    farmer_master.id","LEFT")
            ->join("crop_master", "crop_master.id = farmer_crop.crop_id","LEFT")
            ->join("state_master", "state_master.id = 
                    farmer_master.state_id","LEFT")
            ->join("district_master", "district_master.id = 
                    farmer_master.district_id","LEFT")
            ->join("taluka_master", "taluka_master.id = 
                    farmer_master.taluka_id","LEFT")
            ->join("farmer_crop_images", "farmer_crop_images.farmer_crop_id = 
                    farmer_crop.id","LEFT")
            ->where("farmer_master.sales_id", $sales_id)
            ->group_by("farmer_crop_images.farmer_crop_id")
            ->get();

我有一个如下数组:

$q->result_array();

我想要的结果如下:

Array
(
     [0] => Array
            (
                [id] => 1
                [farmer_name] => Mehul
                [mobile] => 8401036474
                [address] => Karanagar road
                [village] => Nagarvel
                [total_acre] => 100
                [state_id] => 1
                [district_id] => 10
                [taluka_id] => 28
                [sales_id] => 43
                [created_at] => 2017-05-15 04:21:09
                [state_name] => gujarat
                [district_name] => bharuch
                [taluka_name] => anklesvar
                [crops] => 4
                [acres] => 15
                [crop_name] => green gram
                [crops_images] => 1494836337726.jpg,1494739175265.jpg
             )

     [1] => Array
            (
               [id] => 1
               [farmer_name] => Mehul
               [mobile] => 8401036474
               [address] => Karanagar road
               [village] => Nagarvel
               [total_acre] => 100
               [state_id] => 1
               [district_id] => 10
               [taluka_id] => 28
               [sales_id] => 43
               [created_at] => 2017-05-15 04:21:09
               [state_name] => gujarat
               [district_name] => bharuch
               [taluka_name] => anklesvar
               [crops] => 3
               [acres] => 70
               [crop_name] => rice
               [crops_images] => 1494836356691.jpg
         )

)

1 个答案:

答案 0 :(得分:1)

您必须首先获取具有唯一

的农民ID
$ids = array_unique(array_column($array, "id"));
//                print_r($ids);exit;           Array ( [0] => 1 )
$farmer_list_new = array();
for($i=0; $i<count($ids); $i++)
{
    $first_time = true;
    $farmer_list_new[$i] = array();
    foreach( $farmer_list as $row )
    {
        if( $ids[$i] == $row['id'] )
        {
            if( $first_time )
            {
                $first_time = false;
                $farmer_list_new[$i] = array(
                    'id'            => $row['id']           ,
                    'farmer_name'   => $row['farmer_name']  ,
                    'mobile'        => $row['mobile']       ,
                    'address'       => $row['address']      ,
                    'village'       => $row['village']      ,
                    'total_acre'    => $row['total_acre']   ,
                    'state_id'      => $row['state_id']     ,
                    'district_id'   => $row['district_id']  ,
                    'taluka_id'     => $row['taluka_id']    ,
                    'sales_id'      => $row['sales_id']     ,
                    'created_at'    => $row['created_at']   ,
                    'state_name'    => $row['state_name']   ,
                    'district_name' => $row['district_name'],
                    'taluka_name'   => $row['taluka_name']  ,
                    'crops'         => array()
                );
            }
            $crop_images = explode(",", $row['crops_images']);
            foreach($crop_images as $img){
                   $crop_images_url[] = $img;
            }
            $img_crops = implode(",", $crop_images_url);
            $farmer_list_new[$i]['crops'][] = array(
                'crop_id'       => $row['crops'],
                'acres'         => $row['acres'],
                'crop_name'     => $row['crop_name'],
                'crops_images'  => $row['crops_images'],
            );
        }
    }
}