PHP foreach复制具有相同内容的子数组

时间:2017-01-26 17:29:33

标签: php arrays multidimensional-array foreach

我正在使用多维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不会复制子数组?虽然我的第二级键是唯一的,但可能存在两个或更多个第二级键值的情况级别数组是相同的。

2 个答案:

答案 0 :(得分:0)

来自文档:

  

array_search - 在数组中搜索给定值,如果成功则返回第一个对应的键

您可以在输出后删除该索引:

<script async>(function($){
    var $span = $('<span class="fa" style="display:none">`</span>').appendTo('body');
    if ($span.css('fontFamily') !== 'FontAwesome' ) {
    $('head').append('<link href="~/assets/shared/css/vendor/font-awesome-4.7.0.min.css" rel="stylesheet">');
    }
    $span.remove();
    })(window.jQuery);
</script>

我不明白你对这段代码到底想要什么,但这是一个解决方案。我想有更好的方法可以做你想做的事。

答案 1 :(得分:0)

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

如果您希望密钥使用foreach,请执行以下操作:

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