猿问

如何订购“斑马”数组,以便每个键都有一个备用值(1、0)

如果你有这个 PHP 数组:


$args = array(

    'a' => array(

        'zebra' => 1,

    ),

    'b' => array(

        'zebra' => 0,

    ),

    'c' => array(

        'zebra' => 0,

    ),

    'd' => array(

        'zebra' => 0,

    ),

    'e' => array(

        'zebra' => 1,

    ),

);

有没有办法通过“斑马”键值在算法上改变这个数组的顺序,但不是“增量”(0,0,0,1,1),它们会交替(0,1,0,1 ,0)。


因此,使用上面的示例,所需的完成数组将如下所示:


$ordered_args = array(

    'b' => array(

        'zebra' => 0,

    ),

    'a' => array(

        'zebra' => 1,

    ),

    'c' => array(

        'zebra' => 0,

    ),

    'e' => array(

        'zebra' => 1,

    ),

    'd' => array(

        'zebra' => 0,

    ),


);

任何额外的重复项都应附加到末尾,因此解决方案应允许其他数组,例如具有斑马值的数组,例如 (1,1,1,1,0,0),这将导致 (0,1,0, 1,1,1)


我根本想不通这个!


编辑:我尝试通过这个类似但不同的 question使用 usort 来做到这一点,但答案是否定的,所以我正在寻找一个编程解决方案(没有 usort)。


森栏
浏览 97回答 4
4回答

小唯快跑啊

同样的想法是将输入分成 1 和 0,然后只要还有一些东西要输出,就输出 0 和 1。每次输出一个值时,数组都会减少,这会一直持续到两个列表都是空的,所以应该处理不平衡的列表......$temp = [ 0 => [], 1 => []];foreach($args as $key=>$value){    $temp[$value['zebra']][] = $key;}$output = [];while ( !empty($temp[0]) || !empty($temp[1]) )   {    if ( !empty($temp[0]) )   {        $next = array_shift($temp[0]);        $output [$next] = $args[$next];    }    if ( !empty($temp[1]) )   {        $next = array_shift($temp[1]);        $output [$next] = $args[$next];    }}

翻阅古今

这是array_map在单独的数组中抓取 1 和 0 后使用的解决方案:$args0 = array_filter($args, function ($arg) {  return $arg['zebra'] === 0;});$args1 = array_filter($args, function ($arg) {  return $arg['zebra'] === 1;});$result = array_merge(...array_map(static function ($arg0Key, $arg1Key) use ($args0, $args1) {  if ($arg0Key !== null) {    $result[$arg0Key] = $args0[$arg0Key];  }  if ($arg1Key !== null) {    $result[$arg1Key] = $args1[$arg1Key];  }  return $result;}, array_keys($args0), array_keys($args1)));print_r($result);演示:https ://3v4l.org/sfqeq注意:使用两个array_filter分隔值看起来不错,但循环$args两次;如果初始数组可能有点大,则更喜欢简单的循环。不过,这不是答案的相关部分。

德玛西亚99

我可以建议您使用带有比较计数的解构。第一步,您可以使用zebra = 1和 with收集所有索引zebra = 0:$zeros = [];$ones = [];foreach($args as $let=>$arg){&nbsp; &nbsp; if ($arg['zebra'] === 1) {&nbsp; &nbsp; &nbsp; &nbsp; $ones[] = $let;&nbsp; &nbsp; } else if ($arg['zebra'] === 0) {&nbsp; &nbsp; &nbsp; &nbsp; $zeros[] = $let;&nbsp; &nbsp; }}现在您可以构造结果数组,例如:if(abs(count($zeros) - count($ones)) === 1) {&nbsp; &nbsp; // if their difference equal to 1&nbsp; &nbsp; if (count($ones) > count($zeros)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// if $ones is bigger&nbsp; &nbsp; &nbsp; &nbsp; foreach($zeros as $ind=>$let){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $res[$ones[$ind]] = ['zebra' => 1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $res[$let]&nbsp; &nbsp; &nbsp; &nbsp; = ['zebra' => 0];&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $tmp = $ind;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $res[$ones[$tmp+1]] = ['zebra' => 1];&nbsp; &nbsp; } else if (count($ones) < count($zeros)){&nbsp; &nbsp; &nbsp; // if $zeros is bigger&nbsp; &nbsp; &nbsp; &nbsp; foreach($ones as $ind=>$let){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $res[$zeros[$ind]] = ['zebra' => 0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $res[$let]&nbsp; &nbsp; &nbsp; &nbsp; = ['zebra' => 1];&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $tmp = $ind;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $res[$zeros[$tmp+1]] = ['zebra' => 0];&nbsp; &nbsp; }}输出:Array(&nbsp; &nbsp; [b] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [zebra] => 0&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [a] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [zebra] => 1&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [c] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [zebra] => 0&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [e] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [zebra] => 1&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [d] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [zebra] => 0&nbsp; &nbsp; &nbsp; &nbsp; ))演示如果在 (1,0,1,0,0) 的情况下需要结果,请使用下一个构造函数:&nbsp; &nbsp; if (count($ones) > count($zeros)){&nbsp; &nbsp; &nbsp; &nbsp; foreach($ones as $ind=>$let){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (isset($zeros[$ind])) $res[$zeros[$ind]] = ['zebra' => 0];&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $res[$let]&nbsp; &nbsp; &nbsp; &nbsp; = ['zebra' => 1];&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; &nbsp; } else if (count($zeros) > count($ones)){&nbsp; &nbsp; &nbsp; &nbsp; foreach($zeros as $ind=>$let){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $res[$let]&nbsp; &nbsp; &nbsp; &nbsp; = ['zebra' => 0];&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (isset($ones[$ind])) $res[$ones[$ind]] = ['zebra' => 1];&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; &nbsp; }输出:Array(&nbsp; &nbsp; [b] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [zebra] => 0&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [a] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [zebra] => 1&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [d] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [zebra] => 0&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [c] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [zebra] => 1&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [e] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [zebra] => 0&nbsp; &nbsp; &nbsp; &nbsp; ))

宝慕林4294392

好吧,您可以收集0一个数组中的所有1s' 和另一个数组中的所有 s',然后只需将它们交替添加到新数组中并进行布尔flag检查。伪代码:ones = []zeroes = []for($args as key => value)&nbsp; &nbsp; value['key'] = key // to preserve the key as well for later restoration&nbsp; &nbsp; if(value['zebra'] == 1)&nbsp; &nbsp; &nbsp; &nbsp;ones.push(value)&nbsp; &nbsp; else&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;zeroes.push(value)result = []flag = true // to decide to pop from ones or zeroeswhile(sizeof(ones) > 0 || sizeof(zeroes) > 0){&nbsp; &nbsp; if(sizeof(ones) == 0 || flag === false){&nbsp; &nbsp; &nbsp; &nbsp; element = zeroes.pop()&nbsp; &nbsp; &nbsp; &nbsp; result[element['key']] = ['zebra' => element['zebra']]&nbsp; &nbsp; }else if(sizeof(zeroes) == 0 || flag){&nbsp; &nbsp; &nbsp; &nbsp; element = ones.pop()&nbsp; &nbsp; &nbsp; &nbsp; result[element['key']] = ['zebra' => element['zebra']]&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; flag = !flag // to alternately add from either arrays}
随时随地看视频慕课网APP
我要回答