1

I'm trying to make a multidimensional array that should print out like this (without formatting):

array(3) {
  [0]=> array(5) {
    [0]=> int(0) 
    [1]=> string(5) "Arena" 
    [2]=> string(18) "2012-05-3017:00:00" 
    [3]=> string(18) "2012-05-3000:00:00" 
    [4]=> string(33) "Masquerade Checkin (Participants)" 
}, 

  [1]=> array(5) {
    [0]=> int(0) 
    [1]=> string(10) "Workshop 1"
    [2]=> string(18) "2012-05-3017:00:00"
    [3]=> string(18) "2012-05-3000:00:00"
    [4]=> string(15) "Death Note (Live)" 
},

  [2]=> array(5) {
    [0]=> int(0) 
    [1]=> string(7) "Video 6" 
    [2]=> string(18) "2012-05-3017:00:00"
    [3]=> string(18) "2012-05-3000:00:00"
    [4]=> string(26) "Takeuchi Fan Panel" 
  }
}

Notice from the above code that the inner array() length is always 5.

Here is my code below:

$loopsArray = array();
$data=array();
// graphing info come in here. 
foreach ($events as $key => $event) {

    $el=$event['event_location'] ;
    $eln=$event['event_locationName'];
    $ed=$event['event_date'];
    $es=$event['event_start'];
    $ee=$event['event_end'];
    $en=$event['event_name'];

    array_push($loopsArray,$el,$eln, $ed.$es,$ed.$ee,$en);
    array_push($data,$loopsArray);


}
var_dump($data);

Here the print out

array(27) {
  [0]=> array(5) {
    [0]=> int(0)
    [1]=> string(5) "Arena"
    [2]=> string(18) "2012-05-3017:00:00"
    [3]=> string(18) "2012-05-3000:00:00"
    [4]=> string(33) "Masquerade Checkin (Participants)" 
  } 

  [1]=> array(10) { 
    [0]=> int(0) 
    [1]=> string(5) "Arena" 
    [2]=> string(18) "2012-05-3017:00:00"
    [3]=> string(18) "2012-05-3000:00:00"
    [4]=> string(33) "Masquerade Checkin (Participants)"
    [5]=> int(13)
    [6]=> string(11) "Autograph 1"
    [7]=> string(18) "2012-06-2419:00:00"
    [8]=> string(18) "2012-06-2422:00:00"
    [9]=> string(17) "Parents and Anime" 
  } 
  //... continues 
}

Notice that the inner arrays length double each iteration. array(5) array(10) array(15)array(20).

It doubles up to 60 elements in the last inner array. Each inner array should only have 5 elements in them. I don't understand why it is doubling or how to fix it. Can you look over my loop and let me know how to fix it?

I have to use this multidimensional array for this code to work in JpGraph.

2
  • 2
    quick tip : write $loopsArray = array(); inside foreach Commented Jul 23, 2012 at 19:15
  • 1
    you are using $loopsArray as buffer each step without freeing what you pushed previously. In fact it increments its size by 5 each iteration. Commented Jul 23, 2012 at 19:26

1 Answer 1

2

TIP : write $loopsArray = array(); inside foreach

better approach

instead of

array_push($loopsArray,$el,$eln, $ed.$es,$ed.$ee,$en);
array_push($data,$loopsArray);

try this

$temp = array ($el,$eln, $ed.$es,$ed.$ee,$en);
$data[] = $temp;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.