0

If using array_unique in php codeigniter, it returns only on value. please give an guideline to solve the issue, I'm new to programming. I'm code is below.

Controllers    
function get_rck_detail()
{
$partnumber = $this->input->post('part_number');
$subbrand = $this->input->post('subbrand');
$part = $this->suggest->get_part_code($partnumber);
$rack_list = $this->suggest->get_rack_details($part,$subbrand);
echo '<option>--Location--</option>';
foreach(array_unique($rack_list) as $rack)
{

echo '<option value="'.$rack['rs_loc'].'">'.$rack['rack_name'].'</option>';
}
} 

models

function get_rack_details($part,$subbrand)
 {
$this->db->select('*');
$this->db->from('rack_spares');
$this->db->join('rack_name','rack_spares.rs_loc = rack_name.rn_id');
$this->db->where('rs_product_code',$part);
$this->db->where('rs_sub',$subbrand);
$query = $this->db->get();
return $query->result_array();
}
2
  • $rack_list is not single dimensional array. It's two dimensional array. Commented Feb 10, 2014 at 11:17
  • how to use remove the duplicate entries for this multi dimension array Commented Feb 10, 2014 at 11:38

2 Answers 2

1

array_unique compares elements as strings. All arrays are cast to the string "Array" and are therefore all equal in the eyes of array_unique.

Try passing SORT_REGULAR as the second parameter, this should compare the arrays as arrays, ie. returning them as equal if they have the same key/value pairs in the same order. You may need to ksort your arrays first.

Sign up to request clarification or add additional context in comments.

5 Comments

yes i use it. but it's not working display all values
Are your arrays, in fact, unique? Or just the name?
it's not just a name, it's an array value of total row in database
What I'm asking is, are the rows completely identical?
yes sir, actually the output of array like [0] => Array ([rs_id] => 21[rs_product_code] => PRD001[rs_sub_id] => 3[rs_sub] => BRUCE[rs_loc] => 1[rs_ser] => AA[rs_num] => 411[rck_quan] => 0[rs_created_date] => 2014-02-10 13:04:09[rn_id] => 1 [rack_name] => GODOWNS )
0

In your Controller,

$this->load->helper('form');
$rack_list = $this->suggest->get_rack_details($part,$subbrand);
$res = array();
foreach($rack_list as $r)
   $res[$r["rs_loc"]] = $r["rack_name"]; // removes duplicates as we are assigning for keys   
$res[""] = "---Location---";
echo form_dropdown("location",$res); // this will produce <select><option></option>...</select>

In your view file, Remove existing <select> as controller generating options with select

As per your expectation, to get only options run below loop in controller instead of form_dropdown

$ret_content = "";
foreach($res as $k => $v)
  $ret_content .="<option value='".$k."'>".$v."</option>";

echo $ret_content;

10 Comments

it's not generating the value
sorry its my mistake. Please check my updated answer:-)
thank you for response sir, but it's not generating the value
can you check my updated answer. Again i did typo error. remove $ infront of form_dropdown
Call to undefined function form_dropdown() msg is shown, i have an onchange event and dynamically generate that with different ids. can u please guide me how to get the value in option value only.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.