我使用一个多维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不复制子数组?,虽然我的第二级键是唯一的,但在两个或多个二级数组中的值可能是相同的。
发布于 2017-01-26 17:34:55
函数array_search返回与搜索模式匹配的数组中的第一个元素。这意味着,如果两个元素的值相同,那么对第二个元素使用array_search将始终返回第一个元素的键。
如果您想要密钥,请像这样使用foreach:
foreach ($tableArray as $key => $locationTab) {
echo '<p>' . $key . '</p>';
}发布于 2017-01-26 17:33:51
从医生那里:
array_search -搜索数组中给定的值,如果成功返回first对应的键
在这里您可以做的是在输出之后删除该索引:
foreach ($tableArray as $locationTab) {
$loc = array_search($locationTab, $tableArray);
unset($tableArray[$loc]);
echo '<p>' . $loc . '</p>';
}我不明白你到底想用这段代码做什么,但这是一个解决方案。我想有更好的方法来做你想做的事。
https://stackoverflow.com/questions/41879417
复制相似问题