0

I have two arrays. Array 1 contains usernames and associated IDs. Array two contains the same IDs, a timestamp and a number of "followers" associated with that timestamp and that ID.

Examples, (this is just a small subset of the data):

$accounts_list array

array(4) {
  [0]=> array(2) {
    ["id"]=> string(4) "1279"
    ["name"]=> string(13) "Something_big"
  }
}

$followers_list array

array(12) {
  [0]=> array(3) {
    ["account_id"]=> string(4) "1279"
    ["date_time"]=> string(19) "2014-11-14 16:24:03"
    ["followers"]=> string(4) "1567"
  }
  [2]=> array(3) {
    ["account_id"]=> string(4) "1279"
    ["date_time"]=> string(19) "2014-11-14 18:52:35"
    ["followers"]=> string(4) "1566"
  }
  [8]=> array(3) {
    ["account_id"]=> string(4) "1279"
    ["date_time"]=> string(19) "2014-11-17 12:11:59"
    ["followers"]=> string(4) "1557"
  }
}

As you can see, $followers_list array stores the followers per timestamp/date for a specific ID. The ID matches an ID and the name.

What I am trying to do is combine the data together into something like this:

$new_array

array(1) {
      [0]=>
      array(2) {
        ["id"]=> string(4) "1279"
        ["name"]=> string(13) "Something_big"
        ["dates"]=>
            array(3) {
                ["date_time"]=> string(19) "2014-11-17 12:11:59"
                   array(1) {
                      ["followers"]=> string(4) "301"
                   }
                ["date_time"]=> string(19) "2014-12-17 13:10:32"
                   array(1) {
                      ["followers"]=> string(4) "307"
                   }
                ["date_time"]=> string(19) "2014-12-17 15:16:45"
                   array(1) {
                      ["followers"]=> string(4) "317"
                   }
            }
      }
}

Hopefully you can see what I mean, I want to have the arrays with each date contain the followers. And those dates should be inside a user index. So that the whole array stores the dates and followers per user.

I have tried to work out how to loop through the $followers_list array and add it into the $accounts_list array but I cannot work out how to match the ID's in both arrays with each other.

How would you create this array?

This is what I have tried:

$counter = 0;
foreach ($followers_data as &$follower_count){
    $timestamp = $follower_count['date_time'];
    $followers = $follower_count['followers'];
    $time_and_followers = [$timestamp,$followers];

    array_push($accounts_data[$counter],$time_and_followers);
    $counter++;
}

Of course this doesn't really work because the $counter number isn't always equal to the index of the account ID in the first array.

4
  • Have you tried $accounts_list[0]["dates"] = $followers_list;? Commented Nov 17, 2014 at 12:58
  • Index $accounts_list with the id as its key. Commented Nov 17, 2014 at 13:03
  • @CharlesRojas that doesn't give the right result at all: pastebin.com/Z826d7h7 Commented Nov 17, 2014 at 13:04
  • My comment was just an idea to get how to manage the array in a basic concept, check answers Commented Nov 17, 2014 at 13:58

2 Answers 2

2

You can do this by simply using account id as index id for new array.
eg. we will declare a temporary variable with name $new_tmp_array;
first we will add account list in it with following code :

foreach($accounts_list as $account){
  $new_tmp_array[$account['id'] ] = $account;
}

Now it's time to add $followers_list :

foreach($followers_list as $followers){
  $tmp = $followers;
  unset($tmp['account_id'])
  $new_tmp_array[$followers['account_id'] ] ['dates'][] = $tmp;
}

and now, for array without your account keys.

$new_array = array_values(new_tmp_array);
Sign up to request clarification or add additional context in comments.

Comments

0

I'm not sure this would be exact what you want but try it:

$new_array = $accounts_list;

foreach($new_array as &$account){
    foreach($followers_list as $follower){
        if($follower["account_id"] == $account["id"]){
            $account["dates"][] = array($follower["date_time"], $follower["followers"]);
        }
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.