我是PHP的新手。
我需要为API目的发送数据。
我有像这样的数组print_r($detail)
Array
(
[0] => Array
(
[id] => item1
)
[1] => Array
(
[price] => 300
)
[2] => Array
(
[quantity] => 1
)
[3] => Array
(
[id] => item2
)
[4] => Array
(
[price] => 400
)
[5] => Array
(
[quantity] => 2
)
)
我希望在发送到另一个进程之前将其转换为多维,例如
Array
(
[0] => Array
(
[id] => item1
[price] => 300
[quantity] => 1
)
[1] => Array
(
[id] => item2
[price] => 400
[quantity] => 2
)
)
怎么可能?
答案 0 :(得分:2)
您可以将$details
阵列拆分为多个块。我编写了以下接受自定义块大小的函数(注意count($initialArray) % $chunkSize === 0
必须为true):
function transformArray(array $initialArray, $chunkSize = 3) {
if (count($initialArray) % $chunkSize != 0) {
throw new \Exception('The length of $initialArray must be divisible by ' . $chunkSize);
}
$chunks = array_chunk($initialArray, 3);
$result = [];
foreach ($chunks as $chunk) {
$newItem = [];
foreach ($chunk as $item) {
$newItem[array_keys($item)[0]] = reset($item);
}
$result[] = $newItem;
}
return $result;
}
根据您的详细信息数组,调用transformArray($details)
将导致:
可自定义的块大小允许您向源阵列添加更多数据:
$details = [
['id' => 'item1'],
['price' => 300],
['quantity' => 1],
['anotherProp' => 'some value'],
['id' => 'item2'],
['price' => 400],
['quantity' => 2],
['anotherProp' => 'another value'],
];
函数调用现在是transformArray($details, 4);
:
答案 1 :(得分:1)
此版本可能与@PHPglue答案的结果相同。但这只需要一个foreach
。我认为这是Top SO用户之一所说的更快,更有效的方式。
试着看看你是否想要它。
<?php
function explodeArray($arr)
{
$newArr = array();
foreach($arr as $row)
{
$key = array_keys($row)[0];
$curArr = count($newArr) > 0 ? $newArr[count($newArr)-1] : array();
if( array_key_exists($key, $curArr) )
{
array_push($newArr, array($key=>$row[$key]) );
}
else
{
$index = count($newArr) > 0 ? count($newArr) - 1 : 0 ;
$newArr[$index][$key] = $row[$key];
}
}
return $newArr;
}
用于测试的不同阵列。
$detail = array(
array('id'=>'item1'),
array('price'=>300),
array('quantity'=>1),
array('id'=>'item2'),
array('price'=>400),
array('quantity'=>2)
);
var_dump( explodeArray($detail) );
$detail_match = array(
array('id'=>'item1'),
array('price'=>300),
array('quantity'=>1),
array('newkey'=>'sample'),
array('id'=>'item2'),
array('price'=>400),
array('quantity'=>2),
array('newkey'=>'sample')
);
var_dump( explodeArray($detail_match) ); // Works with any size of keys.
$detail_diff_key = array(
array('id'=>'item1'),
array('price'=>300),
array('quantity'=>1),
array('diff1'=>'sample1'),
array('id'=>'item2'),
array('price'=>400),
array('quantity'=>2),
array('diff2'=>'sample2')
);
var_dump( explodeArray($detail_diff_key) ); // Works with any size of keys and different keys.
$detail_unmatch = array(
array('id'=>'item1'),
array('price'=>300),
array('quantity'=>1),
array('unmatchnum'=>'sample1'),
array('id'=>'item2'),
array('price'=>400),
array('quantity'=>2)
);
var_dump( explodeArray($detail_unmatch) );
答案 2 :(得分:1)
我认为这一小段代码适合你。
使用:强>
$detail = array(
"0" => array("id" => "item1"),
"1" => array("price" => "300"),
"2" => array("quantity" => "1"),
"3" => array("id" => "item2"),
"4" => array("price" => "400"),
"5" => array("quantity" => "2"),
"6" => array("id" => "item3"),
"7" => array("price" => "500"),
"8" => array("quantity" => "3")
);
$i = 0;
$j = 0;
$multi_details = array();
while($j < count($detail)){
$multi_details[$i][id] = $detail[$j++][id];
$multi_details[$i][price] = $detail[$j++][price];
$multi_details[$i][quantity] = $detail[$j++][quantity];
$i++;
}
echo '<pre>';
print_r($multi_details);
echo '</pre>';
<强>输出:强>
Array
(
[0] => Array
(
[id] => item1
[price] => 300
[quantity] => 1
)
[1] => Array
(
[id] => item2
[price] => 400
[quantity] => 2
)
[2] => Array
(
[id] => item3
[price] => 500
[quantity] => 3
)
)
答案 3 :(得分:0)
这样的事情应该这样做
$newArray = array();
$newRow = array();
foreach ($array as $row) {
$key = array_keys($row)[0];
$value = array_values($row)[0];
if ($key == 'id') {
$newArray[] = $newRow;
}
$newRow[$key] = $value;
}
$newArray[] = $newRow;
答案 4 :(得分:0)
此版本查找重复键,然后在它们相同时创建一个新数组,以便您可以拥有一些不同的数据。希望它有所帮助。
req.params.position;