从数组内的数组中的数组中获取值

时间:2015-03-18 05:38:42

标签: php arrays

我无法理解数组。例如,我得到了以下数据。

array(
  label=> person1
  start=> 2014-10-10
  end=> 2014-10-11
  class=> annual
)
array(
  label=> person2
  start=> 2014-10-08
  end=> 2014-10-08
  class=> sick
)
array(
  label=> person1
  start=> 2014-10-01
  end=> 2014-10-03
  class=> sick
)
array(
  label=> person3
  start=> 2014-10-20
  end=> 2014-10-20
  class=> annual
)
array(
  label=> person1
  start=> 2014-10-29
  end=> 2014-10-29
  class=> compassionate
)

我想以这种方式安排

array(
     [person1]=>array(
            array(
                start=> 2014-10-10
                end=> 2014-10-11
                class=> annual),
            array(
                start=> 2014-10-01
                end=> 2014-10-03
                class=> sick),
            array(
                 start=> 2014-10-29
                 end=> 2014-10-29
                 class=> compassionate), 
            ),
      [person2]=>array(
                 start=> 2014-10-08
                 end=> 2014-10-08
                 class=> sick),
      [person3]=>array(
                 start=> 2014-10-20
                 end=> 2014-10-20
                 class=> annual)
)

我的要求是在同一行显示具有相同标签的数据。 这是我使用的代码但它根本不起作用。

var $blocks = array();
var $data = array();
var $blocksByLabel = array();

global $blocksByLabel;
global $blocks;

foreach($this->data as $d) {
     foreach ($this->blocks as $block) {
         $label = $block->$d['label'];
              if (!array_key_exists($d['label'], $blocksByLabel)){
                    $blocksByLabel[$block->label] = array();
               }
array_push($blocksByLabel[$block->label], $blocks);
         }


  $this->blocks[] = array(
    'label' => $d['label'],
    'start' => $start = strtotime($d['start']),
    'end'   => $end   = strtotime($d['end']),
    'class' => @$d['class']
  );

1 个答案:

答案 0 :(得分:1)

使用它:

$old_array是源数组,$new_array是重新排列的数组

foreach($old_array as $r) {
  $new_array[$r['label']][] = array("start" => $r['start'],
                                    "end"   => $r['end'],
                                    "class" => $r['class']);
}

这应该按照你想要的方式排列你的数组..你应该根据你的变量名重命名old_array和new_array。

添加运行程序以进一步说明

这是我正在运行的代码:

<?php
  $old_array[] = array("label" => "person1", "start" => "2014-10-10", "end" => "2014-10-11", "class" => "anual");
  $old_array[] = array("label" => "person2", "start" => "2014-10-08", "end" => "2014-10-08", "class" => "sick");
  $old_array[] = array("label" => "person1", "start" => "2014-10-01", "end" => "2014-10-03", "class" => "sick");
  $old_array[] = array("label" => "person3", "start" => "2014-10-20", "end" => "2014-10-20", "class" => "anual");
  $old_array[] = array("label" => "person1", "start" => "2014-10-29", "end" => "2014-10-29", "class" => "compassionate");

  foreach($old_array as $r) {
    $new_array[$r['label']][] = array("start" => $r['start'],
                                "end" => $r['end'],
                                "class" => $r['class']);
  }

  echo "<pre>";
  print_r($new_array);
  echo "</pre>";
?>

执行后,输出应为:

Array
(
    [person1] => Array
        (
            [0] => Array
                (
                    [start] => 2014-10-10
                    [end] => 2014-10-11
                    [class] => anual
                )

            [1] => Array
                (
                    [start] => 2014-10-01
                    [end] => 2014-10-03
                    [class] => sick
                )

            [2] => Array
                (
                    [start] => 2014-10-29
                    [end] => 2014-10-29
                    [class] => compassionate
                )

        )

    [person2] => Array
        (
            [0] => Array
                (
                    [start] => 2014-10-08
                    [end] => 2014-10-08
                    [class] => sick
                )

        )

    [person3] => Array
        (
            [0] => Array
                (
                    [start] => 2014-10-20
                    [end] => 2014-10-20
                    [class] => anual
                )

        )

)

重新排列数组的代码工作正常..请通过$this->data检查var_dump($this->data)变量以检查变量的内容..(如果源数组没有值,当然结果也没有任何价值。)

希望这会有所帮助..