Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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']
  );
share|improve this question
    
If this is PHP, there is a basic misunderstanding about strings and constants. You have to put all the keys and values in quotes. As you write it, they are undefined constants. Undefined constants raise warnings and not errors and are assumed to represent themselves. You should never use undefined constants. –  Lorenz Meyer Mar 18 at 6:01

1 Answer 1

up vote 0 down vote accepted

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

share|improve this answer
    
thanks a lot for the help..but the code didn't display the result.. my table is blank but it didn't have any warning message either... –  LBMG Mar 18 at 7:01
    
foreach($this->data as $d){ $this->blocks[$d['label']][]=array( "start"=>$start = strtotime($d['start']), "end"=>$end = strtotime($d['end']), "class"=>$d['class']); if(!$this->first || $this->first > $start) $this->first = $start; if(!$this->last || $this->last < $end) $this->last = $end; } –  LBMG Mar 18 at 7:04
    
maybe you have problem getting the data in $this->data try to use var_dump($this->data) so you'll see the content of the variable, I'll provide you a running sample of my code so that you can see that its working fine and so that you can have confidence in testing your code in it... –  catzilla Mar 19 at 7:21
    
$this->data has their values.. after i dump it.. –  LBMG Mar 20 at 3:04
    
then what is the array structure inside $this->data.. it should work if its the same as your question above.. –  catzilla Mar 23 at 5:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.