简体   繁体   中英

PHP multi dimensional array to unique array of some data but return multi dimensional with its related data

I have an array of farmer and their related crops and also crop related images. I want that array to farmer array with their related crops with image. I have to unique farmer and related crops can be different object in farmer array. I use below query:

$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();

and result is

$q->result_array();

I have an array like below:

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
         )

)

And my desired result like below:

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] => Array
                (
                    [0] => Array
                        (
                            [crop_id] => 4
                            [acres] => 15
                            [crop_name] => green gram
                            [crops_images] => 1494836337726.jpg,1494739175265.jpg
                        )

                    [1] => Array
                        (
                            [crop_id] => 3
                            [acres] => 70
                            [crop_name] => rice
                            [crops_images] => 1494836356691.jpg
                        )

                )

        )
  )

You have to first take get id of farmer with unique

$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'],
            );
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM