Parsing array values into multidimensional array

I have some data I retrieve from a JSON feed that currently is being parsed into an array like this: (simplifying for demonstration purposes)

So pretty much an array returns a movie theater name, with the showtimes associated with that particular theater.

        [0] => American Theater
        [1] => 2014-06-04T13:10
        [2] => 2014-06-04T15:10
        [3] => Grand Theater
        [4] => 2014-06-04T15:30
        [5] => 2014-06-04T19:10

How would I parse this array to be multidimensional, for instance:

Array
(
    [0] => Array
    (

        [theater] => Array
            (
                [name] => American Theater
            )
        [showtimes] => Array
            (
                [1] => 2014-06-04T13:10
                [2] => 2014-06-04T15:10
            )

    )
    [1] => Array
    (

        [theater] => Array
            (
                [name] => Grand Theater
            )
        [showtimes] => Array
            (
                [1] => 2014-06-04T15:30
                [2] => 2014-06-04T19:10
            )

    )

 )

Answers


I'm assuming you're trying to access some api and have no control over how the data is passed back to you? If you do then the API should be responsible for returning a sensible schema.

But if you're forced to work with this array and the amount of showtimes are unknown to you, then you can do something like this:

$array = array(
    'American Theater', 
    '2014-06-04T13:10',
    '2014-06-04T15:10',
    'Grand Theater', 
    '2014-06-04T15:30',
    '2014-06-04T19:10'
);

$i = 0;

foreach ($array as $key => $value) {

    if (strtotime($value)) {
        $theaters[$i - 1]['showtimes'][] = $value;
    }
    else {
        $theaters[$i]['theater']['name'] = $value;
        $i++;
    }

}

Outcome

To walk you through it, $array is whatever the returned dataset is. We set an index in the $i value and want to only increment it if we determine we've detected a theater name. Within the loop we first try to determine if the string can be converted to a php time value. If it cannot we add the theater name to our new schema structure, and increment our index value. Since times are always added to theater names, we are expecting the first index number to always be one higher than what we want to add the showtime to.

This will fail to be accurate in cases when a theater name is convertible to a time value in such cases like Next Month. There are a couple of other ways to solve this with regex or by inspecting the string for certain characters and their position since the time format will remain the same.

You could replace the strtotime() with:

$str = str_split($value);
if (($str[4] && $str[7]) == '-' && $str[10] == 'T' && $str[13] == ':' ) {
    $theaters[$i - 1]['showtimes'][] = $value;
}

If you want such structure, you need to create a new copy of it. You may also need to chunk/group them by three's using array_chunk first, and then, from there, you can loop it now and start creating the desired format.

Consider this example:

$old_values = array('American Theater', '2014-06-04T13:10', '2014-06-04T15:10', 'Grand Theater', '2014-06-04T15:30', '2014-06-04T19:10');

$old_values = array_chunk($old_values, 3);
$new_values = array();
foreach($old_values as $key => $value) {
    $new_values[] = array(
        'theater' => array('name' => $value[0]),
        'showtimes' => array(1 => $value[1], 2 => $value[2]),
    );
}

Edit: As mentioned, one theater can have many showtimes, therefore this current solution will fail. This may be an alternative (you may need to check each element if its a theater name or a date). Consider this example:

$old_values = array(
    'American Theater',
    '2014-06-04T13:10',
    '2014-06-04T15:10',
    'Grand Theater',
    '2014-06-04T15:30',
    '2014-06-04T19:10',
    'Magic Johnson Theater',
    '2014-06-04T19:10',
    '2014-06-04T19:10',
    '2014-06-04T19:10',
    'Mall of America Theater',
    '2014-06-04T19:10',
    '2014-06-04T19:10',
    '2014-06-04T19:10',
    '2014-06-04T19:10',
);

$new_values = array();
$current_key = 0;
foreach($old_values as $key => $value) {
    $current_value = $value;
    $pieces = explode('T', $current_value);
    $dates = explode('-', $pieces[0]);
    if(count($dates) == 3) {
        $new_values[$current_key]['showtimes'][] = $current_value;
    } else {
        $current_key++;
        $new_values[$current_key]['theater']['name'] = $current_value;
    }
}

Sample Output:

Array
(
    [1] => Array
        (
            [theater] => Array
                (
                    [name] => American Theater
                )

            [showtimes] => Array
                (
                    [0] => 2014-06-04T13:10
                    [1] => 2014-06-04T15:10
                )

        )

    [2] => Array
        (
            [theater] => Array
                (
                    [name] => Grand Theater
                )

            [showtimes] => Array
                (
                    [0] => 2014-06-04T15:30
                    [1] => 2014-06-04T19:10
                )

        )

    [3] => Array
        (
            [theater] => Array
                (
                    [name] => Magic Johnson Theater
                )

            [showtimes] => Array
                (
                    [0] => 2014-06-04T19:10
                    [1] => 2014-06-04T19:10
                    [2] => 2014-06-04T19:10
                )

        )

    [4] => Array
        (
            [theater] => Array
                (
                    [name] => Mall of America Theater
                )

            [showtimes] => Array
                (
                    [0] => 2014-06-04T19:10
                    [1] => 2014-06-04T19:10
                    [2] => 2014-06-04T19:10
                    [3] => 2014-06-04T19:10
                )

        )

)

Sample Fiddle


Need Your Help

web.xml error-pages are not working, why?

java jsf glassfish web.xml

I'm running out of ideas by now, and don't know what else to try. The point is not even one error-page is working, I just would like to know, what am I doing wrong?, here is the code:

strtok not working as expected

c strtok

I tried to write a function that gets a pointer to array of char, reads a string from the user and remove all spaces in the start of the string till the first char that is not string appear. Finally

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.