多维数组中的每个第一个元素

时间:2014-07-02 18:20:01

标签: php arrays multidimensional-array

尝试过发布此内容,但我会尝试重新说出来。

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

我有一系列项目,其中键是订单#,值是完成订单所需的小时数。

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
                    )
            )
    )

所以orders数组应如下所示:

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
    )

)

现在它只是通过第一个项目阵列订单1-8然后转移到第二个项目。我希望它能完成order->项目数组中的所有第一个元素,然后再移到每个元素中。

我希望这是有道理的!

3 个答案:

答案 0 :(得分:0)

如果你发布了现在使用的代码,那么它可能会有所帮助。我也不是100%明确你想做什么。看起来你可能有更好的方法来实现整个事物,而不是多dim数组,但回答这个问题,这可能有用:

$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

答案 1 :(得分:0)

如果我们假设您帖子中的第一个数组被称为$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循环并不理想,但是你尝试工作的方法也不理想。我认为这是最好的情况。

答案 2 :(得分:0)

查看array_walk_recursive。

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

EDIT 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;
        }
    }
}
);