2

I will mention my array bellow. I need to remove duplicating keys from array but also I need to keep other elements merged. I know this is not clean enough to understand so I will explain.

This the expected result I want.

Main Cat Items 1 -> Categories 1 --> Sub Cat Items 1 
                    Categories 1 --> Sub Cat Items 2
                    Categories 2 --> Sub Cat Items 3
                    Categories 3 --> Sub Cat Items 4

Main Cat Items 2 -> Categories 4 --> Sub Cat Items 5

but from the query I am getting some thing like this.

Main Cat Items 1 -> Categories 1 --> Sub Cat Items 1 
Main Cat Items 1 -> Categories 1 --> Sub Cat Items 2
Main Cat Items 1 -> Categories 2 --> Sub Cat Items 3
Main Cat Items 1 -> Categories 3 --> Sub Cat Items 4
Main Cat Items 2 -> Categories 4 --> Sub Cat Items 5

My query can't refine to get the result. so I have to do it by php. this is what I had tried.

$input = array_map("unserialize", array_unique(array_map("serialize", $test)));

using this nothing changed I got the same array with duplicates.

    $serialized = array_map('serialize', $test);
    $unique = array_unique($serialized);
    $tdsdsdf =  array_intersect_key($test, $unique);

this one also the same.

 $unique = array();
    foreach ($test as $key => $value) {
        if (!array_key_exists($value['main_cat_id'], $unique)) {
            $unique[$value['main_cat_id']] = $value;
        }
    }

when trying with this it is removing the all duplicates with all the elements.

this is the array I am getting just after the db query.

Array
(
    [0] => Array
        (
            [main_cat_id] => 1
            [main_cat_name] => main cat 1
            [cat_id] => 1
            [cat_name] => cat1 parent 1
            [sub_cat_name] => sub 1 cat1 parent 1
        )

    [1] => Array
        (
            [main_cat_id] => 1
            [main_cat_name] => main cat 1
            [cat_id] => 1
            [cat_name] => cat1 parent 1
            [sub_cat_name] => sub 2 cat1 parent 1
        )

    [2] => Array
        (
            [main_cat_id] => 1
            [main_cat_name] => main cat 1
            [cat_id] => 4
            [cat_name] => cat2 parent 1
            [sub_cat_name] => sub 3 cat 4 parent 1
        )

    [3] => Array
        (
            [main_cat_id] => 1
            [main_cat_name] => main cat 1
            [cat_id] => 5
            [cat_name] => cat3 parent 1
            [sub_cat_name] => 
        )

    [4] => Array
        (
            [main_cat_id] => 2
            [main_cat_name] => main cat 2
            [cat_id] => 6
            [cat_name] => cat 4 parent 2
            [sub_cat_name] => 
        )

    [5] => Array
        (
            [main_cat_id] => 2
            [main_cat_name] => main cat 2
            [cat_id] => 7
            [cat_name] => cat 5 parent 2
            [sub_cat_name] => 
        )

    [6] => Array
        (
            [main_cat_id] => 3
            [main_cat_name] => main cat 3
            [cat_id] => 8
            [cat_name] => cat 6 parent 3
            [sub_cat_name] => 
        )

)
2
  • simple array_unique is not working. :( thanks anyway. Commented Nov 4, 2014 at 9:40
  • Still couldn't solve any help ? Commented Nov 4, 2014 at 9:54

3 Answers 3

1

Is this what you want?

<?php
$arr = [
    ["main_cat_id" => 1, "main_cat_name" => "main cat 1", "cat_id" => 1, "cat_name" => "cat1 parent 1", "sub_cat_name" => "sub 1 cat1 parent1"],
    ["main_cat_id" => 1, "main_cat_name" => "main cat 1", "cat_id" => 1, "cat_name" => "cat1 parent 1", "sub_cat_name" => "sub 2 cat1 parent1"],
    ["main_cat_id" => 1, "main_cat_name" => "main cat 1", "cat_id" => 4, "cat_name" => "cat1 parent 1", "sub_cat_name" => "sub 3 cat4 parent1"],
    ["main_cat_id" => 1, "main_cat_name" => "main cat 1", "cat_id" => 5, "cat_name" => "cat3 parent 1", "sub_cat_name" => ""],
    ["main_cat_id" => 2, "main_cat_name" => "main cat 2", "cat_id" => 6, "cat_name" => "cat3 parent 1", "sub_cat_name" => ""],
    ["main_cat_id" => 2, "main_cat_name" => "main cat 2", "cat_id" => 7, "cat_name" => "cat3 parent 1", "sub_cat_name" => ""],
    ["main_cat_id" => 3, "main_cat_name" => "main cat 3", "cat_id" => 8, "cat_name" => "cat3 parent 1", "sub_cat_name" => ""],
];
$unique = [];
foreach ($arr as $key => $value) {
    $unique[$value['main_cat_id']][] = $value;
}

function print_me($arr)
{
    foreach ($arr as $main_cat) {
        echo $main_cat[0]["main_cat_name"] . " -> ";
        $len = strlen($main_cat[0]["main_cat_name"] . " -> ");
        foreach ($main_cat as $index => $cat) {
            echo $cat['cat_name'] . " --> " . $cat['sub_cat_name'] . "\n";
            if ($index != count($main_cat)-1)
                echo str_repeat(" ", $len);
        }
        echo "\n";
    }
}
print_me($unique);
?>

Sorry about the str_repeat, I have trouble with printf... Output:

main cat 1 -> cat1 parent 1 --> sub 1 cat1 parent1
              cat1 parent 1 --> sub 2 cat1 parent1
              cat1 parent 1 --> sub 3 cat4 parent1
              cat3 parent 1 --> 

main cat 2 -> cat3 parent 1 --> 
              cat3 parent 1 --> 

main cat 3 -> cat3 parent 1 --> 
Sign up to request clarification or add additional context in comments.

5 Comments

hmmm yeh but not exactly. I will try and let you know. give me few minutes. Than you very much.
@Yasitha Okay, if you can't make it work, could you give an exemple of the expected print_r() output? I'm not sure of what you want as keys.
I think this is what I am expecting. but still I couldn't display it as I mention in the very beginning of this question. Husnis' answer also same as yours.
@Yasitha I edited and added a print_me function that output something more like your original request.
now this is working fully as I expected. Thank you very much.
1

Rebuilding array:

$result = array();
foreach($test as $key=>$val){ 
    $result[$val['main_cat_id']][$val['cat_id']][] = $val;
}

Display like a format you mentioned:

echo '<pre>';
foreach($result as $key=>$main_cat) {
    echo 'Main Cat Items ' . $key . ' --> ';
    foreach($main_cat as $key2=>$cat) {
        foreach($cat as $key3=>$sub) {
            echo 'Categories ' . $key2 . ' --> ';
            echo 'Sub Cat Items ' . $key3;
            echo "\r\n                     ";
        }
    }
    echo "\r\n";
}
echo '</pre>';

5 Comments

yes This is ok. but I couldn't display as I want(as I showed in the very binning of this question) . any idea ?
@Yasitha : did you mean that you need to display the array as in a format you mentioned above?
@Yasitha : answer updated, does it work for you now?
yes it is good and I am thanking you for your kindness and help. only one thing is there any way to access like this? $key['main_cat_name']
Dear Husni, thank you very much for giving your time and appreciated. Goombis' answer is the exact thing I wanted so I have to mark it as the answer.
0

try with this :)

    /**
      * remove duplicate based on the given key
      * @param {array 2d}
      * @param {mixed} 
      * @return {array 2d}
      */
    function removeDuplicate($array, $key) {
        $clearArray = array();
        foreach($array as $a) {
            $found_b = FALSE;
            foreach($clearArray as $ca) {
                if($ca[$key] == $a[$key]) {
                    $found_b = TRUE;
                    break;
                }
            }
            if(!$found_b) {
                $clearArray[] = $a;
            }
        }
        return $clearArray;
    }

Comments

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.