首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP foreach复制内容相同的子数组。

PHP foreach复制内容相同的子数组。
EN

Stack Overflow用户
提问于 2017-01-26 17:29:32
回答 2查看 45关注 0票数 0

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

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

输出:

罗马

伊斯坦布尔

伊斯坦布尔

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

代码语言:javascript
复制
$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不复制子数组?,虽然我的第二级键是唯一的,但在两个或多个二级数组中的值可能是相同的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-01-26 17:34:55

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

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

代码语言:javascript
复制
foreach ($tableArray as $key => $locationTab) {
    echo '<p>' . $key . '</p>';
}
票数 0
EN

Stack Overflow用户

发布于 2017-01-26 17:33:51

从医生那里:

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

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

代码语言:javascript
复制
foreach ($tableArray as $locationTab) {
    $loc = array_search($locationTab, $tableArray);
    unset($tableArray[$loc]);
    echo '<p>' . $loc . '</p>';
}

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

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

https://stackoverflow.com/questions/41879417

复制
相关文章

相似问题

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