php - Group a multidimensional array by a particular value? -
Hopefully, I can explain it correctly ...
I have a multi-dimensional array And I am trying to make them groups according to the value of keys
Therefore, I am trying to group them on the basis of the level, but in reality I do not already know the level. So, it's not like I can put it in the loop and say $ i & lt; 7, because I did not know that the maximum value for the key of the level is 7, and, obviously, I'm not sure that I would need to do this, even if I did ...
[Code] array ([0] = & gt; Aare ([customer] => XT8900 [type] => standard [level] => 1) [1] => Hey ([ Cust] => XT8944 [type] => standard [level] => 1] [2] => array ([cust] => XT8922 [type] => principal [ Levels => 3] => [Sir] => Standard [Level]; Array ([Customer] => XT8816 [Type] => permier [level] = & Gt; 3) [4] = & gt; Hey ([cust] = & Gt; XT7434 [Type] => = & Gt; 7))
What I'm hoping to produce:
Array ([ 1] => Array ([0] = & gt; Hey ([customer] => gt08900 [type] => standard) [1] = & gt; Array ([cust] = & gt; XT8944 [type] => standard)) [3] => Array ([2] = & gt; Array ([customer] => XT8922 [type] => premiere) [3] = & Gt; Array ([customer] => XT8816 [type] = permier)] [7] = & gt; Array ([4] = & gt; Array ([customer] = & gt; XT7434 [type] => standard)))
You need to group them by Level
Strong> foreach if the level is the same with the previous item that group with that array
$ templevel = 0; $ Newkey = 0; $ Grouparr [$ templevel] = ""; Forex currency ($ item as $ key = & gt; $ val) {if ($ templevel == $ val ['level']) {$ grouparr [$ templevel] [$ newkey] = $ val; } And {$ grouparr [$ val ['level']] [$ newkey] = $ val; } $ Newkey ++; } Print ($ grouparr);
Print ($ grouparr); You can also try the output of the format you are hoping for, like
You can also try
print ($ grouparr [7]) ;
will be displayed
[7] = & gt; Array ([4] = & gt; Array ([customer] = & gt; XT7434 [type] => standard))
or
Print ($ grouparr [3]);
will be displayed
[3] = & gt; Array ([2] = & gt; Array ([customer] = & gt; XT8922 [type] => premiere) [3] => Array ([cust] => XT8816 [type] = & Gt; permier))
Comments
Post a Comment