先谢谢大家,实际上我有一个" BUSINESS_NAME"还有FOOD AND GROCERY和NONFOOD,所以我想分开[series_name] => "FOOD AND GROCERY" with array values and [series_name] => "NONFOOD" with different values.
所以我的数组是
<?php
$arrFirst = Array
(
"0" => Array
(
"BUSINESS_NAME" => "FOOD AND GROCERY",
"Division_name" => "BAKERY AND CONCEPTS",
"value" => 0.0009477694110518376
),
"1" => Array
(
"BUSINESS_NAME" => "FOOD AND GROCERY",
"Division_name" => "DAIRY AND FROZEN",
"value" => 0.005450625689261974
),
"2" => Array
(
"BUSINESS_NAME" => "FOOD AND GROCERY",
"Division_name" => "FISH AND MEAT",
"value" => 0.000021331155586759062
),
"3" => Array
(
"BUSINESS_NAME" => "FOOD AND GROCERY",
"Division_name" => "FOODS",
"value" => 7.763433792429072
),
"4" => Array
(
"BUSINESS_NAME" => "FOOD AND GROCERY",
"Division_name" => "FMCGFOODS",
"value" => 28.280507791758872
),
"5" => Array
(
"BUSINESS_NAME" => "FOOD AND GROCERY",
"Division_name" => "FRUIT AND VEG",
"value" => 0.00004250371522120021
),
"6" => Array
(
"BUSINESS_NAME" => "FOOD AND GROCERY",
"Division_name" => "LIQUOR AND TOBACCO",
"value" => 0.0001807993856424188
),
"7" => Array
(
"BUSINESS_NAME" => "FOOD AND GROCERY",
"Division_name" => "STAPLES",
"value" => 63.63833077404931
),
"8" => Array
(
"BUSINESS_NAME" => "NONFOOD",
"Division_name" => "APPARELS",
"value" => 0.009254311711678597
),
"9" => Array
(
"BUSINESS_NAME" => "NONFOOD",
"Division_name" => "ELECTRONICS",
"value" => 0.0009884492728213818
),
"10" => Array
(
"BUSINESS_NAME" => "NONFOOD",
"Division_name" => "GENERALMERCHANDISING",
"value" => 0.051390163621477414
),
"11" => Array
(
"BUSINESS_NAME" => "NONFOOD",
"Division_name" => "MAX",
"value" => 0.23221365584864856
),
"12" => Array
(
"BUSINESS_NAME" => "NONFOOD",
"Division_name" => "TEXTILES",
"value" => 0.01723803195136225
)
);
我需要一个像下面这样的动态数组
Array
(
[BUSINESS_NAME] => Array
(
[0] => Array
(
[series_name] => FOOD AND GROCERY
(
[0] => 0.00094776941105184
[1] => 0.005450625689262
[2] => 2.1331155586759E-5
[3] => 7.7634337924291
[4] => 28.280507791759
[5] => 4.25037152212E-5
[6] => 0.00018079938564242
[7] => 63.638330774049
)
)
[1] => Array
(
[series_name] => NONFOOD
(
[0] => 0.0092543117116786
[1] => 0.00098844927282138
[2] => 0.051390163621477
[3] => 0.23221365584865
[4] => 0.017238031951362
)
)
)
)
我从谷歌使用
获得了参考foreach ($arrFirst as $value) {
$businesArr[$value['BUSINESS_NAME']]['series_name'][] = $value['value'];
}
foreach ($businesArr as $key => $value) {
$finalArr['BUSINESS_NAME'][][$key] = $value;
}
但阵列看起来不同
答案 0 :(得分:0)
尝试查看输出并相应更新 更新并与您的输出相匹配(完全正确)
$tmp = array();
foreach($arrFirst as $arg)
{
$tmp[$arg['BUSINESS_NAME']][] = $arg['value'];
}
$output = array();
foreach($tmp as $name => $values)
{
$output['BUSINESS_NAME'][] = array(
'series_name' => $name,
'value' => $values
);
}
答案 1 :(得分:0)
这样做,除了代替BUSINESS_NAME
下的数字键,您将获得原始数组中的实际BUSINESS_NAME
值:
foreach($arrFirst as $array) {
$result["BUSINESS_NAME"][$array["BUSINESS_NAME"]]
["series_name"][$array["BUSINESS_NAME"]][] = $array["value"];
}
答案 2 :(得分:-1)
您的第一个脚本只定义了一个数组,因此我不确定您在第二个脚本中引用$ businessArr的原因。
您的目标阵列看起来有点奇怪。我不确定您的粘贴输出是否有有效的var_dump输出,但我会尝试。
$destArray = array(
"BUSINESS_NAME" => array(
array(
0 => array("series_name" => "FOOD AND GROCERY"),
1 => array("series_name" => "NONFOOD")
)
)
);
foreach($arrFirst as $record) {
if ($record["BUSINESS_NAME"] == "FOOD AND GROCERY") {
$destArray["BUSINESS_NAME"][0][] = $record["value"];
} elseif ($record["BUSINESS_NAME"] == "NONFOOD") {
$destArray["BUSINESS_NAME"][1][] = $record["value"];
}
}
就个人而言,我不会像那样定义第二个数组。我也觉得你的原帖可以在表达你想要完成的事情方面有所改进。