在php中,当第一个值相同时,如何删除多维数组中的所有键?

时间:2021-05-18 07:42:26

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..

我如何检查第一个值条件,如mydata1是唯一的,如果mydata1被发现更多,那么一次删除完整的数组检查我的输出。

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
        )

)

1 个解决方案

#1


0  

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"
    }
}

#1


0  

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"
    }
}