-5

As a stepping stone towards building a bit of json from which to build dependent dropdown, I'd like to transform this array...

Array
(
    [0] => Array
        (
            [id] => 4
            [project_id] => 2289
            [task] => Drawing
        )

    [1] => Array
        (
            [id] => 5
            [project_id] => 2289
            [task] => Surveying
        )

    [2] => Array
        (
            [id] => 6
            [project_id] => 2289
            [task] => Meeting
        )

    [3] => Array
        (
            [id] => 1
            [project_id] => 2282
            [task] => Folding
        )

    [4] => Array
        (
            [id] => 2
            [project_id] => 2282
            [task] => Printing
        )

    [5] => Array
        (
            [id] => 3
            [project_id] => 2282
            [task] => Cutting
        )

)

..to something like this...

Array
(
  [0] = Array
        (
        [project_id] => 2289
        [task] => Array
                  (
                  [0] => Drawing
                  [1] => Surveying
                  [2] => Meeting
                  )
        )
  [1] = Array
        (    
        [project_id] => 2282
        [task] => Array
                  (
                  [0] => Folding
                  [1] => Printing
                  [2] => Cutting
                  )
        )

)

Using...

$newArray = array();
foreach ($array as $row)
{
   $newArray[$row['project_id']][] = $row['task'];
}

...I'm able to get this...

Array
(
    [2289] => Array
        (
            [0] => Drawing
            [1] => Surveying
            [2] => Meeting
        )

    [2282] => Array
        (
            [0] => Folding
            [1] => Printing
            [2] => Cutting
        )

)

... but I've forgotten how to include the associative keys in the result

Strawberry
  • 32,714
  • 12
  • 37
  • 56
  • you have to put separate groups of `project_id`, it needs to be unique, the one you have is already okay – Kevin Feb 19 '16 at 00:41

2 Answers2

1

You can modify your foreach simply using a index:

$newArray = array();
$index = array();
foreach ($array as $row)
{
    $found = array_search( $row['project_id'], $index );
    if( $found === False )
    {
        $found = array_push( $newArray, array( 'project_id' => $row['project_id'] ) )-1;
        $index[$found] = $row['project_id'];
    }
    $newArray[ $found ]['task'][] = $row['task'];
}

eval.in demo

When a new project_id key is found, it is added to $index array, so — searching for it at next loop — I can retrieve the index of corresponding multi-dimensional array.

fusion3k
  • 11,012
  • 4
  • 21
  • 42
0

Just assign them as you would like, the project id in an index, and task continually pushing it there:

$newArray = array();
foreach ($array as $row) {
   $newArray[$row['project_id']]['project_id'] = $row['project_id'];
   $newArray[$row['project_id']]['task'][] = $row['task'];
}
$newArray = array_values($newArray); // reindex
// removes `$row['project_id']` on each group

Note: Just use array_values to reset the grouping key that you used in the project id grouping.

Kevin
  • 40,904
  • 12
  • 48
  • 67