Php 如何透视此阵列
我想将其转换为:Php 如何透视此阵列,php,Php,我想将其转换为: $arr = [ [ 'type' => 'fruit', 'name' => 'apple', 'cost' => 1 ], [ 'type' => 'fruit', 'name' => 'orange', 'cost' => 2 ],
$arr = [
[
'type' => 'fruit',
'name' => 'apple',
'cost' => 1
],
[
'type' => 'fruit',
'name' => 'orange',
'cost' => 2
],
[
'type' => 'vegetable',
'name' => 'carrot',
'cost' => 2.5
],
[
'type' => 'vegetable',
'name' => 'avocado',
'cost' => 3.5
]
];
为此:
$arr = [
[
'type' => 'fruit',
'apple' => '1',
'orange' => 2
],
[
'type' => 'vegetable',
'carrot' => 2.5,
'avocado' => 3.5
]
];
如您所见,我将每种类型分组在一个数组中,并以水果名称和成本为中心
有什么想法吗
谢谢。对于这样的事情,只要按照Rizier的建议在数组中循环就可以了。如果您是按类型分组的,那么使用该类型作为数组键可能是最简单的,因此格式与您所要求的略有不同。您可以像这样访问生成的数组:
$fruitItems = $newArr['fruit'];
下面是示例代码:
$size = count($itemArray);
$newArr = []; //The array you want to create
for ($i = 0; $i < $size; $i++)
{
$item = $itemArray[$i];
$type = $item['type'];
$name = $item['name'];
$cost = $item['cost']
//If your new array doesn't have this type yet, make a sub-array
//with the type as key
if (!isset($newArr[$type]))
$newArr[$type] = [];
$newArr[$type][$name] = $cost; //In your type sub-array, add the new item
}
$size=count($itemArray);
$newArr=[]//要创建的数组
对于($i=0;$i<$size;$i++)
{
$item=$itemArray[$i];
$type=$item['type'];
$name=$item['name'];
$cost=$item['cost']
//如果您的新数组还没有此类型,请创建一个子数组
//以类型作为键
如果(!isset($newArr[$type]))
$newArr[$type]=[];
$newArr[$type][$name]=$cost;//在类型子数组中添加新项
}
如果您必须使用该结构创建一个数组,那么可以稍微调整上面的代码来搜索您的数组,并找到具有正确类型的子数组。不过,这似乎有点太复杂了。对于类似这样的事情,只要按照Rizier的建议在数组中循环就可以了。如果您是按类型分组的,那么使用该类型作为数组键可能是最简单的,因此格式与您所要求的略有不同。您可以像这样访问生成的数组:
$fruitItems = $newArr['fruit'];
下面是示例代码:
$size = count($itemArray);
$newArr = []; //The array you want to create
for ($i = 0; $i < $size; $i++)
{
$item = $itemArray[$i];
$type = $item['type'];
$name = $item['name'];
$cost = $item['cost']
//If your new array doesn't have this type yet, make a sub-array
//with the type as key
if (!isset($newArr[$type]))
$newArr[$type] = [];
$newArr[$type][$name] = $cost; //In your type sub-array, add the new item
}
$size=count($itemArray);
$newArr=[]//要创建的数组
对于($i=0;$i<$size;$i++)
{
$item=$itemArray[$i];
$type=$item['type'];
$name=$item['name'];
$cost=$item['cost']
//如果您的新数组还没有此类型,请创建一个子数组
//以类型作为键
如果(!isset($newArr[$type]))
$newArr[$type]=[];
$newArr[$type][$name]=$cost;//在类型子数组中添加新项
}
如果您必须使用该结构创建一个数组,那么可以稍微调整上面的代码来搜索您的数组,并找到具有正确类型的子数组。但是,这似乎有点过于复杂。这里有一种方法可以在输出中获得精确的数组结构。这比我想象的要复杂得多:
//first build groups by type
$groups = array();
foreach($arr as $key => $array){
//$type is not necessary, it's just for clarity below
$type = $array['type'];
if( !isset($groups[$type]) ){
$groups[$type] = array();
$groups[$type]['type'] = $array['type'];
}
$groups[$type][$array['name']] = $array['cost'];
}
//then combine the groups into a master array
$out = array();
foreach($groups as $g){
$out[] = $g;
}
echo '<pre>'. print_r($out, true).'</pre>';
//按类型第一次生成组
$groups=array();
foreach($arr作为$key=>$array){
//$type不是必需的,只是为了下面的清楚起见
$type=$array['type'];
如果(!isset($groups[$type])){
$groups[$type]=array();
$groups[$type]['type']=$array['type'];
}
$groups[$type][$array['name']]=$array['cost'];
}
//然后将这些组组合成一个主阵列
$out=array();
foreach($g组){
$out[]=$g;
}
回显“”。打印($out,true)。“”;
以下是一种在输出中获得精确数组结构的方法。这比我想象的要复杂得多:
//first build groups by type
$groups = array();
foreach($arr as $key => $array){
//$type is not necessary, it's just for clarity below
$type = $array['type'];
if( !isset($groups[$type]) ){
$groups[$type] = array();
$groups[$type]['type'] = $array['type'];
}
$groups[$type][$array['name']] = $array['cost'];
}
//then combine the groups into a master array
$out = array();
foreach($groups as $g){
$out[] = $g;
}
echo '<pre>'. print_r($out, true).'</pre>';
//按类型第一次生成组
$groups=array();
foreach($arr作为$key=>$array){
//$type不是必需的,只是为了下面的清楚起见
$type=$array['type'];
如果(!isset($groups[$type])){
$groups[$type]=array();
$groups[$type]['type']=$array['type'];
}
$groups[$type][$array['name']]=$array['cost'];
}
//然后将这些组组合成一个主阵列
$out=array();
foreach($g组){
$out[]=$g;
}
回显“”。打印($out,true)。“”;
如果有人需要更通用的方法,并且必须处理同一列中的多个值,我将@larsAnders解决方案放大了一点
用法(以获得确切的所需输出):
功能:
PHPDoc:
如果有人迫切需要改进编码风格、输入验证和/或变量命名,我将非常高兴。如果有人需要更通用的方法,并且必须处理同一列中的多个值,我将@larsAnders解决方案放大了一点 用法(以获得确切的所需输出): 功能: PHPDoc:
如果有人迫切需要改进编码风格、输入验证和/或变量命名,我会非常高兴。在数组中循环,使用类型作为键,您卡在哪里?在数组中循环,使用类型作为键,您卡在哪里?