frankchester Posted November 17, 2014 Share Posted November 17, 2014 (edited) Hello! First of all, I'm new here. I've been working with PHP for a few months and am really enjoying its capabilities A little background on the project I'm doing at the mo, I'm creating a small dashboard for grabbing Twitter stats via the API. The result will eventually output in a pretty table showing the daily followers, follower gains (or losses) etc. Here's an example of the table I eventually want to end up with. So far I have two arrays of data: $accounts_data is a list of all account names and their associated ID's $followers_data is a list of all follower counts, for a specific time of day, with an associated account ID (which matches the ID found in $accounts_data, thus pairing the follower count to a specific account) I'm now trying to combine these two arrays into something more sensible that can then be used to output the table as shown in the link above. $accounts_data: array(4) { [0]=> array(2) { ["id"]=> string(4) "1279" ["name"]=> string(13) "AccountName1" } [1]=> array(2) { ["id"]=> string(4) "1280" ["name"]=> string(10) "AccountName2" } [2]=> array(2) { ["id"]=> string(4) "1281" ["name"]=> string(12) "AccountName3" } [3]=> array(2) { ["id"]=> string(4) "1283" ["name"]=> string(11) "AccountName4" } } $followers_data (truncated this data) array(12) { [0]=> array(3) { ["account_id"]=> string(4) "1279" ["date_time"]=> string(19) "2014-11-14 09:00:03" ["followers"]=> string(4) "1567" } [1]=> array(3) { ["account_id"]=> string(4) "1280" ["date_time"]=> string(19) "2014-11-14 09:00:29" ["followers"]=> string(4) "7688" } [4]=> array(3) { ["account_id"]=> string(4) "1279" ["date_time"]=> string(19) "2014-11-14 16:30:35" ["followers"]=> string(4) "1566" } } 1. The first thing I want to do is take the name data and make that the first item in a sub-array of the ID. As such: This is because I think the ID data should be the most top-level data, and I think name should come underneath it, not be equal to it in the hierarchy, ["1283"]=> array (1){ ["name"]=> string(11) "AccountName4" } 2. Next up, I want to add a "dates" section to this array This will be where the follower data per date is stored ["1283"]=> array (2){ ["name"]=> string(11) "AccountName4" ["dates"]=> array(3){ // stuff here } } 3. Then, I want to take the contents of $followers_data and put it into the ['dates'] index for it's relevant ID. This is probably the most confusing part. I have no idea how to match the account_ID from my second array to the ID in the first array. And I need to nest the ['followers'] data underneath each date. ["1279"]=> array (2){ ["name"]=> string(11) "AccountName4" ["dates"]=> array(3){ [2014-11-14 09:00:03]=> array(1){ ["followers"]=> string(4) "1567" } [2014-11-14 16:30:35]=> array(1){ ["followers"]=> string(4) "1566" } } } In this array above you can see that the ID contains the name, and another array of dates. The Dates array contains each date. And inside each date, there are the followers. I know this is probably a really huge thing to do, but if anyone can offer help on even getting started in the right direction I would be very grateful. Edited November 17, 2014 by frankchester Quote Link to comment Share on other sites More sharing options...
ginerjm Posted November 17, 2014 Share Posted November 17, 2014 I'm always fascinated by the newcomer's attraction to creating complex arrays to handle their data output needs. All that work to create a temporary structure when there is already a very nice structure available that is permanent - the database. Why not focus on writing a better query (probably need a join - read up on them) and in that way obtain your data just the way you need it and then use those results to build your output? Quote Link to comment Share on other sites More sharing options...
Barand Posted November 17, 2014 Share Posted November 17, 2014 (edited) ginerjm Instead of being so ready to criticise the OP about not writing a better query, read the post and you'll see the data comes via an API Frankchester You could structure your accounts array as id => array ('name' => accountname) Then loop through the second array. Using the id as the key, append to that id's array in the first table. Your "dates" subarray structure probably just needs to be dates = array ( $date => $followers) instead of creating the extra level with dates = array ($date = array('followers' => $followers)) Edited November 17, 2014 by Barand Quote Link to comment Share on other sites More sharing options...
frankchester Posted November 17, 2014 Author Share Posted November 17, 2014 ginerjm Instead of being so ready to criticise the OP about not writing a better query, read the post and you'll see the data comes via an API Frankchester You could structure your accounts array as id => array ('name' => accountname) Then loop through the second array. Using the id as the key, append to that id's array in the first table. Your "dates" subarray structure probably just needs to be dates = array ( $date => $followers) instead of creating the extra level with dates = array ($date = array('followers' => $followers)) Thanks, that makes a lot of sense. I'll see what I can do. Quote Link to comment Share on other sites More sharing options...
Barand Posted November 17, 2014 Share Posted November 17, 2014 I was thinking something like this $data = array(); foreach ($accounts_data as $adata) { $data[$adata['id']]['name'] = $adata['name']; $data[$adata['id']]['dates'] = array(); } foreach ($followers_data as $fdata) { $id = $fdata['account_id']; $data[$id]['dates'][$fdata['date_time']] = $fdata['followers']; } echo '<pre>',print_r($data, true),'</pre>'; Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 17, 2014 Share Posted November 17, 2014 Also, I think it may be slow to get the data from the APIs each time your page is requested. You might want to consider implementing logic that will get the current data from the APIs and then save it to a database. Then, update that data every X minutes (whatever makes sense for your needs). Then create the output by getting the data from your database. That way the performance will be quick and your site will not have issues is there are errors or delays in getting the current data from the APIs. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.