Split an array up?

I have

foreach($devices as $value) {
	echo '<pre>';print_r($value);echo '</pre>';
	
}

which produces a huge array

Array
(
    [Name] => Dog Locator
    [Device_Id] => 5
    [Room] => Comms Room
    [Link] => ../rooms/comms.php
    [Rack_Title] => Racky
    [Rack_Id] => 1
    [Row] => 1
    [Bay] => A
)

Array
(
    [Name] => Dog Locator
    [Device_Id] => 7
    [Room] => Comms Room
    [Link] => ../rooms/comms.php
    [Rack_Title] => Racky
    [Rack_Id] => 1
    [Row] => 1
    [Bay] => A
)

Array
(
    [Name] => Dog Locator
    [Device_Id] => 9
    [Room] => Comms Room
    [Link] => ../rooms/comms.php
    [Rack_Title] => Racky
    [Rack_Id] => 1
    [Row] => 1
    [Bay] => A
)
...

Im trying to break up that array into 6 arrays.
The 6 arrays should be grouped by the Room index of the big array
I thought something like this would work

foreach($devices as $key => $value) {
  if($value['Room'] = 'Comms Room') {

  $deviocesInCommsRoom[] .= $key[$value];

}
print_r($deviocesInCommsRoom);

[spitball]

$out = array();
foreach($devices AS $value) {
   (is_array($out[$value['Room']])) ? $out[$value['Room']] = array($value) : $out[$value['Room']][] = $value;
}
1 Like

what does this do?

   (is_array($out[$value['Room']])) 

checks to see if the thing is already an array. Though looking at it again, i got the true and false clauses backwards.

1 Like

Make sure you are comparing values, i.e.

if($value['Room'] == 'Comms Room') {
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.