Leoa
Leoa

Reputation: 1187

Fixing multidimensional array

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.

Upvotes: 1

Views: 143

Answers (1)

xkeshav
xkeshav

Reputation: 54050

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;

Upvotes: 2

Related Questions