【问题标题】:Every first element in multidimensional array多维数组中的每个第一个元素
【发布时间】:2014-07-02 18:20:01
【问题描述】:

之前尝试过发布此内容,但我将尝试重新措辞。

假设我有一个多维数组,我想在移动到每个元素中的第二个元素之前影响每个元素中的第一个元素。

我有一个项目数组,其中键是订单#,值是完成订单所需的时间。

Array
(
[100 Series] => Array
    (
        [Order1] => 6
        [Order2] => 6
        [Order3] => 6
        [Order4] => 6
        [Order5] => 6
        [Order6] => 6
        [Order7] => 6
        [Order8] => 6
    )

[50 Series] => Array
    (
        [Order1] => 4
        [Order2] => 4
        [Order3] => 4
        [Order4] => 4
    )

)

然后我有一群人,他们可以从事的时间表和项目:

Array
(
[Eric Smith] => Array
    (
        [Schedule] => Array
            (
                [Monday] => 8
                [Tuesday] => 8
                [Wednesday] => 8
                [Thursday] => 8
                [Friday] => 6
                [Saturday] => 0
                [Sunday] => 0
            )

        [Projects] => Array
            (
                [0] => 100 Series
                [1] => 50 Series
            )

    )

)

我想用日期、项目名称、订单号以及他们在该项目上工作的人员姓名和时间来填充工作流数组,它看起来像这样:

Array
(
[Monday] => Array
    (
        [100 Series] => Array
            (
                [Order1] => Array
                    (
                        [Eric Smith] => 6
                    )
             )
        [50 Series] => Array
            (
                [Order1] => Array
                    (
                        [Eric Smith] => 2
                    )
    )
[Tuesday] => Array
    (
        [50 Series] => Array
            (
                [Order1] => Array
                    (
                        [Eric Smith] => 2
                    )
            )
        [100 Series] => Array

                [Order2] => Array
                    (
                        [Eric Smith] => 6
                    )
            )
    )

所以订单数组应该是这样的:

Array
(
[100 Series] => Array
    (
        [Order1] => 0
        [Order2] => 0
        [Order3] => 6
        [Order4] => 6
        [Order5] => 6
        [Order6] => 6
        [Order7] => 6
        [Order8] => 6
    )

[50 Series] => Array
    (
        [Order1] => 0
        [Order2] => 4
        [Order3] => 4
        [Order4] => 4
    )

)

现在它只是通过第一个项目数组 order 1-8 然后移动到第二个项目。我希望它先完成 orders->project 数组中的所有第一个元素,然后再移动到每个元素中的第二个。

我希望这是有道理的!

【问题讨论】:

  • 您是否有任何理由不在关系数据库中进行跟踪,并通过查询获取有关项目及其人员的信息?
  • 我最终会的。只是先让本地数据正常工作。

标签: php arrays multidimensional-array


【解决方案1】:

如果您发布了您现在正在使用但不起作用的代码,这可能会有所帮助。我也不是 100% 清楚你到底想做什么。似乎有一种比使用多维数组更好的方法可以实现整个事情,但要回答这个问题,这可能会奏效:

$index = 0;
start:
for ($i=0;i<count($projectArray);$i++){
    if (count($project)<$index)goto finished; //or maybe continue;
    //you may need some extra conditionals, but that depends on what you want to be terminating the loop
    $project = $projectArray[$i];
    $item = $project[$index]; //then do what you want with $item
}
$index++;
//if you are not finished
goto start;
finished:
//the rest of your code

【讨论】:

    【解决方案2】:

    如果我们假设您帖子中的第一个数组称为$projects;,而您的工作流数组称为$workflow;

    您将需要以下内容:

    // first loop $workflow and create an array that can be processed more easily
    $data = array();
    foreach($workflow as $day => $project){
        foreach($project as $projectName => $projectDetails){
                foreach($projectDetails as $orderNumber => $person){
                        foreach($person as $hours){
                                $data[$orderNumber][$projectName] = $hours;    
                        }
                }
        }
    }
    
    // print_r($data) shows new array that is ordered in terms of order => project => hours
    
    // now update your $projects array with this $data
    foreach($data as $key => $project){
        foreach($project as $projectName => $projectHours){
                $projects[$projectName][$key] -= $projectHours;
        }
    }
    
    // print_r($projects) to see that the project hours have decreased appropriately
    

    我很欣赏深度嵌套的 foreach 循环并不理想,但您尝试使用的方法也不理想。我认为这是最好的情况。

    【讨论】:

      【解决方案3】:

      查看array_walk_recursive。

      您可以使用回调来检查该值是否不是数组本身并且是数组的第一个值。

      编辑 array_walk_recursive 似乎没用,因为它没有传递有关您在数组中位置的任何信息。 这是我的步行功能:

      class arr{
      
          /**
           * Iterate and traverse array and apply function to data
           * 
           * example:
           * $f = function($v, $k, $custom, $info){
           *      if(!is_array($v) && !obj::is($v)) {
           *          return $v." [ custom: {$custom['key']} ] [ level: ".$info['depth'].' | No '.$info['i'].' of '.$info['count']." ]";
           *      }
           *      return $v;
           *  };
           * 
           *  arr::walk($mixed,$f,['key'=>'value'],true)
           * 
           * @param array $array
           * @param function,array $callback
           * @param mixed $custom
           * @param integer,boolean $recursive
           * @param boolean $deduce
           * @param array $info
           * @return array
           */
          public static function walk(&$array, $callback, $custom = null, $recursive = true, $deduce = false, $info = [])
          {
              if (is_array($array)) {
                  $return = null;
                  if (array_key_exists('depth', $info)) {
                      $info['depth'] ++;
                  } else {
                      $info['depth'] = 1;
                  }
                  $info['count'] = count($array);
                  $info['i'] = 1;
                  $r = $recursive;
                  if (gettype($r) === 'integer') {
                      $r--;
                  }
                  foreach($array as $k => &$v) {
                      $inf = $info;
                      if ($info['depth'] > 0) {
                          $inf['path'][] = $k;
                      }
                      if ($return = $callback($v, $k, $custom, $inf, $array)) {
                          break;
                      }
                      if (is_array($v) && $r > 0) {
                          $return = arr::walk($v, $callback, $custom, $r, $deduce, $inf);
                          if ($return) {
                              break;
                          }
                      }
                      $info['i'] ++;
                  }
                  return $return;
              }
          }    
      }
      

      您的实现应该如下所示:

      $result = [];
      arr::w($a, function($v,$k) use (&$result){
          if(is_array($v) && !empty($v)){        
              $r = array_values($v)[0];
              if(!is_array($r)){
                  $result[] = $r;
              }
          }
      }
      );
      

      【讨论】:

        猜你喜欢
        • 2015-07-08
        • 1970-01-01
        • 2019-09-30
        • 2018-02-01
        • 2021-06-28
        • 2014-11-01
        • 2021-10-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多