如何组合这两个数组?
$array1 = array("gif" => "gif", "jpg" => "jpeg", "jpeg" => "jpeg", "png" =>"png");
$array2 = array("gif" => "0", "jpg" => "90", "jpeg" => "90", "png" => "8");
我试过
$array1 = array("gif" => "gif" => "0", "jpg" => "jpeg" => "90", "jpeg" => "jpeg" => "90", "png" =>"png" => "8");
但当然没有任何帮助吗?
答案 0 :(得分:6)
这似乎更有意义:
$arr = array("gif" => array("extension" => "gif", "size" => "90"),
"jpg" => array("extension" => "jpeg", "size" => "120")
...
);
echo "Extension: " . $arr['gif']['extension'] . " Size or whatever: " . $arr['gif']['size'];
循环遍历:
foreach($arr as $key => $val) {
echo "Image Type: $key, Extension: " . $val['extension'] . ", Size: " . $val['size'];
}
答案 1 :(得分:2)
每个键都有两个值。为具有多个值的每个键创建一个子数组:
$ar1 = array( 'key' => array('key1','key2') );
答案 2 :(得分:1)
要建立我以前的答案,你最好使用并行数组:
$validTypes = array("gif" => "gif", "jpg" => "jpeg", "jpeg" => "jpeg", "png" => "png");
$quality = array("gif" => 0, "jpg" => 90, "jpeg" => 90, "png" => "png");
if (!array_key_exists($image_type, $validTypes)) {
trigger_error("Not a valid image type", E_USER_WARNING);
return NULL;
}
// ...
$inFunc = "imagecreatefrom" . $validTypes[$image_type];
$outFunc = "image" . $validTypes[$image_type];
// ...
$outFunc($image, $source_file, $quality[$image_type]);
不过,卡里姆的回答也很好。只是一个偏好问题。
答案 3 :(得分:1)
$array1 = array("gif" => "gif", "jpg" => "jpeg", "jpeg" => "jpeg", "png" =>"png");
$array2 = array("gif" => "0", "jpg" => "90", "jpeg" => "90", "png" => "8");
$finalarray = (array_merge_recursive($array1,$array2));
$ finalarray的结构是:
Array
(
[gif] => Array
(
[0] => gif
[1] => 0
)
[jpg] => Array
(
[0] => jpeg
[1] => 90
)
[jpeg] => Array
(
[0] => jpeg
[1] => 90
)
[png] => Array
(
[0] => png
[1] => 8
)
)
答案 4 :(得分:0)
不确定如何“组合”它们。我将假设您希望第一个数组中的每个键映射到第二个
中的值 foreach($array1 as $k=>$v)
{
$a3[$k] = $array2[$v]
}
给你
$a3 = array('gif'=>'0', 'jpg'=>'90', 'jpeg'=>'90', 'png'=>'8');
但这与array2相同。 也许
foreach($array1 as $k=>$v)
{
$a3[$k] = array($array1[$k],$array2[$k]);
}
给出
$a3 = array('gif'=>array('gif',0),'jpg'=>array('jpeg',90)...)
答案 5 :(得分:0)
我很惊讶,没有一个答案提到你的下面,这些答案基本上可以描述为"everything you ever wanted to know about PHP arrays but were afraid to ask"