简体   繁体   中英

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

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

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

hope this helps..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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