只是想知道是否有办法将数组中的所有内容推送到一个。 例如:
//This will add the first error to array
$error[] = "You must provide a first_name";
//This will add the second error to array
$error[] = "You must provide a last_name";
//This error i want to add at the first index of the array, while pushing the previous two that are already added down a value. But doing it the way i've been doing it just replaces the first index
$error[0] = "This will push the other two errors down one index";
无论如何都没有实际删除数组索引中的第一个值。
固定版本:
//This will add the first error to array
$error[] = "You must provide a first_name";
//This will add the second error to array
$error[] = "You must provide a last_name";
//This error i want to add at the first index of the array, while pushing the previous two that are already added down a value. But doing it the way i've been doing it just replaces the first index
array_unshift($error, "This will push the other two errors down one index");
感谢Sharanya Dutta。
答案 0 :(得分:3)
内置的PHP函数array_unshift
将满足您的目的:
$error = array();
$error[] = "You must provide a first_name";
$error[] = "You must provide a last_name";
array_unshift($error, "This will push the other two errors down one index");
print_r($error);
输出将是:
Array
(
[0] => This will push the other two errors down one index
[1] => You must provide a first_name
[2] => You must provide a last_name
)
答案 1 :(得分:-1)
疯狂的黑客入侵从大价值开始添加。 :^)
$error[1000] = "You must provide a first_name";
$error[] = "You must provide a last_name";
$error[0] = "This will push the other two errors down one index";