简体   繁体   English

如果值相同,则从另一个数组创建数组

[英]Create array from another array if values are same

I have the following array -我有以下数组 -

Array
(
    [31] => Array
        (
            [0] => 3
            [1] => 3
        )
    [33] => Array
        (
            [0] => 2
            [1] => 1
        )
)

Now for the key 31 both of the elements has same value ie 3 but not for the key 33. So I am trying to create another array which will look like.现在对于键 31,两个元素都具有相同的值,即 3,但对于键 33 则不是。所以我试图创建另一个看起来像的数组。

Array
(
    [31] =>  same
    [33] =>  notsame       
)

That means if a key from multidimensional array has got all the values same then it will have the text 'same' else 'notsame'这意味着如果来自多维数组的键具有相同的所有值,那么它将具有文本“相同”否则为“不相同”

My code-我的代码-

foreach($subvaluesArr as $k1=>$v1) //$subvaluesArr is the multidimensional array here
    {
        foreach($v1 as $k2=>$v2)
        {           
             if($v1[$k2] = $v1[$k2+1])
            {
                $newArr[$k1] = 'same';
            }
            else
            {
                $newArr[$k1] = 'notsame';
            }
        }
    }

    echo '<pre>';
    print_r($newArr);
    echo '</pre>';

And the output is showing 'notsame' for both keys.并且输出显示两个键的“不同”。

Array
(
    [31] => notsame
    [33] => notsame
)

Any help is highly appreciated.任何帮助都受到高度赞赏。

When you run this snippet, you will get this error当您运行此代码段时,您将收到此错误

Notice: Undefined index: 2 in /in/bcqEH on line 14注意:未定义索引:第 14 行 /in/bcqEH 中的 2

See https://3v4l.org/bcqEHhttps://3v4l.org/bcqEH

This is because the code tries to compare first and second, and it tries to compare second and third element.这是因为代码尝试比较第一个和第二个元素,尝试比较第二个和第三个元素。 But this third element doesn't exist.但是这第三个元素不存在。 This means the comparison fails and sets the value to notsame .这意味着比较失败并将值设置为notsame

To fix this, you could just compare the first two elements, eg要解决此问题,您可以只比较前两个元素,例如

foreach ($subvaluesArr as $k1 => $v1) {
    if ($v1[0] == $v1[1]) {
        $newArr[$k1] = 'same';
    } else {
        $newArr[$k1] = 'notsame';
    }
}

When you really have more than two elements, you might try array_unique当你真的有两个以上的元素时,你可以试试array_unique

foreach ($subvaluesArr as $k1 => $v1) {
    $u = array_unique($v1);
    if (count($u) == 1) {
        $newArr[$k1] = 'same';
    } else {
        $newArr[$k1] = 'notsame';
    }
}

You have to break;你必须break; loop after running the if-else condition.运行if-else条件后循环。 example :例子 :

foreach($subvaluesArr as $k1=>$v1) //$subvaluesArr is the multidimensional array here
{
    foreach($v1 as $k2=>$v2)
    {           
         if($v1[$k2] === $v1[$k2+1])
        {
            $newArr[$k1] = 'same';
        }
        else
        {
            $newArr[$k1] = 'notsame';
        }
        break;
    }
}

cant comment so I write it as answer.无法评论,所以我将其写为答案。 In your loop when you hit last element of an array, you ask在循环中,当您点击数组的最后一个元素时,您会问

if($v1[$k2] == $v1[$k2+1])

where $v1[$k2+1] is "undefined" as you are out of bounds.其中$v1[$k2+1]为“未定义”,因为您超出了范围。 So this last element is always false and you end up with "notsame"所以最后一个元素总是假的,你最终得到“不一样”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM