views:

65

answers:

5

I've "inherited" some data, which I'm trying to clean up. The array is from a database which, apparently, had no keys.

The array itself, is pretty long, so I'm simplifying things for this post...

[0] => Array
        (
            [id] => 2
            [uid] => 130
            [eid] => 8
            [ename] => Standard
            [eaction] => Check
        )
[1] => Array
        (
            [id] => 2
            [uid] => 110
            [eid] => 8
            [ename] => Standard
            [eaction] => Check
        )
[2] => Array
        (
            [id] => 2
            [uid] => 200
            [eid] => 8
            [ename] => Standard
            [eaction] => Check
        )

I'm trying to shift things around so the array is multidimensional and is grouped by ename:

[0] => Array
        (
            [Standard] => Array
            (
                 [id] => 2
                 [uid] => 130
                 [eid] => 8
                 [eaction] => Check
             ) 
        )
[0] => Array
        (
            [Standard] => Array
            (
                 [id] => 2
                 [uid] => 130
                 [eid] => 8
                 [eaction] => Check
             ) 
        )
[0] => Array
        (
            [Standard] => Array
            (
                 [id] => 2
                 [uid] => 130
                 [eid] => 8
                 [eaction] => Check
             ) 
        )

Anyone know how to do something like this?

A: 
$outputarray = array();

foreach($inputarray as $value) {
  $outputarray[] = array($value['ename'] => $value);
}

would accomplish what your examples seem to indicate (aside from the fact that your 'result' example has multiple things all with key 0... which isn't valid. I'm assuming you meant to number them 0,1,2 et cetera). However, I have to wonder what benefit you're getting from this, since all it appears to be doing is adding another dimension that serves no purpose. Perhaps you could clarify your example if there are other things to take into account?

Amber
A: 

You can use usort() to sort an array by a user-defined function. That function could compare the ename fields. Then it's just a simple transformation. Like:

usort($array, 'cmp_ename');

function cmp_ename($a, $b) {
  return strcmp($a['ename'], $b['ename']);
}

and then:

$output = array();
foreach ($array as $v) {
  $ename = $v['ename'];
  unset($v['ename']);
  $output[] = array($ename => $v);
}
cletus
A: 
$outputarray = array();

foreach($inputarray as &$value) {
  $outputarray[][$value['ename']] = $value;
  unset($value['ename']);
} unset($value);
philfreo
A: 

I'm guessing that this is what you're asking for:

function array_group_by($input, $field) {
  $out = array();
  foreach ($input as $row) {
    if (!isset($out[$row[$field]])) {
      $out[$row[$field]] = array();
    }
    $out[$row[$field]][] = $row;
  }
  return $out;
}

And usage:

var_dump(array_group_by($input, 'ename'));
troelskn
A: 

philfreo was right but he was also off a little. with his code every time you encounter an array element with an ['ename'] the same as one you've already gone through it will overwrite the data from the previous element with the same ['ename']

you might want to do something like this:

$output = array();
foreach ($YOURARRAY as $value) {
    $output[$value['ename']][] = $value;
}
var_dump($output); // to check out what you get
Jaimz