Tommy Wilkey
Tommy Wilkey

Reputation: 137

A couple of issues with PHP arrays

Here is how my code is supposed to work. I pull data from a few remote JSON urls and decode them back into normal arrays. Then I loop through those arrays and create a single combined array. While looping I do an array_search inside the combined array to see if the value for username already exists and return the key. If a key is returned then I combine the data from that key with the loops data. If the search returns false then I add the loop data to the end of the array.

There are a couple issues I am having and they could be related but I am not sure.

First, in my code when my array_search is ran it breaks the code. Second, if I var_dump the master array above the array_search if statement then the array is populated with the first round of the loop, however when I look at the structure of the array from the dump I see that the array starts out strange and I don't know why.

Here is the code

    $master_user_array = array();
    foreach($url_list AS $url) {
        $json = file_get_contents($url."?key=".self::AUTH_KEY);
  $data = json_decode($json, true);

  $user_count = 0;
        foreach($data['user'] AS $user) {echo highlight_string(var_export($master_user_array, true));
    if(count($master_user_array) > 0) {
      $key = array_search($user['username'], array_column($master_user_array, 'username'));
            } else {
                $key = false;
            }

    if(false !== $key) {
        $master_user_array[$key]['username'] = $user['username'];
        $master_user_array[$key]['email'] = $user['email'];

        $master_user_array[$key]['total']['counttoday'] += $user['counttoday'];
                $master_user_array[$key]['total']['countweek'] += $user['countweek'];
                $master_user_array[$key]['total']['countmonth'] += $user['countmonth'];
                $master_user_array[$key]['total']['countyear'] += $user['countyear'];
                $master_user_array[$key]['total']['counttotal'] += $user['counttotal'];

        $master_user_array[$key]['sites'][$data['siteurl']]['counttoday'] = $user['counttoday'];
                $master_user_array[$key]['sites'][$data['siteurl']]['countweek'] = $user['countweek'];
                $master_user_array[$key]['sites'][$data['siteurl']]['countmonth'] = $user['countmonth'];
                $master_user_array[$key]['sites'][$data['siteurl']]['countyear'] = $user['countyear'];
                $master_user_array[$key]['sites'][$data['siteurl']]['counttotal'] = $user['counttotal'];
            } else {
        $master_user_array[$user_count]['username'] = $user['username'];
        $master_user_array[$user_count]['email'] = $user['email'];

        $master_user_array[$user_count]['total']['counttoday'] = $user['counttoday'];
                $master_user_array[$user_count]['total']['countweek'] = $user['countweek'];
                $master_user_array[$user_count]['total']['countmonth'] = $user['countmonth'];
                $master_user_array[$user_count]['total']['countyear'] = $user['countyear'];
                $master_user_array[$user_count]['total']['counttotal'] = $user['counttotal'];

        $master_user_array[$user_count]['sites'][$data['siteurl']]['counttoday'] = $user['counttoday'];
                $master_user_array[$user_count]['sites'][$data['siteurl']]['countweek'] = $user['countweek'];
                $master_user_array[$user_count]['sites'][$data['siteurl']]['countmonth'] = $user['countmonth'];
                $master_user_array[$user_count]['sites'][$data['siteurl']]['countyear'] = $user['countyear'];
                $master_user_array[$user_count]['sites'][$data['siteurl']]['counttotal'] = $user['counttotal'];

      $user_count++;
            }
        }
    }

And here is the output from the var_dump notice the array starts with array ( ) 1. If I get rid of the array_search and the code doesn't break then this part of the array is added to the beginning of every round of the loop. Always with a 1.

array (
) 1 array (
  '' => 
  array (
    'username' => 'somename',
    'email' => 'someemail',
    'total' => 
    array (
      'counttoday' => 0,
      'countweek' => 0,
      'countmonth' => 0,
      'countyear' => 0,
      'counttotal' => 3,
    ),
    'sites' => 
    array (
      '' => 
      array (
        'counttoday' => 0,
        'countweek' => 0,
        'countmonth' => 0,
        'countyear' => 0,
        'counttotal' => '3',
      ),
    ),
  ),
)

Upvotes: 0

Views: 72

Answers (1)

Tommy Wilkey
Tommy Wilkey

Reputation: 137

Just so everyone knows I went a different way. Rather than trying to make things so complicated I was able to simplify the array and get rid of the array_search altogether.

Upvotes: 0

Related Questions