dozoqn3347 2014-10-29 10:32
浏览 16
已采纳

在PHP中的多维数组中从数组中存储相同类型的值?

I have a multidimensional array in php like this:

 Array
 (
 [0] => Array
    (
        [Year_id] => 29
        [Make_id] => 7
        [Model_id] => 34
        [Engine_id] => 19
    )

[1] => Array
    (
        [Year_id] => 30
        [Make_id] => 7
        [Model_id] => 34
        [Engine_id] => 19
    )

[2] => Array
    (
        [Year_id] => 21
        [Make_id] => 7
        [Model_id] => 34
        [Engine_id] => 19
    )

[3] => Array
    (
        [Year_id] => 1
        [Make_id] => 7
        [Model_id] => 34
        [Engine_id] => 19
    )

[4] => Array
    (
        [Year_id] => 2
        [Make_id] => 7
        [Model_id] => 34
        [Engine_id] => 2
    )

[5] => Array
    (
        [Year_id] => 3
        [Make_id] => 7
        [Model_id] => 34
        [Engine_id] => 2
    )

[6] => Array
    (
        [Year_id] => 4
        [Make_id] => 7
        [Model_id] => 34
        [Engine_id] => 2
    )

 )

My code is as follows:

  <?php
      $cars = array
      (
      array("Year_id"=>29, "Make_id"=>7, "Model_id"=>34, "Engine_id"=>19),
      array("Year_id"=>30, "Make_id"=>7, "Model_id"=>34, "Engine_id"=>19),
      array("Year_id"=>21, "Make_id"=>7, "Model_id"=>34, "Engine_id"=>19),
      array("Year_id"=>1, "Make_id"=>7, "Model_id"=>34, "Engine_id"=>19),
      array("Year_id"=>2, "Make_id"=>7, "Model_id"=>34, "Engine_id"=>2),
      array("Year_id"=>3, "Make_id"=>7, "Model_id"=>34, "Engine_id"=>2),
      array("Year_id"=>4, "Make_id"=>7, "Model_id"=>34, "Engine_id"=>2)
      );
   echo '<pre>';
   print_r($cars);
   ?>

In above result, I have four ids named Year_id, Make_id, Model_id and Engine_id. Year_id may change any time but when any of remaining IDs change in value, I want to store those arrays in a separate array and again check for remaining arrays and when any of ID values change except Year_id, store those arrays in a separate array.

Right now it should store first four arrays in a separate array because Engine_id value changes. How to solve it? I am ready for bounty after 2 days.

  • 写回答

2条回答 默认 最新

  • dspvin19712 2014-10-29 10:39
    关注
    $i = 0;
    $sepr = 0;
    $arr = array();
    foreach($cars as $car) {
        $k = $i; $k = ($k > 0 ? $k-1 : 0);
        foreach($car as $key => $value) {
            if($key != "Year_id" && $value != $cars[$k][$key]) {
                $sepr++;
            }
        }
        $arr[$sepr][] = $car;
        $i++;
    }
    
    print_r($arr);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?