PHP数组不同元素分解
我动态地从数据库中提取结果,并将它们放入数组$myArray中,如下所示PHP数组不同元素分解,php,arrays,Php,Arrays,我动态地从数据库中提取结果,并将它们放入数组$myArray中,如下所示 Array ( [0] => Array ( [0] => Array ( [Fruit] => Apple [Number] => 1 [Date] => 3117-01-41 [supplier] => Store 1
Array (
[0] => Array (
[0] => Array (
[Fruit] => Apple
[Number] => 1
[Date] => 3117-01-41
[supplier] => Store 1
[description] => SAmple text for apple )
[1] => Array (
[Fruit] => Orange
[Number] => 1932
[Date] => 3117-01-41
[supplier] => Store 2
[description] => Sample text for Orange )
[2] => Array (
[Fruit] => Grape
[Number] => 22
[Date] => 3117-01-41
[supplier] => Store Street
[description] => Sample Text for Grape )
[3] => Array (
[Fruit] => Apple
[Number] => 23
[Date] => 3117-01-41
[supplier] => Store 9
[description] => This is text for a second apple )
[4] => Array (
[Fruit] => Apple
[Number] => 49
[Date] => 3117-01-41
[supplier] => Store 007
[description] => This is more text for some apples )
[5] => Array (
[Fruit] => Orange
[Number] => 1
[Date] => 3117-01-41
[supplier] => Store 7
[description] => This is for orange also )
)
)
理想情况是创建一个新阵列,使原始阵列保持不变
$newArray=$myArray
我想做的是在新数组中选择一个不同的字段,例如“Fruit”,它将显示一个不同的水果,但如果其他字段不同,则在新数组中为其创建一个嵌套数组
<!-- Desired output of $newArray -->
[0] => Array (
[Fruit] => Apple
[Number] => Array
(
[0] => 1
[1] => 23
[2] => 49
)
[Date] => Array
(
[0] => 3117-01-41
[1] => 3117-01-41
[2] => 3117-01-41
)
[supplier] => Array
(
[0] => Store 1
[1] => Store 9
[2] => Store 007
)
[description] => Array
(
[0] => SAmple text for apple
[1] => This is text for a second apple
[2] => This is more text for some apples
) )
[1] => Array (
[Fruit] => Orange
[Number] => Array
(
[0] => 1932
[1] => 1
)
[Date] => Array
(
[0] => 3117-01-41
[1] => 3117-01-41
[2] => 3117-01-41
)
[supplier] => Array
(
[0] => Store 2
[1] => Store 7
)
[description] => Array
(
[0] => Sample text for Orange
[1] => This is for orange also
) )
<!--Grape contents-->
[0]=>数组(
[水果]=>苹果
[编号]=>阵列
(
[0] => 1
[1] => 23
[2] => 49
)
[日期]=>数组
(
[0] => 3117-01-41
[1] => 3117-01-41
[2] => 3117-01-41
)
[供应商]=>阵列
(
[0]=>存储区1
[1] =>商店9
[2] =>商店007
)
[说明]=>阵列
(
[0]=>苹果的示例文本
[1] =>这是第二个苹果的文本
[2] =>这是一些苹果的更多文本
) )
[1] =>数组(
[水果]=>橙色
[编号]=>阵列
(
[0] => 1932
[1] => 1
)
[日期]=>数组
(
[0] => 3117-01-41
[1] => 3117-01-41
[2] => 3117-01-41
)
[供应商]=>阵列
(
[0]=>商店2
[1] =>商店7
)
[说明]=>阵列
(
[0]=>橙色的示例文本
[1] =>这也是橙色的
) )
首先组合相同水果的数组。现在您将在单个数组下拥有相同的多个水果数组。然后迭代该数组并格式化响应
$tempArray = [];
//creates the array of common fruit
foreach ($myArray[0] as $key => $value) {
$tempArray[$value['Fruit']][] = $value;
}
$newarray = [];
//iterate the new array of array of fruits
foreach ($tempArray as $key => $value) {
//here value will be common fruit array so iterate & format the array
$temp = [];
$temp['Fruit'] = $value[0]['Fruit'];
foreach ($value as $key => $val) {
$temp['Number'][] = $val['Number'];
$temp['Date'][] = $val['Date'];
$temp['supplier'][] = $val['supplier'];
$temp['description'][] = $val['description'];
}
$newarray [] = $temp;
}
var_dump($newarray);
那么,当你试图解决这个任务时,你写的代码在哪里呢?我不确定从哪里开始呢?你将创建一个循环,迭代所有数组元素,并在每次迭代中创建一个新元素,并将其添加到最终数组中。然后逐个保存每个属性。