-1
私はこのような単純な二次元配列を持っている:PHPグループ多次元配列
Array
(
[0] => Array
(
[id] => 1
[name] => John
[company] => One
[price] => 12.22
)
[1] => Array
(
[id] => 1
[name] => John
[company] => Two
[price] => 14.33
)
[2] => Array
(
[id] => 2
[name] => Mike
[company] => One
[price] => 15.11
)
[3] => Array
(
[id] => 2
[name] => Mike
[company] => Two
[price] => 10.12
)
[4] => Array
(
[id] => 3
[name] => Paul
[company] => One
[price] => 42.22
)
[5] => Array
(
[id] => 3
[name] => Paul
[company] => Two
[price] => 56.62
)
[6] => Array
(
[id] => 3
[name] => Paul
[company] => Three
[price] => 16.12
)
)
私はその後、別の値を持つ配列を作成し、グループid
とname
に必要なこのような何か:
Array
(
[0] => Array
(
[id] => 1
[name] => John
[companies] => array (
array(
[company] => One
[price] => 12.22
)
array(
[company] => Two
[price] => 14.33
)
)
)
[1] => Array
(
[id] => 2
[name] => Mike
[companies] => array (
array(
[company] => One
[price] => 15.11
)
array(
[company] => Two
[price] => 10.12
)
)
)
[2] => Array
(
[id] => 3
[name] => Paul
[companies] => array (
array(
[company] => One
[price] => 42.22
)
array(
[company] => Two
[price] => 56.62
)
array(
[company] => Three
[price] => 16.12
)
)
)
)
を
PHPで行う最良の方法は何ですか?
これは私のattempです:
<?php
$items=array();
$temp = 0;
$companies = array('uno','dos','tres');
foreach ($values as $value) {
if ($temp == $value['id'])
continue;
else
$temp == $value['id'];
foreach ($companies as $key => $company){
foreach ($values as $item){
if ($item['id'] == $temp && $item['company'] == $key)
$value['company'][$key] = $item['price'];
}
}
$items[] = $value;
}
私はそれを行うにはいくつかの方法が考えられます。これまでに何を試しましたか?これはコード作成サービスではありません。 – miken32
私の考え方を追加しました – kurtko
あなたは名前でグループ化したいと言っていますが、あなたのコードには 'name'とは言いません。 – miken32