将一个数组中的某些值与另一个数组中的值组合
Combine certain values in one array with values in another array
我想将 $url
中包含前缀 lt_
的值与 $color
中的值合并,并创建一个新数组 $new
。 $url
和 $color
数组都存储接下来 36 小时的值:
print_r($url);
产出
Array(
[0] => "http://example.com/color/green.png",
[1] => "http://example.com/color/lt_green.png",
[2] => "http://example.com/color/lt_blue.png",
[3] => "http://example.com/color/blue.png",
[4] => "http://example.com/color/blue.png",
[5] => "http://example.com/color/yellow.png",
..
[35] => "http://example.com/color/lt_blue.png",
);
和
print_r($color);
产出
Array(
[0] => "Green",
[1] => "Green",
[2] => "Blue",
[3] => "Blue",
[4] => "Blue",
[5] => "Yellow",
...
[35] => "Blue",
);
我设法在 $url
中找到了字符串部分 lt_
并在 $new
中找到了键,但是我如何添加 "Light" (或 lt_
) 到 $color
中的相应值以填充 $new
?
$new = array(
[0] => "Green",
[1] => "Light Green",
[2] => "Light Blue",
[3] => "Blue",
[4] => "Blue",
[5] => "Yellow",
...
[35] => "Light Blue",
);
我成功地创建了一个数组,其中只有键值包含 lt_
:
$lt = 'lt_';
foreach($url as $key1=>$value1)
{
foreach($color as $key2=>$value2)
{
if (strpos($value1,$lt) !== false)
{
$new[$key1] = array();
}
}
}
如果两个数组的大小相同且索引对应,则可以根据条件迭代并向新数组添加带前缀或不带前缀的颜色:
$arr = Array("http://example.com/color/green.png",
"http://example.com/color/lt_green.png",
"http://example.com/color/lt_blue.png",
"http://example.com/color/blue.png",
"http://example.com/color/blue.png",
"http://example.com/color/yellow.png",);
$arr2 = Array(
"Green",
"Green",
"Blue",
"Blue",
"Blue",
"Yellow",);
$new = array();
for ($i = 0; $i < count($arr2); $i += 1) {
(strpos($arr[$i], 'lt_') !== false) ?
$new[] = "Light " . $arr2[$i] :
$new[] = $arr2[$i];
}
print_r($new);
我想将 $url
中包含前缀 lt_
的值与 $color
中的值合并,并创建一个新数组 $new
。 $url
和 $color
数组都存储接下来 36 小时的值:
print_r($url);
产出
Array(
[0] => "http://example.com/color/green.png",
[1] => "http://example.com/color/lt_green.png",
[2] => "http://example.com/color/lt_blue.png",
[3] => "http://example.com/color/blue.png",
[4] => "http://example.com/color/blue.png",
[5] => "http://example.com/color/yellow.png",
..
[35] => "http://example.com/color/lt_blue.png",
);
和
print_r($color);
产出
Array(
[0] => "Green",
[1] => "Green",
[2] => "Blue",
[3] => "Blue",
[4] => "Blue",
[5] => "Yellow",
...
[35] => "Blue",
);
我设法在 $url
中找到了字符串部分 lt_
并在 $new
中找到了键,但是我如何添加 "Light" (或 lt_
) 到 $color
中的相应值以填充 $new
?
$new = array(
[0] => "Green",
[1] => "Light Green",
[2] => "Light Blue",
[3] => "Blue",
[4] => "Blue",
[5] => "Yellow",
...
[35] => "Light Blue",
);
我成功地创建了一个数组,其中只有键值包含 lt_
:
$lt = 'lt_';
foreach($url as $key1=>$value1)
{
foreach($color as $key2=>$value2)
{
if (strpos($value1,$lt) !== false)
{
$new[$key1] = array();
}
}
}
如果两个数组的大小相同且索引对应,则可以根据条件迭代并向新数组添加带前缀或不带前缀的颜色:
$arr = Array("http://example.com/color/green.png",
"http://example.com/color/lt_green.png",
"http://example.com/color/lt_blue.png",
"http://example.com/color/blue.png",
"http://example.com/color/blue.png",
"http://example.com/color/yellow.png",);
$arr2 = Array(
"Green",
"Green",
"Blue",
"Blue",
"Blue",
"Yellow",);
$new = array();
for ($i = 0; $i < count($arr2); $i += 1) {
(strpos($arr[$i], 'lt_') !== false) ?
$new[] = "Light " . $arr2[$i] :
$new[] = $arr2[$i];
}
print_r($new);