【问题标题】:PHP foreach duplicates subarrays with identical contentPHP foreach 复制具有相同内容的子数组
【发布时间】:2017-06-12 06:37:15
【问题描述】:

我正在使用多维 php 数组为 html 生成提供数据,当我的两个子数组(具有不同的键)包含相同的值时,我注意到一些奇怪的行为。例如,这个数组产生重复:

$tableArray = Array(
    'rome' => Array(
        0 => Array(
            'home_prefix' => 'AWE',
            'home_number' => '122',
            'home_title' => 'Beginning Stretching',
            'abroad_prefix' => 'ARCH',
            'abroad_number' => '111',
            'abroad_title' => 'Intro to stuff'
        )
    ),
    'istanbul' => Array(
        0 => Array(
            'home_prefix' => 'RPED',
            'home_number' => '103',
            'home_title' => 'Beginning Stretching',
            'abroad_prefix' => 'ARCH',
            'abroad_number' => '111',
            'abroad_title' => 'Intro to Greek concepts of stretchiness'
        ),
        1 => Array(
            'home_prefix' => 'RPED',
            'home_number' => '104',
            'home_title' => 'Theory of Stretching',
            'abroad_prefix' => 'ARCH',
            'abroad_number' => '111',
            'abroad_title' => 'Intro to concepts of stretchiness'
        )
    ),
    'new york' => Array(
        0 => Array(
            'home_prefix' => 'RPED',
            'home_number' => '103',
            'home_title' => 'Beginning Stretching',
            'abroad_prefix' => 'ARCH',
            'abroad_number' => '111',
            'abroad_title' => 'Intro to Greek concepts of stretchiness'
        ),
        1 => Array(
            'home_prefix' => 'RPED',
            'home_number' => '104',
            'home_title' => 'Theory of Stretching',
            'abroad_prefix' => 'ARCH',
            'abroad_number' => '111',
            'abroad_title' => 'Intro to concepts of stretchiness'
        )
    )
);

foreach ($tableArray as $locationTab):
                echo '<p>' . array_search($locationTab, $tableArray) . '</p>';
endforeach;

输出:

罗马

伊斯坦布尔

伊斯坦布尔

但是当我添加另一个子数组时,最后两个数组不相同,没有重复:

$tableArray = Array(
    'rome' => Array(
        0 => Array(
            'home_prefix' => 'AWE',
            'home_number' => '122',
            'home_title' => 'Beginning Stretching',
            'abroad_prefix' => 'ARCH',
            'abroad_number' => '111',
            'abroad_title' => 'Intro to stuff'
        )
    ),
    'istanbul' => Array(
        0 => Array(
            'home_prefix' => 'RPED',
            'home_number' => '103',
            'home_title' => 'Beginning Stretching',
            'abroad_prefix' => 'ARCH',
            'abroad_number' => '111',
            'abroad_title' => 'Intro to Greek concepts of stretchiness'
        ),
        1 => Array(
            'home_prefix' => 'RPED',
            'home_number' => '104',
            'home_title' => 'Theory of Stretching',
            'abroad_prefix' => 'ARCH',
            'abroad_number' => '111',
            'abroad_title' => 'Intro to concepts of stretchiness'
        )
    ),
    'new york' => Array(
        0 => Array(
            'home_prefix' => 'RPED',
            'home_number' => '103',
            'home_title' => 'Beginning Stretching',
            'abroad_prefix' => 'ARCH',
            'abroad_number' => '111',
            'abroad_title' => 'Intro to Greek concepts of stretchiness'
        ),
        1 => Array(
            'home_prefix' => 'RPED',
            'home_number' => '104',
            'home_title' => 'Theory of Stretching',
            'abroad_prefix' => 'ARCH',
            'abroad_number' => '111',
            'abroad_title' => 'Intro to concepts of stretchiness'
        ),
        2 => Array(
            'home_prefix' => 'RPED',
            'home_number' => '104',
            'home_title' => 'Theory of Stretching',
            'abroad_prefix' => 'ARCH',
            'abroad_number' => '111',
            'abroad_title' => 'Intro to concepts of stretchiness'
        )
    )
);

输出:

罗马

伊斯坦布尔

纽约

我该如何解决这个问题,以便 foreach 不会复制子数组?虽然我的二级键是唯一的,但可能存在两个或多个二级键中的值的情况级别数组是相同的。

【问题讨论】:

  • 我已经编辑了我的答案。我忘记使用新创建的$key 变量

标签: php arrays multidimensional-array foreach


【解决方案1】:

来自文档:

array_search — 在数组中搜索给定的值,如果成功则返回 first 对应的键

您可以在这里做的是在输出后删除该索引:

foreach ($tableArray as $locationTab) {
    $loc = array_search($locationTab, $tableArray);
    unset($tableArray[$loc]);
    echo '<p>' . $loc . '</p>';
}

我不完全了解您对这段代码的要求,但这是一个解决方案。我想有更好的方法来做你想做的事。

【讨论】:

    【解决方案2】:

    函数array_search 返回数组中与搜索模式匹配的第一个元素。这意味着如果两个元素具有相同的值,则在第二个元素上使用 array_search 将始终返回第一个元素的键

    如果您想要密钥,请像这样使用foreach

    foreach ($tableArray as $key => $locationTab) {
        echo '<p>' . $key . '</p>';
    }
    

    【讨论】:

      猜你喜欢
      • 2013-11-13
      • 1970-01-01
      • 1970-01-01
      • 2021-12-10
      • 2015-01-26
      • 2012-10-21
      • 2015-03-29
      • 1970-01-01
      • 2011-09-18
      相关资源
      最近更新 更多