提问人:Syed Ibrahim 提问时间:2/15/2020 更新时间:2/17/2020 访问量:130
PHP - 按子数组值对数组进行分组(索引数组)
PHP - Group Array by its Sub Array Value (Indexed Array)
问:
我需要按子数组值对数组进行分组。
刚刚通过这个链接。
但是这里的子数组值有键。就我而言,我们没有钥匙。仅索引键。下面是我的数组
Array
(
[0] => Array
(
[0] => 14. Remarketing Bengaluru
[1] => RM Web
[2] => Ludhiana
**[3] => Display Only**
[4] => INR
[5] => 6
)
[1] => Array
(
[0] => 12. Other Search ROTN
[1] => 01. Eye glass Shapes
[2] => Tiruchirappalli
**[3] => Search Only**
[4] => INR
[5] => 144
)
[2] => Array
(
[0] => 10. Brand ROTN
[1] => 03. Brand + Keywords
[2] => Erode
**[3] => Search Only**
[4] => INR
[5] => 24
)
[3] => Array
(
[0] => 11. Competitors ROTN
[1] => 02. Titan Eye Plus
[2] => Thoothukudi
**[3] => Search Only**
[4] => INR
[5] => 1
)
[4] => Array
(
[0] => 14. Remarketing Bengaluru
[1] => RM Web
[2] => Chandigarh
**[3] => Display Only**
[4] => INR
[5] => 6
)
)
预期:
需要对带有星标值的数组进行分组。例如。需要使用数组索引键 3 对数组进行分组。
分组数组应类似于“仅显示”、“仅搜索”
答:
2赞
jspit
2/17/2020
#1
这是按子数组值对数组进行分组的解决方案。我只是用 3 替换了“id”。
$old_arr = array(
array( "14. Remarketing", "RM", "Lu", "Display Only", "INR", "6" ),
array( "12. Other", "Shapes", "Ti", "Search Only", "INR", "144" ),
array( "10. Brand", "Brand", "E", "Search Only", "INR", "24" ),
array( "11. Competitors", "Titan", "Th", "Search Only", "INR", "1"),
);
foreach ($old_arr as $key => $item) {
$arr[$item[3]][$key] = $item;
}
echo '<pre>';
var_export($arr);
结果:
array (
'Display Only' =>
array (
0 =>
array (
0 => '14. Remarketing',
1 => 'RM',
2 => 'Lu',
3 => 'Display Only',
4 => 'INR',
5 => '6',
),
),
'Search Only' =>
array (
1 =>
array (
0 => '12. Other',
1 => 'Shapes',
2 => 'Ti',
3 => 'Search Only',
4 => 'INR',
5 => '144',
),
2 =>
array (
0 => '10. Brand',
1 => 'Brand',
2 => 'E',
3 => 'Search Only',
4 => 'INR',
5 => '24',
),
3 =>
array (
0 => '11. Competitors',
1 => 'Titan',
2 => 'Th',
3 => 'Search Only',
4 => 'INR',
5 => '1',
),
),
)
评论