1

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?

2
  • Can you explain further? What is "Standard" and when are there more of them? Do you want to know how to transform your current array into the new one or just create the new one? Commented Dec 23, 2009 at 3:24
  • Your second example doesn't really make sense. The IDs are repeated (all 0) and I wouldn't describe that as multi-dimensional. Please clarify. Commented Dec 23, 2009 at 3:29

5 Answers 5

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

1
$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?

Comments

0
$outputarray = array();

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

Comments

0

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'));

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.