dongyipa0028 2013-03-29 07:44
浏览 25
已采纳

更有效地减去数组

I have a few questions. Got this array:

$array_1  = array(
    "1" => "4",
    "2" => "8",
    "3" => "12",
    "4" => "16",
    "5" => "20",
    "6" => "24",
    "7" => "28",
    "8" => "32",
    "9" => "36",
    "10" => "40",
    "11" => "44",
    "12" => "48",
    "13" => "52",
    "14" => "56",
    "15" => "60",
    "16" => "64",
    "17" => "68",
    "18" => "72",
    "19" => "76",
    "20" => "80",
    "21" => "84",
    "22" => "88",
    "23" => "92",
    "24" => "96",
    "25" => "100",
    "26" => "104",
    "27" => "108",
    "28" => "112",
    "29" => "116",
    "20" => "120",
    "31" => "124",
    "32" => "128",
    "33" => "132",
    "34" => "136",
    "35" => "140",
    "36" => "144",
    "37" => "148",
    "38" => "152",
    "39" => "156",
    "40" => "160",
    "41" => "164",
    "42" => "168",
    "43" => "172",
    "44" => "176",
    "45" => "180",
    "46" => "184",
    "47" => "188",
    "48" => "192",
    "49" => "196",
    "50" => "200"
);

I would like to subtract $array_1 by 1,2,3,4 into multiple arrays. So far I have done it by having other arrays:

$array_2  = array(
    "1" => "1",
    "2" => "1",
    ...
    "49" => "1",
    "50" => "1"
);

$array_3  = array(
    "1" => "2",
    "2" => "2",
    ...
    "49" => "2",
    "50" => "2"
);
// All the way to values of 4

Then with this I would do an array_diff to get the lowered value.

I was wondering firstly is there a better way (more efficient) to have the array subtract rather than repeat the 1,2,3,4 50 times each.

Secondly, is there a more efficient way to have $array_1 have the values of multiple of 4 up to 200?

Thirdly, do I need an array to subtract from $array_1 to lower the value? Is there a better way to this like: $array_1 - 1

INTENDED OUTPUT

// $array_1 - $array_2 (value of 1)
$minus_one = array(
    "1" => "3",
    "2" => "7",
    "3" => "11",
    ...
);
// $array_1 - $array_3 (value of 2)
$minus_two = array(
    "1" => "2",
    "2" => "6",
    "3" => "10",
    ...
);
// $array_1 - $array_4 (value of 3)
$minus_one = array(
    "1" => "1",
    "2" => "4",
    "3" => "8",
    ...
);
  • 写回答

2条回答 默认 最新

  • dopt85756 2013-03-29 09:05
    关注

    While xdazz is solution is right. You also direct can generate final intended array by

    $array_1 = array_fill(1, 50, null);
    array_walk($array_1, function(&$item, $key){
      $item = $key * 4;
    });
    
    $minus_one = $minus_two = $minus_three = array();
    foreach($array_1 as $key => $value)
    {
        $minus_one[$key] = $value - 1 ;
        $minus_two[$key] = $value - 2 ;
        $minus_three[$key] = $value - 3 ;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题