Search code examples
phparraysmultidimensional-arrayunique

How can i remove all keys in multidimensional array when first value are same in php


How can get only unique value in array and how can remove blank array.

how can i check first value condition like mydata1 are unique if mydata1 are found more then one time then remove full array check my output..

Array
(
    [0] => Array
        (
            [0] => Mydata1
            [1] => first
            [2] => first
        )

    [1] => Array
        (
            [0] => 
            [1] => 
            [2] => 
        )

    [2] => Array
        (
            [0] => Mydata3
            [1] => second
            [2] => first
        )

    [3] => Array
        (
            [0] => Mydata1
            [1] => first
            [1] => dddemo
        )

    [4] => Array
        (
            [0] => Mydata1
            [1] => fourth
            [2] => first
        )

    [5] => Array
        (
            [0] => Mydata3
            [1] => fifth
            [2] => first
        )

    [6] => Array
        (
            [0] => Mydata2
            [1] => sixth
            [2] => first
        )

)

/////////////////////////////////////////////////////////////////

Output :

Array
(
    [0] => Array
        (
            [0] => Mydata1
            [1] => first
            [2] => first
        )

    [1] => Array
        (
            [0] => Mydata3
            [1] => second
            [2] => first
        )


    [2] => Array
        (
            [0] => Mydata2
            [1] => sixth
            [2] => first
        )

)

Solution

  • Okay, so here is my solution:

    $array = [
        0 => [
            'Mydata1',
            'first',
            'first',
        ],
        1 => [
            null,
            null,
            null,
        ],
        2 => [
            'Mydata3',
            'second',
            'first',
        ],
        3 => [
            'Mydata1',
            'first',
            'dddemo',
        ],
        4 => [
            'Mydata1',
            'fourth',
            'first',
        ],
        0 => [
            'Mydata3',
            'fifth',
            'first',
        ],
        0 => [
            'Mydata2',
            'sixth',
            'first',
        ],
    ];
    $newarray = [];
    foreach ($array as $key => $data)
    {
        $flag = true;
        foreach ($newarray as $new)
        {
            if ($new[0] == $data[0])
            {
                $flag = false;
            }
        }
        if ($flag && isset($data[0]))
        {
            $newarray[count($newarray)] = $data;
        }
    }
    var_dump($newarray);
    // Outputs
    array(3) {
        [0]=> array(3) {
            [0]=>
                string(7) "Mydata2"
            [1]=>
                string(5) "sixth"
            [2]=>
                string(5) "first"
        }
        [1]=> array(3) {
            [0]=>
                string(7) "Mydata3"
            [1]=>
                string(6) "second"
            [2]=>
                string(5) "first"
        }
        [2]=> array(3) {
            [0]=>
                string(7) "Mydata1"
            [1]=>
               string(5) "first"
            [2]=>
                string(6) "dddemo"
        }
    }