如何替换帖子中提交的仅数组元素?
我有一个包含多个复选框的表单,我将所有内容提交到一个PHP页面,我想从另一个中替换一个数组。
形式:
$oldArray = array(
"colors" => array(1 => "mandarin", "strawberry", "blueberry", "kiwi", "banana"),
"other" => array(1 => "test1", "test2", "test3", "test4")
);
目标网页
$newArray = array(
"colors" => array(1 => "orange", "red", "blue", "green", "yellow"),
"other" => array(1 => "example1", "example2", "example3", "example4")
);
print_r( array_replace($_POST['colors'], $newArray['colors']) );
如果我只检查两个元素,“mandarin”和“kiwi”,array_replace
会返回:
Array (
[1] => orange <--- OK
[2] => strawberry
[3] => blueberry
[4] => green <--- OK
[5] => banana
)
我如何echo
仅$_POST
元素?
e.g。
Array (
[1] => orange
[4] => green
)
答案 0 :(得分:1)
你应该发布数组密钥。试试这个:
$oldArray = array(
"colors" => array(1 => "mandarin", "strawberry", "blueberry", "kiwi", "banana"),
"other" => array(1 => "test1", "test2", "test3", "test4")
);
echo '<select name="colors">';
foreach ($oldArray['colors'] as $key => $val)
{
echo '<option value="'.$key.'">'.$val.'</option>';
}
echo '</select>';
然后在您收到的PHP页面中,您可以执行以下操作:
$newArray = array(
"colors" => array(1 => "orange", "red", "blue", "green", "yellow"),
"other" => array(1 => "example1", "example2", "example3", "example4")
);
foreach($_POST['colors'] as $key)
{
echo $newArray['colors'][$key];
}
答案 1 :(得分:0)
尝试:
print_r(array_intersect ($_POST['colors'],$newArray['colors']));