在 php 中使用 2 个不同的数组创建一个新数组
Create a new array with using 2 different arrays in php
我有 2 个这样的数组。数组 1:
Array
(
[abu-garcia] => 1
[daiwa] => 4
[shimano] => 4
)
Array 2:
Array
(
[0] => Array
(
[brand_id] => 36
[brand_name] => Abu Garcia
[brand_slug] => abu-garcia
)
[1] => Array
(
[brand_id] => 41
[brand_name] => Daiwa
[brand_slug] => daiwa
)
[2] => Array
(
[brand_id] => 41
[brand_name] => Daiwa
[brand_slug] => daiwa
)
[3] => Array
(
[brand_id] => 41
[brand_name] => Daiwa
[brand_slug] => daiwa
)
[4] => Array
(
[brand_id] => 41
[brand_name] => Daiwa
[brand_slug] => daiwa
)
[5] => Array
(
[brand_id] => 39
[brand_name] => Shimano
[brand_slug] => shimano
)
[6] => Array
(
[brand_id] => 39
[brand_name] => Shimano
[brand_slug] => shimano
)
[7] => Array
(
[brand_id] => 39
[brand_name] => Shimano
[brand_slug] => shimano
)
我想创建一个如下所示的新数组:
Array
(
[0] => Array
(
[brand_id] => 36
[brand_name] => Abu Garcia
[brand_slug] => abu-garcia
[count] => 1
)
[1] => Array
(
[brand_id] => 41
[brand_name] => Daiwa
[brand_slug] => daiwa
[count] => 4
)
[2] => Array
(
[brand_id] => 41
[brand_name] => Daiwa
[brand_slug] => daiwa
[count] => 4
)
[3] => Array
(
[brand_id] => 41
[brand_name] => Daiwa
[brand_slug] => daiwa
[count] => 4
)
[4] => Array
(
[brand_id] => 41
[brand_name] => Daiwa
[brand_slug] => daiwa
[count] => 4
)
[5] => Array
(
[brand_id] => 39
[brand_name] => Shimano
[brand_slug] => shimano
[count] => 4
)
[6] => Array
(
[brand_id] => 39
[brand_name] => Shimano
[brand_slug] => shimano
[count] => 4
)
[7] => Array
(
[brand_id] => 39
[brand_name] => Shimano
[brand_slug] => shimano
[count] => 4
)
这两个数组都是由 wordpress 创建的。 array1 是检查品牌猫中产品数量时函数的输出,其他数组提供品牌详细信息。
谢谢
我想我明白你想做什么...
如果这些是您的数组示例:
$CountArray = array("abu-garcia"=>1,
"daiwa"=>4,
"shimano"=>4);
$DataArray = array(
array("brand_id"=>36, "brand_name"=>"Abu Garcia", "brand_slug"=>"abu-garcia"),
array("brand_id"=>41, "brand_name"=>"Daiwa", "brand_slug"=>"daiwa"),
array("brand_id"=>39, "brand_name"=>"Shimano", "brand_slug"=>"shimano")
);
然后遍历第二个数组以构建一个新数组,并像这样从第一个数组添加 count
键:
$i = 0;
foreach($DataArray as $ItemArray){
foreach($ItemArray as $Key=>$Value){
$NewArray[$i][$Key] = $Value;
$NewArray[$i]['count'] = 0;
if(isset($CountArray[$ItemArray['brand_slug']])){
$NewArray[$i]['count'] = $CountArray[$ItemArray['brand_slug']];
}
}
ksort($NewArray[$i]);
$i++;
}
print_r($NewArray);
以最简单的方式,您可以这样做。
假设您的第一个数组名称是 $first_array,第二个是 $second_array.
像
一样循环第二个
foreach($second_array as $index => $item)
{
$second_array[$index]['count'] = $first_array[$item['brand_slug']];
}
echo "<pre>";
print_r($second_array);
array_merge
将一个或多个数组的元素合并在一起,以便将一个数组的值附加到前一个数组的末尾。它 returns 结果数组。
Example #array_merge() example
<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
我有 2 个这样的数组。数组 1:
Array
(
[abu-garcia] => 1
[daiwa] => 4
[shimano] => 4
)
Array 2:
Array
(
[0] => Array
(
[brand_id] => 36
[brand_name] => Abu Garcia
[brand_slug] => abu-garcia
)
[1] => Array
(
[brand_id] => 41
[brand_name] => Daiwa
[brand_slug] => daiwa
)
[2] => Array
(
[brand_id] => 41
[brand_name] => Daiwa
[brand_slug] => daiwa
)
[3] => Array
(
[brand_id] => 41
[brand_name] => Daiwa
[brand_slug] => daiwa
)
[4] => Array
(
[brand_id] => 41
[brand_name] => Daiwa
[brand_slug] => daiwa
)
[5] => Array
(
[brand_id] => 39
[brand_name] => Shimano
[brand_slug] => shimano
)
[6] => Array
(
[brand_id] => 39
[brand_name] => Shimano
[brand_slug] => shimano
)
[7] => Array
(
[brand_id] => 39
[brand_name] => Shimano
[brand_slug] => shimano
)
我想创建一个如下所示的新数组:
Array
(
[0] => Array
(
[brand_id] => 36
[brand_name] => Abu Garcia
[brand_slug] => abu-garcia
[count] => 1
)
[1] => Array
(
[brand_id] => 41
[brand_name] => Daiwa
[brand_slug] => daiwa
[count] => 4
)
[2] => Array
(
[brand_id] => 41
[brand_name] => Daiwa
[brand_slug] => daiwa
[count] => 4
)
[3] => Array
(
[brand_id] => 41
[brand_name] => Daiwa
[brand_slug] => daiwa
[count] => 4
)
[4] => Array
(
[brand_id] => 41
[brand_name] => Daiwa
[brand_slug] => daiwa
[count] => 4
)
[5] => Array
(
[brand_id] => 39
[brand_name] => Shimano
[brand_slug] => shimano
[count] => 4
)
[6] => Array
(
[brand_id] => 39
[brand_name] => Shimano
[brand_slug] => shimano
[count] => 4
)
[7] => Array
(
[brand_id] => 39
[brand_name] => Shimano
[brand_slug] => shimano
[count] => 4
)
这两个数组都是由 wordpress 创建的。 array1 是检查品牌猫中产品数量时函数的输出,其他数组提供品牌详细信息。 谢谢
我想我明白你想做什么...
如果这些是您的数组示例:
$CountArray = array("abu-garcia"=>1,
"daiwa"=>4,
"shimano"=>4);
$DataArray = array(
array("brand_id"=>36, "brand_name"=>"Abu Garcia", "brand_slug"=>"abu-garcia"),
array("brand_id"=>41, "brand_name"=>"Daiwa", "brand_slug"=>"daiwa"),
array("brand_id"=>39, "brand_name"=>"Shimano", "brand_slug"=>"shimano")
);
然后遍历第二个数组以构建一个新数组,并像这样从第一个数组添加 count
键:
$i = 0;
foreach($DataArray as $ItemArray){
foreach($ItemArray as $Key=>$Value){
$NewArray[$i][$Key] = $Value;
$NewArray[$i]['count'] = 0;
if(isset($CountArray[$ItemArray['brand_slug']])){
$NewArray[$i]['count'] = $CountArray[$ItemArray['brand_slug']];
}
}
ksort($NewArray[$i]);
$i++;
}
print_r($NewArray);
以最简单的方式,您可以这样做。 假设您的第一个数组名称是 $first_array,第二个是 $second_array.
像
一样循环第二个foreach($second_array as $index => $item)
{
$second_array[$index]['count'] = $first_array[$item['brand_slug']];
}
echo "<pre>";
print_r($second_array);
array_merge 将一个或多个数组的元素合并在一起,以便将一个数组的值附加到前一个数组的末尾。它 returns 结果数组。
Example #array_merge() example
<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>