[0]=>
array(5) {
["id"]=>
string(1) "2"
["title1"]=>
string(6) "color"
["content1"]=>
string(4) "Red 2"
["title2"]=>
string(6) "size"
["content2"]=>
string(2) "XL"
}
[1]=>
array(5) {
["id"]=>
string(1) "1"
["title1"]=>
string(6) "color"
["content1"]=>
string(4) "Red 1"
["title2"]=>
string(6) "size"
["content2"]=>
string(1) "L"
}
[2]=>
array(5) {
["id"]=>
string(1) "3"
["title1"]=>
string(6) "color"
["content1"]=>
string(4) "Red 3"
["title2"]=>
string(6) "size"
["content2"]=>
string(3) "XXL"
}
Assembled like this
array(5) {
["title1"]=>
string(6) "color"
["content1"]=>
array(5) {
["id"]=>
string(1) "2"
["content1"]=>
string(4) "Red 2"
["id"]=>
string(1) "1"
["content1"]=>
string(4) "Red 1"
["id"]=>
string(1) "3"
["content1"]=>
string(4) "Red 3"
}
["title2"]=>
string(6) "size"
["content2"]=>
array(5) {
["id"]=>
string(1) "2"
["content2"]=>
string(4) "XL"
["id"]=>
string(1) "1"
["content2"]=>
string(4) "L"
["id"]=>
string(1) "3"
["content2"]=>
string(4) "XXL"
}
}
phpcn_u15822017-05-16 13:09:30
There is a problem with the part that the subject wants to assemble. Take content1
as an example:
['content1'] => [
'id'=>2,
'content1'=>'红2',
'id'=>1,
'content1'=>'红1',
'id'=>3,
'content1'=>'红3',
]
In this way, only the data with id 3 can be retained.
I guess what the questioner needs is this:
['content1'] => [
['id'=>2,'content1'=>'红2'],
['id'=>1,'content1'=>'红1'],
['id'=>3,'content1'=>'红3'],
]
So, "here comes the most rubbish way of writing in history", I suggest you run the following code yourself.
Original array:
$arr = [
['id'=>2,'title1'=>'颜色','content1'=>'红2','title2'=>'尺码','content2'=>'XL'],
['id'=>1,'title1'=>'颜色','content1'=>'红1','title2'=>'尺码','content2'=>'L'],
['id'=>1,'title1'=>'颜色','content1'=>'红3','title2'=>'尺码','content2'=>'XXL']
];
Code I wrote:
$newArr = [];
foreach($arr as $k=>$v)
{
$keyArr = array_keys($v);
$valueArr = array_values($v);
foreach($keyArr as $key=>$val)
{
if((strstr($val, 'title') || strstr($val, 'content')) && !array_key_exists($val, $newArr))
$newArr[$val] = strstr($val, 'title') ? $valueArr[$key] : [];
if(strstr($val, 'content'))
{
foreach($arr as $ke=>$va)
{
$newArr[$val][$ke]['id'] = $va['id'];
$newArr[$val][$ke][$val] = $va[$val];
}
}
}
}
I’m not talented, the code is not well written, I hope you can forgive me, please don’t slap me in the face.
世界只因有你2017-05-16 13:09:30
How should it be assembled? When I asked a question, you couldn’t explain it clearly