-1

i need your help, i have an array which look like this

Array
(
    [vaccine_id] => 1
)
Array
(
    [vaccine_id] => 1
)
Array
(
    [vaccine_id] => 1
)
Array
(
    [vaccine_id] => 2
)
Array
(
    [vaccine_id] => 2
)
Array
(
    [vaccine_id] => 2
)
Array
(
    [vaccine_id] => 12
)
Array
(
    [vaccine_id] => 12
)
Array
(
    [vaccine_id] => 12
)

and i need to convert it so it can look some thing like this so that array with the same key and value should not repeat. e.g

Array
(
    [vaccine_id] => 1
)
Array
(
    [vaccine_id] => 2
)
Array
(
    [vaccine_id] => 12
)

my code looks like this. i am using codeigniter framework.

$data['vaccManagment'] = $this -> Common_model -> get_info('manage_epi_vacc', '', '','*', array('uncode' => $this -> uri -> segment(3), 'fmonth' => $this -> uri -> segment(4)));
        $vaccManagmentDetail = $this -> Common_model -> fetchall('manage_epi_vacc_items_record', '','vaccine_id', array('uncode' => $this -> uri -> segment(3), 'fmonth' => $this -> uri -> segment(4),'manage_vacc_id'=>$data['vaccManagment']->recid));
        $newDetail = array();
        foreach($vaccManagmentDetail as $detail){
            print_r($detail);
        }exit;

Thanks for your interest.

K. Uzair
  • 303
  • 3
  • 16
  • 1
    Can you show the code producing those arrays, and printing it? – Martin Dec 30 '15 at 07:42
  • yes sure i am doing it some thing like this. i am using codeigniter structure $data['vaccManagment'] = $this -> Common_model -> get_info('manage_epi_vacc', '', '','*', array('uncode' => $this -> uri -> segment(3), 'fmonth' => $this -> uri -> segment(4))); $vaccManagmentDetail = $this -> Common_model -> fetchall('manage_epi_vacc_items_record', '','vaccine_id', array('uncode' => $this -> uri -> segment(3), 'fmonth' => $this -> uri -> segment(4),'manage_vacc_id'=>$data['vaccManagment']->recid)); $newDetail = array(); foreach($vaccManagmentDetail as $detail){ print_r($detail); }exit; – K. Uzair Dec 30 '15 at 07:45
  • Please edit the question so we can read it :) – Martin Dec 30 '15 at 07:52
  • 2
    [This should solve your problem](http://stackoverflow.com/questions/307674/how-to-remove-duplicate-values-from-a-multi-dimensional-array-in-php) – Basheer Kharoti Dec 30 '15 at 08:07

2 Answers2

2

you have to use array_unique

Refer:

http://php.net/manual/en/function.array-unique.php

Anto S
  • 2,448
  • 6
  • 32
  • 50
  • 1
    Thanks for you interest. but, array_unique — Removes duplicate values from an array. In may case arrays are different but there key and values are matching. – K. Uzair Dec 30 '15 at 07:51
1

If you want to combine them and have unique on base of values then your code should update like this:

$data['vaccManagment'] = $this -> Common_model -> get_info('manage_epi_vacc', '', '','*', array('uncode' => $this -> uri -> segment(3), 'fmonth' => $this -> uri -> segment(4)));
$vaccManagmentDetail = $this -> Common_model -> fetchall('manage_epi_vacc_items_record', '','vaccine_id', array('uncode' => $this -> uri -> segment(3), 'fmonth' => $this -> uri -> segment(4),'manage_vacc_id'=>$data['vaccManagment']->recid));
$arr= array_column($vaccManagmentDetail, "vaccine_id");
print_r(array_unique($arr));      
exit;

note, Removed your foreach code and added two line code that is:

$arr= array_column($vaccManagmentDetail, "vaccine_id");
print_r(array_unique($arr));exit; 

Output should look like this:

array([0]=>1,[3]=>2,[6]=>12)
Imran Qamer
  • 2,253
  • 3
  • 29
  • 52