我试图通过for循环运行一个数组,将检查的项目附加到每个数组上,每个数组附加后缀为$ n(1-3)。谢谢你的建议我更近了一步,现在我得到一个项目的每个后缀。我希望每个人都在数组中作为自己的项目。任何人都可以看到错误吗?
我已经更新了我的代码,它离解决方案更近了一步,它正是我的目标。
$equip = 'Phone';
$ailments_checkvar = explode(', ', 'Cracked, Scratched, Bent, Twisted');
foreach ($ailments_checkvar as &$value) {
$value = 'directory/'.$equip.'_'.$value.'';
}
unset($value);
$duplicateArray = $ailments_checkvar;
foreach ($ailments_checkvar as $key) {
$duplicateArray[] = $key;
}
foreach ($ailments_checkvar as $key) {
$duplicateArray[] = $key;
}
for ($n = 1; $n <= 3; $n++) {
foreach ($duplicateArray as &$valueN) {
$valueN = $valueN.'_0'.$n.'.pdf';
}
}
unset($valueN);
print_r ($duplicateArray);
获取此
Array ( [0] => directory/Phone_Cracked_01.pdf_02.pdf_03.pdf
[1] => directory/Phone_Scratched_01.pdf_02.pdf_03.pdf
[2] => directory/Phone_Bent_01.pdf_02.pdf_03.pdf
[3] => directory/Phone_Twisted_01.pdf_02.pdf_03.pdf
[4] => directory/Phone_Cracked_01.pdf_02.pdf_03.pdf
[5] => directory/Phone_Scratched_01.pdf_02.pdf_03.pdf
[6] => directory/Phone_Bent_01.pdf_02.pdf_03.pdf
[7] => directory/Phone_Twisted_01.pdf_02.pdf_03.pdf
[8] => directory/Phone_Cracked_01.pdf_02.pdf_03.pdf
[9] => directory/Phone_Scratched_01.pdf_02.pdf_03.pdf
[10] => directory/Phone_Bent_01.pdf_02.pdf_03.pdf
[11] => directory/Phone_Twisted_01.pdf_02.pdf_03.pdf
)
想要做到这一点......
Array (
[0] => directory/Phone_Cracked_01.pdf
[1] => directory/Phone_Cracked_02.pdf
[2] => directory/Phone_Cracked_03.pdf
[3] => directory/Phone_Scratched_01.pdf
[4] => directory/Phone_Scratched_02.pdf
[5] => directory/Phone_Scratched_03.pdf
[6] => directory/Phone_Bent_01.pdf
[7] => directory/Phone_Bent_02.pdf
[8] => directory/Phone_Bent_03.pdf
[9] => directory/Phone_Twisted_01.pdf
[10] => directory/Phone_Twisted_02.pdf
[11] => directory/Phone_Twisted_03.pdf
)
答案 0 :(得分:0)
请看这里的陈述,
$valueN = $ailments_checkvar.'_0'.$n.'.pdf';
^^^^^^^^^^^^^^^^^^
$ailments_checkvar
是一个数组,而不是一个字符串。在上面的语句中,您尝试将数组转换为字符串。它应该是,
$valueN = $valueN.'_0'.$n.'.pdf';
根据您在上面发布的要求,解决方案将是这样的:
$equip = 'Phone';
$ailments_checkvar = explode(', ', 'Cracked, Scratched, Bent, Twisted');
$path_array = array();
foreach($ailments_checkvar as $property_status){
for($i = 1; $i <= 3; ++$i){
$path_array[] = 'directory/' . $equip . '_' . $property_status . '_0' . $i . '.pdf';
}
}
echo '<pre>'; print_r ($path_array);
这里是live demo。
答案 1 :(得分:0)
for ($z=1;$z<=count($ailments_checkvar);$z++) {
$ailments_checkvar[$z-1] = $ailments_checkvar[$z-1].'_0'.$z.'.pdf';
}
unset($z);
你曾经做过什么导致$ ailments_checkvar的堆栈问题 请查看这是优化代码。
你的错误在于$ valueN = $ ailments_checkvar .'_ 0'。$ n。'。pdf';