-1

I need to generate an array whose keys are not pre-defined.

I have this array:

regions = [
  {
    "id":1,
    "name":"Alaska",
    "continent_id":5,
    "owner_id":3,
     ....
  },
  {
    "id":2,
    "name":"Greenland",
    "continent_id":5,
    "owner_id":7,
     ....
   }

I want to generate

$summary = [];

for ($i = 0; $i < count($owners); $i++) {
    for ($j = 0; $j < count($regions); $j++) {

        if ($owners[$i]['id'] == $regions[$j]['owner_id']) {
           $summary[ $regions[$j]['continent_id'] ]++;        <-- NEED HELP HERE
        }

     }
}

So I end up with $summary containing a "key" for each continent that the user owns regions in, and how may in each continent.

The above does not work as it returns undefined index. How do I generate the array keys on the fly and keep the count?

My expected output is:

$summary = ['1' => 12, '3' => 5, '5' => 7];

$summary[1] = 12;
$summary[3] = 5;
$summary[5] = 7;
TheRealPapa
  • 4,393
  • 8
  • 71
  • 155
  • 1
    put your expected output also. – Ravi Sep 24 '17 at 04:11
  • Hi @Ravi, I updated my q. Thanks – TheRealPapa Sep 24 '17 at 04:14
  • On what basis you are getting Ids ? or assigning value to specific Ids ? – Ravi Sep 24 '17 at 04:17
  • Specifically, where do the owner id's come from and what it in that array? – Altimus Prime Sep 24 '17 at 04:19
  • Hi @Ravi, I do not understand the Q. The data is coming from a mysql db. I have `regions`, which belong to `owners` and `continents`. The data is in array format. I want to generate a summary array containing an entry for each continent the user owns regions in, and how many for the continent. – TheRealPapa Sep 24 '17 at 04:19
  • If this is coming from MySQL you could just write the query to find out the answer. for example, `SELECT continent_id, count(*) FROM owners GROUP BY continent_id` – Altimus Prime Sep 24 '17 at 04:22
  • No, the records have had data changed since first selected from mysql. I need to do it with the array. – TheRealPapa Sep 24 '17 at 04:29
  • In question you only have given `regions` array. Add `owners` array also – B. Desai Sep 24 '17 at 04:38
  • What line exactly throws the "undefined index" error? – kmoser Sep 24 '17 at 04:45
  • Possible duplicate of [PHP: “Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset”](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Johnny Dew Sep 24 '17 at 05:26
  • The line marked `<-- NEED HELP HERE` throws the error – TheRealPapa Sep 24 '17 at 05:26

1 Answers1

0

When you first encounter a particular value of 'continent_id' - or in fact any element of an array that hasn't previously been encountered, it's better to do an isset and then create it if required.

if ($owners[$i]['id'] == $regions[$j]['owner_id']) {
   if ( isset($summary[ $regions[$j]['continent_id'] ]) === false ) {
       $summary[ $regions[$j]['continent_id'] ] = 0;
   }
   $summary[ $regions[$j]['continent_id'] ]++;
}
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55