首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将数组值解析为多维数组

将数组值解析为多维数组
EN

Stack Overflow用户
提问于 2014-06-04 23:50:03
回答 2查看 48关注 0票数 0

我从JSON提要中检索到一些数据,这些数据目前正被解析为如下数组:(为演示目的而简化)

因此,数组会返回一个电影院名称,其中包含与该特定影院相关的显示时间。

代码语言:javascript
复制
        [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

例如,如何将该数组解析为多维数组:

代码语言:javascript
复制
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
            )

    )

 )
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-06-05 00:47:02

我假设您正在尝试访问一些api,而无法控制数据是如何被传递回您的?如果您这样做了,那么API应该负责返回一个合理的模式。

但是,如果您被迫使用这个数组,而且显示时间的数量对您来说是未知的,那么您可以这样做:

代码语言:javascript
复制
$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++;
    }

}

结果

要遍历它,无论返回的数据集是什么,$array都是。我们在$i值中设置了一个索引,并且只希望在确定已经检测到剧院名称的情况下增加索引。在循环中,我们首先尝试确定字符串是否可以转换为php时间值。如果不能,我们将剧院名称添加到新的架构结构中,并增加索引值。由于时间总是被添加到剧院名称中,我们期望第一个索引数总是比我们想要添加的时间高一个。

在像Next Month这样的情况下,当剧院名称可转换为时间值时,这将是不准确的。还有几种其他方法可以用regex来解决这个问题,或者检查字符串中的某些字符及其位置,因为时间格式将保持不变。

您可以将strtotime()替换为:

代码语言:javascript
复制
$str = str_split($value);
if (($str[4] && $str[7]) == '-' && $str[10] == 'T' && $str[13] == ':' ) {
    $theaters[$i - 1]['showtimes'][] = $value;
}
票数 1
EN

Stack Overflow用户

发布于 2014-06-05 00:03:17

如果您想要这样的结构,您需要创建一个新的副本。您还可能需要首先使用array_chunk按三个块/组对它们进行分组,然后从那里开始循环它并开始创建所需的格式。

考虑一下这个例子:

代码语言:javascript
复制
$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]),
    );
}

编辑:正如前面提到的,一个剧院可以有很多次展示,因此这个当前的解决方案将失败。这可能是另一种选择(您可能需要检查每个元素,如果是剧院名称或日期)。考虑一下这个例子:

代码语言:javascript
复制
$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;
    }
}

样本输出:

代码语言:javascript
复制
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
                )

        )

)

样品填充

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24049450

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档