0
    stdClass Object
    (
        [id] => 132
        [slug] => test
        [parent_id] => 15
    )
    stdClass Object
    (
        [id] => 132
        [slug] => test
        [parent_id] => 15
    )

I used the array_unique function to delete duplicate data, but it gave the following error

Message: array_unique() expects parameter 1 to be array, object given

here is my code:

  > //get category
//build category query
public function build_query($lang_id = null)
{
    if (empty($lang_id)) {
        $lang_id = $this->selected_lang->id;
    }
    $ci =& get_instance();
    if (item_count($ci->languages) > 1) {
        return "SELECT categories.*, categories.parent_id AS join_parent_id, categories_lang.name AS name,
            (SELECT slug FROM categories WHERE id = join_parent_id) AS parent_slug,
            (SELECT name FROM categories_lang WHERE categories_lang.category_id = categories.id AND categories_lang.lang_id != " . $this->db->escape(clean_number($lang_id)) . "  ORDER BY categories_lang.lang_id LIMIT 1) AS second_name
            FROM categories
            LEFT JOIN categories_lang ON categories.id = categories_lang.category_id AND categories_lang.lang_id = " . $this->db->escape(clean_number($lang_id)) . " ";
    } else {
        return "SELECT categories.*, categories.parent_id AS join_parent_id, categories_lang.name AS name,
            (SELECT slug FROM categories WHERE id = join_parent_id) AS parent_slug
            FROM categories
            LEFT JOIN categories_lang ON categories.id = categories_lang.category_id AND categories_lang.lang_id = " . $this->db->escape(clean_number($lang_id)) . " ";
    }
}
    >     public function get_category($id)
    >     {
    >         $sql = $this->build_query() . "WHERE categories.id = ?";
    >         $query = $this->db->query($sql, array(clean_number($id)));
    >         return $query->row();
    >     }
    > 
    >  function get_category_by_id($id)
    >     {
    >         $ci =& get_instance();
    >         return $ci->category_model->get_category($id);
    >     }
 $item_category = get_category_by_id($product->category_id);
    > 
    >  print_r(array_unique($item_category));
5
  • how do you know, which record is duplicate of the other? which one do you want to keep? do the dups come from a sql query where you could use group by? Commented Aug 28, 2020 at 16:49
  • It does not matter which one to delete. I edited my code please see again Commented Aug 28, 2020 at 16:54
  • add group by categories.id after your where clause and forget about array_unique() Commented Aug 28, 2020 at 16:57
  • ٍError: Unknown column 'categories.id' in 'group statement' Commented Aug 28, 2020 at 16:59
  • $query->row() will return object. Try $query->row_array() or $query->result_array() Commented Aug 30, 2020 at 7:14

0

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.