Steven
Steven

Reputation: 13975

Multidimensional Array Pushing/Adding?

I want to create something like the following array

[Schedule_Date_Group] => Array
            (
                [Schedule_Date] => Array
                    (
                        [Friday, September 16, 2011] => Array
                            (
                                [Schedule_Item] => Array
                                    (
                                        [nid] => 763
                                        [time] => 1:15 PM
                                        [title] => What a Publisher Does: 5 Reasons Why You Need a...
                                        [event_type] => events
                                        [length] => 
                                        [movie_type] => 
                                        [details] => 
                                    )

                                [Schedule_Item] => Array
                                    (
                                        [nid] => 763
                                        [time] => 1:15 PM
                                        [title] => What a Publisher Does: 5 Reasons Why You Need a...
                                        [event_type] => events
                                        [length] => 
                                        [movie_type] => 
                                        [details] => 
                                    )

                            )

                    )

            )

But I have a few issues, first the array seems to be getting created with a preceding # for the first value. Example

[7] => Array
    (
        [Schedule_Date_Group] => Array
            (

And my arrays are not pushing it under the Date Array ([Friday, September 16, 2011] => Array) They are just being added to the end as a normal array. Example

[7] => Array
    (
        [Schedule_Date_Group] => Array
            (
                [Schedule_Date] => Array
                    (
                        [Friday, September 16, 2011] => Array
                            (
                                [Schedule_Item] => Array
                                    (
                                        [nid] => 763
                                        [time] => 1:15 PM
                                        [title] => What a Publisher Does: 5 Reasons Why You Need a...
                                        [event_type] => events
                                        [length] => 
                                        [movie_type] => 
                                        [details] => 
                                    )

                            )

                    )

            )

    )

[8] => Array
    (
        [Schedule_Item] => Array
            (
                [nid] => 764
                [time] => 1:30 PM
                [title] => Navigating the Road to Licensing Music For Your...
                [event_type] => events
                [length] => 
                [movie_type] => 
                [details] => 
            )

    )

How can I fix these two issues. They are again, #'s preceding the Schedule_Date_Group array and the sub arrays being added to the end rather then nested under the date group array.

PHP For the main schedule item and date group part

$xml[] = array("Schedule_Date_Group" => array("Schedule_Date" => array($pretty_date => array("Schedule_Item" => array("nid" => $do['nid'], "time" => $pretty_time, "title" => $title, "event_type" => $do['field_event_type_value'], "length" => $do['field_length_value'], "movie_type" => $do['field_movie_type_value'], "details" => $schedule_details)))));

PHP for the sub menu items

$xml[] = array("Schedule_Item" => array("nid" => $do['nid'], "time" => $pretty_time, "title" => $title, "event_type" => $do['field_event_type_value'], "length" => $do['field_length_value'], "movie_type" => $do['field_movie_type_value'], "details" => $schedule_details));

It is being looped through so there is no way for me to just create a giant array. And if a new "Schedule Date" is set it will create a new [Schedule_Date_Group] => Array ( [Schedule_Date] => Array ( [Friday, September 16, 2011] => Array ( and all the sub content should go under that new one. So I would end up with

DATE
 - Schedule_Item 1
 - Schedule_Item 2
 - Schedule_Item 3
 - Schedule_Item 4
New Date
 - Schedule Item 5
 - Schedule Item 6

etc...

Any help?

Upvotes: 0

Views: 314

Answers (1)

DaveRandom
DaveRandom

Reputation: 88667

Firstly, using a huge array to manually generate some XML like this is not the way to do it. Use something like XMLWriter or DOM instead, then you can build your document on the fly as you obtain your data. However, if you really want to, or are forced to do it like this, read on...


Secondly, what you are trying to do cannot be done. This is because you want use the same array key for multiple entries which won't work - you will just end up overwriting your previous entry.

Thirdly, your numeric keys are appearing because you are using $xml[] (array_push() behaves in the same way) and it will always add a numeric key because you have not told it what you want your text key to be.

Fourthly, your extra items are being added to the outer level of the array because that is what you have told PHP to do. $xml[] will always add a new key to the outer level of the $xml variable because you have not told PHP you are dealing with an inner array.

Your structure needs to be more like this:

$scheduleDateGroup = array (
  'Friday, September 16, 2011' => array (
    // These are your schedule items...
    0 => array( ... ),
    1 => array( ... ),
    2 => array( ... ),
    ...
  ),
  'Saturday, September 17, 2011' => array (
    0 => array( ... ),
    1 => array( ... ),
    2 => array( ... ),
    ...
  ),
  ...
);

...and you can push new items onto specific days like this:

$scheduleDateGroup[$date][] = array( ... );

Then you can loop through it and turn it into XML with something like this:

echo "<Schedule_Date_Group>\n";
foreach ($scheduleDateGroup as $day => $schedules) {
  echo "  <Schedule_Date date=\"$day\">\n";
  foreach ($schedules as $item) {
    echo "    <Schedule_Item";
    foreach ($item as $attr => $value) echo " $attr=\"$value\"";
    echo " />\n";
  }
  echo "  </Schedule_Date>\n";
}
echo "</Schedule_Date_Group>";

Upvotes: 1

Related Questions