2

I have an array a bit like:


Array (    
        [1] => Array
            (
                [1] => 21
                [2] => 3
                [0] => Analyst
            )    
        [2] => Array
            (
                [1] => 47
                [2] => 8
                [0] => Catalysis
            )

        [3] => Array
            (
                [1] => 1
                [2] => 0
                [0] => Biomaterials 
            )

        [4] => Array
            (
                [3] => 12
                [4] => 2
                [0] => Analyst
            )

       [5] => Array
            (
                [5] => 12
                [6] => 2
                [0] => Analyst
            )
...

However I would like to renumber those entries with the same [0] value so that I end up with


       [1] => Array
            (
                [1] => 21
                [2] => 3
                [3] => 12
                [4] => 2
                [5] => 12
                [6] => 2
                [0] => Analyst
            )

So far I've tried getting the [0] values out of the $results array by putting them in their own array and saying if you're already there then add [3] and [4] to where [1] and [2] are in a new array but it's not working.

$final = array();
$jArray = array();

foreach($results as $key => $result) {
  if(!in_array($result[0],$jArray) && !empty($result[0])) {
    $jArray[$i] = $result[0];
    $i++;
  }
}



for($x = 0; $x < count($results); $x++) { 
      $k = array_search($results[$x][0],$jArray);
    if(!isset($results[$x][1])) 
      $final[$k][1] = $results[$x][1];
    if(!isset($results[$x][2])) 
      $final[$k][2] = $results[$x][2];    
    if(!isset($results[$x][3])) 
      $final[$k][3] = $results[$x][3];
   if(!isset($results[$x][4])) 
      $final[$k][4] = $results[$x][4];
    if(!isset($results[$x][5]))     
      $final[$k][5] = $results[$x][5];
    if(!isset($results[$x][6]))     
      $final[$k][6] = $results[$x][6];
}

Any simpler ways of doing this?

1
  • Just put a second loop inside the loop. Commented Apr 16, 2014 at 11:39

3 Answers 3

1

You can do this way...

$new_arr=array();
$arkeys = array_unique(array_map(function ($v){ return $v[0];},$arr));
foreach($arr as $k=>$arr1)
{
            $new_arr[$arr1[0]][]=array_slice($arr1,0,count($arr1)-1);
}

foreach($arkeys as $v)
{
$new_arr[$v] = call_user_func_array('array_merge', $new_arr[$v]);
}
print_r($new_arr);

OUTPUT :

Array
(
    [Analyst] => Array
        (
            [0] => 21
            [1] => 3
            [2] => 12
            [3] => 2
            [4] => 12
            [5] => 2
        )

    [Catalysis] => Array
        (
            [0] => 47
            [1] => 8
        )

    [Biomaterials] => Array
        (
            [0] => 1
            [1] => 0
        )

)

Working Demo

Sign up to request clarification or add additional context in comments.

1 Comment

Nice, but I have 38 different "subjects" that may change so I would like something more elegant that a big switch statement
1

If you just want to group by the first element of the sub array, a single loop is enough:

$result = array();

foreach ($array as $sub_arr) {
  $key = $sub_arr[0];
  unset($sub_arr[0]);
  if (!isset($result[$key])) {
    $result[$key] = array();
  }
  $result[$key] += $sub_arr;
}

Comments

-1

Here

$final = array();

foreach($results as $key => $result) {
  if (!in_array($result[0], array_keys( $final ) ) && !empty($result[0])) {
    $final[$result[0]] = array( $result[0] );
  }
  foreach($result as $k => $v) {
    if ($k != 0 && isset($v)) {
      $final[$result[0]][$k] = $v;
    }
  } 
}

4 Comments

I'm afraid it doesn't work: I get [] => [Analyst] => Array ( [0] => Analyst [1] => 21 [2] => 3 [3] => 4 [4] => 1 [5] => 42 [6] => 3 [7] => [8] => [9] => 4 [10] => 0 ) [Biomaterials] => Array ( [0] => Biomaterials [1] => 1 [2] => 0 [3] => [4] => [5] => [6] =>
i.e. I dont know why 9 and 10 are filled in the first one and only 1 and 2 are filled in subsequently
Sorry, I have misunderstood. Now I modified my codes.
in_array($result[0], array_keys()) is definitely not an efficient way to check if a key exists in an array.

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.