简体   繁体   English

从数组内的数组中获取值

[英]fetch value from array within array within array

I have trouble understanding arrays. 我在理解数组时遇到麻烦。 For example, I got the following data. 例如,我得到了以下数据。

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
)

And I want to arrange it this way 我想这样安排

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

My requirement is to display the data with same label in the same row. 我的要求是在同一行中显示带有相同标签的数据。 This is the code I used but it does not works at all. 这是我使用的代码,但是根本不起作用。

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']
  );

use this: 用这个:

$old_array is the source array and $new_array is the rearanged array $old_array是源数组,而$new_array是重新排列的数组

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

this should arrange your array the way you want.. and you should rename the old_array and new_array based on your variable names.. 这应该按照您想要的方式排列数组。并且您应该根据变量名重命名old_array和new_array。

ADDED RUNNING PROGRAM for further clarification 附加的运行程序,以进一步说明

here is my running code: 这是我的运行代码:

<?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>";
?>

after execution, the output should be: 执行后,输出应为:

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
                )

        )

)

the code for rearranging the array is working fine.. please check your $this->data variable through var_dump($this->data) to check the variable's content.. (if the source array doesn't have value, of course the result would have no value either).. 为重新安排数组的代码工作正常。请检查您的$this->data通过可变var_dump($this->data)来检查变量的内容。(如果源阵列不具有价值,当然结果也将没有任何价值)。

hope this helps.. 希望这可以帮助..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM