需要比较两个数组
工作示例
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
Array1输出:
Array ( [a] => green [0] => red [1] => blue )
当我这样做时
$array1 = array();
while($fetch = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))
{
$array1[] = $fetch['color'];
}
我得到了这个输出:
Array ([0] => gren [1] => red [2] blue
如何在数组中添加“a”并将第一个颜色设为零?
这会添加“a”但它会得到零号
array_unshift($array1,"a");
LIKE
Array ( [0] => a [1] => green
我想要这个
Array ( [a] => green [0]
答案 0 :(得分:0)
我不确定你为什么要这样做,但这是如何:
$array1 = array();
while ($fetch = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))
if (empty($array1)) {
$array1['a'] = $fetch['color'];
} else {
$array1[] = $fetch['color'];
}
}
答案 1 :(得分:0)
$arr = array(0 => 'green', 1 => 'red', 2 => 'blue');
$res = array_merge(array('a' => current($arr)), array_slice($arr, 1));
答案 2 :(得分:0)
您可以使用array_merge
和array_shift
功能:
$arr = array(0 => 'green', 1 => 'red', 2 => 'blue');
$new = array_merge(array('a' => array_shift($arr)), $arr);