phpN00b
phpN00b

Reputation: 807

php - recreate array?

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?

Upvotes: 1

Views: 406

Answers (5)

Jaimz
Jaimz

Reputation: 840

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

Upvotes: 0

troelskn
troelskn

Reputation: 117517

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

Upvotes: 0

philfreo
philfreo

Reputation: 43814

$outputarray = array();

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

Upvotes: 0

cletus
cletus

Reputation: 625097

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

Upvotes: 1

Amber
Amber

Reputation: 526743

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

Upvotes: 1

Related Questions