dpo60833 2014-11-11 12:09
浏览 24
已采纳

验证PHP数组键>值

Having an array like [1]

$arr = array(
        array(
            "ignoreMe" => "123",
            "checkMe" => "value",
        ),
        array(
            "ignoreMe" => "456",
            "checkMe" => "value",
        ),
 );

I'd like to check if special keys (here the key checkMe) of the inner array have the same value. If all keys have the same value, then I'd like to remove the key from the inner array. (from all arrays)

But when having an array like [2]

$arr = array(
        array(
            "ignoreMe" => "123",
            "checkMe" => "value",
        ),
        array(
            "ignoreMe" => "456",
            "checkMe" => "value",
        ),
        array(
            "ignoreMe" => "789",
            "checkMe" => "foo", 
        ),
 );

All keys should stay intact.

How would I do this with this complex validator? (Link https://github.com/Respect/Validation)

Expected result of [1] is

$arr = array(
        array(
            "ignoreMe" => "123",
        ),
        array(
            "ignoreMe" => "456",
        ),
 );

[2] should not be touched

Here is what has been tried:

$validator = v::arr()->each(v::key("check", v::equals('value')));
  • 写回答

1条回答 默认 最新

  • doujiling4377 2014-11-11 12:41
    关注

    Okay, if you are running PHP 5.5+ then you can use a combination of the array_column and array_unique functions to remove the items from the array, if they all have the same value:

    I am not sure exactly what such a function would be called, so I just called it myFunc...

    function myFunc(array $arr, $key)
    {
        // Get all the values using a key
        $values = array_column($arr, $key);
    
        // Remove all duplicates
        $unique = array_unique($values);
    
        // If there only is one item left then it means
        // that all the values are the same, so proceed
        // with modifying it...
        if (count($unique) === 1) {
    
            // Go over each array...
            foreach ($arr as $x => & $value) {
    
                // And unset the key
                unset($value[$key]);
            }
        }
        // Return the array
        return $arr;
    }
    

    Example:

    $arr1 = array(
        array("ignoreMe" => "123", "checkMe" => "value"),
        array("ignoreMe" => "456", "checkMe" => "value"),
    );
    $arr2 = array(
        array("ignoreMe" => "123", "checkMe" => "value"),
        array("ignoreMe" => "456", "checkMe" => "value"),
        array("ignoreMe" => "789", "checkMe" => "foo"),
    );
    
    // All the values in this array are the same, so they
    // will be removed
    var_dump($arr1);
    var_dump(myFunc($arr1, 'checkMe'));
    echo '<hr>';
    
    // There is a value in this array that is not the same
    // as the others, so this array will be left intact
    var_dump($arr2);
    var_dump(myFunc($arr2, 'checkMe'));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化