waynew Posted July 14, 2008 Share Posted July 14, 2008 Hey guys. Enjoy the weekend? I've got a small array_slice problem. I'm getting two dynamic GET values. These two values define what part of the array that I have to split up. So for example, say that I've got an array of 12 months and the user only wants to see between June and July, the two GET values will be 6 and 7 (or 7 and 8 if the index starts at 0, which it should). The array should then be split to only take out those two months. How should I handle this dynamic split? Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 14, 2008 Share Posted July 14, 2008 You mean 5 and 6 if the index starts at 0. Well you would: $start = $_GET['start']; $end = $_GET['end']; $diff = $end - $start; $months = array_split($months, $start, $diff); Should do the trick, unless my logic is wrong. Quote Link to comment Share on other sites More sharing options...
waynew Posted July 14, 2008 Author Share Posted July 14, 2008 I had tried that but there's definately something small wrong here: <?php //This graph shows profit, cost and end price. if(isset($_GET['startdate']) && isset($_GET['enddate'])){ //check to see if user is wanting to limit dates $dates_selected = true; //this means that the user has chosen to select a time period. Used later on. $start_date = $_GET['startdate']; //begin date. $end_date = $_GET['enddate']; //end date. $end_date = $end_date - $start_date; //Start date and end dates are not really dates. They are index keys. //If there are three months and the user wants to see only the last two months //the start date will be 1 and the end date will be 2. indexes begin at 0 //Split up together in order to maintain sync of timeline and sum. $x_months = array_slice($x_months,$start_date,$end_date); $monthly_profit = array_slice($monthly_profit,$start_date,$end_date); } ?> There are two arrays. One holds the profit ($monthly_profit) for each month and the other ($x_months) holds the months. They are both synchronised so that when I print out $monthly_profit[1] it will be for the month $x_months[1]. However have a look at the results of this split: Before split using startdate 2 and enddate 4: 1151.519 8177.1744 16434.037 29104.1746 10674.6675 186.456 24345.6248 5935.786 10309.1478 4215.903 After split: 186.456 (Comes from index 5 (6th value). 8177.1744 (Comes from index 1 (2nd value). This wrecking my head. Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 14, 2008 Share Posted July 14, 2008 So I take it you've tried to echo out the start date and end date to ensure that they are the correct values. Quote Link to comment Share on other sites More sharing options...
waynew Posted July 14, 2008 Author Share Posted July 14, 2008 I have indeed. Once giving it the startdate 2 and the enddate 4. They both came back as 2. Which makes sense considering the calculations of enddate = 4 - 2. Quote Link to comment Share on other sites More sharing options...
waynew Posted July 14, 2008 Author Share Posted July 14, 2008 Herea a better debug of the problem: Before split using startdate 2 and enddate 4: Profit for Oct 07 at index 0 : 1151.519 Profit for Nov 07 at index 1 : 8177.1744 Profit for Dec 07 at index 2 : 16434.037 Profit for Jan 08 at index 3 : 29104.1746 Profit for Feb 08 at index 4 : 10674.6675 Profit for Mar 08 at index 5 : 186.456 Profit for Apr 08 at index 6 : 24345.6248 Profit for May 08 at index 7 : 5935.786 Profit for Jun 08 at index 8 : 10309.1478 Profit for Jul 08 at index 9 : 4215.903 After split: Profit for Dec 07 at index 0 : 186.456 Profit for Jan 08 at index 1 : 8177.1744 Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 14, 2008 Share Posted July 14, 2008 Well then it's correct. Isn't it? Or is that what you're aiming for? I just did it myself and it returned the correct results. If you want it between the correct months then you would need to subtract 1 from each variable, correct? Quote Link to comment Share on other sites More sharing options...
waynew Posted July 14, 2008 Author Share Posted July 14, 2008 But you see, the two arrays are split using the same parameters, which means that the profit and the month should both still be in sync. If you notice, the profit for the two months after the split is off. Before split Profit for Dec 07 at index 2 : 16434.037 After split: Profit for Dec 07 at index 0 : 186.456 Quote Link to comment Share on other sites More sharing options...
waynew Posted July 14, 2008 Author Share Posted July 14, 2008 For some reason it's giving DEC 07 the profit of MAR 08 Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 14, 2008 Share Posted July 14, 2008 Set the flag at the end of array_slice to true, to maintain index.. Not sure if that'll solve it though. $x_months = array_slice($x_months,$start_date,$end_date,true); $monthly_profit = array_slice($monthly_profit,$start_date,$end_date,true); Quote Link to comment Share on other sites More sharing options...
waynew Posted July 14, 2008 Author Share Posted July 14, 2008 You could be onto something here: Profit for Oct 07 at index 0 : 1151.519 Profit for Nov 07 at index 1 : 8177.1744 Profit for Dec 07 at index 2 : 16434.037 Profit for Jan 08 at index 3 : 29104.1746 Profit for Feb 08 at index 4 : 10674.6675 Profit for Mar 08 at index 5 : 186.456 Profit for Apr 08 at index 6 : 24345.6248 Profit for May 08 at index 7 : 5935.786 Profit for Jun 08 at index 8 : 10309.1478 Profit for Jul 08 at index 9 : 4215.903 Profit for at index 0 : Profit for at index 1 : 8177.1744 As you can see it's not displaying the after split Here's code of section, including debug echo (bearing in mind that I've added <?php ect to make it more readable for you): <?php include('stats.php'); $i=0; while($i < sizeof($x_months)){ echo "Profit for ".$x_months[$i]." at index ".$i." : ".$monthly_profit[$i]."<br/>"; $i++; } //This graph shows profit, cost and end price. if(isset($_GET['startdate']) && isset($_GET['enddate'])){ //check to see if user is wanting to limit dates $dates_selected = true; //this means that the user has chosen to select a time period. Used later on. $start_date = $_GET['startdate']; //begin date. $end_date = $_GET['enddate']; //end date. $end_date = $end_date - $start_date; //Start date and end dates are not really dates. They are index keys. //If there are three months and the user wants to see only the last two months //the start date will be 1 and the end date will be 2. indexes begin at 0 //Split up together in order to maintain sync of timeline and sum. $x_months = array_slice($x_months,$start_date,$end_date,true); $monthly_profit = array_slice($monthly_profit,$start_date,$end_date,true); } echo "<br/>"; $i=0; while($i < sizeof($x_months)){ echo "Profit for ".$x_months[$i]." at index ".$i." : ".$monthly_profit[$i]."<br/>"; $i++; }?> Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 14, 2008 Share Posted July 14, 2008 What should it be display? Man this is getting confusing. And can you post the array so I can try some stuff as well? Quote Link to comment Share on other sites More sharing options...
waynew Posted July 14, 2008 Author Share Posted July 14, 2008 Tell me about it man. You see, this is for a graph. The x-months is displayed on the x axis and the monthly_profit is displayed on the y axis. Therefore, the index value of x-months must always correspond with corresponding index value of monthly_profit. Currently, it displays the entire graph as a whole, correctly. However, when the user wants to limit the months via the GET values posted above. The slice seems to put the two arrays out of sync with one another, even thought both are the same size and both are sliced with the same parameters in order to maintain their sync. (EXAMPLE SPLIT) A result should look like this: Before slice: Profit for Oct 07 at index 0 : 1151.519 Profit for Nov 07 at index 1 : 8177.1744 Profit for Dec 07 at index 2 : 16434.037 Profit for Jan 08 at index 3 : 29104.1746 Profit for Feb 08 at index 4 : 10674.6675 Profit for Mar 08 at index 5 : 186.456 Profit for Apr 08 at index 6 : 24345.6248 Profit for May 08 at index 7 : 5935.786 Profit for Jun 08 at index 8 : 10309.1478 Profit for Jul 08 at index 9 : 4215.903 After slice Profit for Oct 07 at index 0 :1151.519 Profit for Nov 07 at index 1 : 8177.1744 Profit for Dec 07 at index 2 : 16434.037 Quote Link to comment Share on other sites More sharing options...
waynew Posted July 14, 2008 Author Share Posted July 14, 2008 This is driving me around the bend. Quote Link to comment Share on other sites More sharing options...
waynew Posted July 14, 2008 Author Share Posted July 14, 2008 I will give 5 Internet points to anyone who can solve this. Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 14, 2008 Share Posted July 14, 2008 I'm scratching my head at this one as well... It's really confusing me... I'll think about it. Quote Link to comment Share on other sites More sharing options...
waynew Posted July 14, 2008 Author Share Posted July 14, 2008 This is crazy. Logically, it should work. If both arrays are in sync with one another and are the same length, and are then sliced according to the same parameters, they really should remain in sync regardless of what part of the array you are returning. I have other programmers with me at the moment who are stumped too. :-\ Quote Link to comment Share on other sites More sharing options...
waynew Posted July 14, 2008 Author Share Posted July 14, 2008 Ok I've returned to this: <?php include('stats.php'); $i=0; while($i < sizeof($x_months)){ echo "Profit for ".$x_months[$i]." at index ".$i." : ".$monthly_profit[$i]."<br/>"; $i++; } //This graph shows profit, cost and end price. if(isset($_GET['startdate']) && isset($_GET['enddate'])){ //check to see if user is wanting to limit dates $dates_selected = true; //this means that the user has chosen to select a time period. Used later on. $start_date = $_GET['startdate']; //begin date. $end_date = $_GET['enddate']; //end date. $end_date = $end_date - $start_date; //Start date and end dates are not really dates. They are index keys. //If there are three months and the user wants to see only the last two months //the start date will be 1 and the end date will be 2. indexes begin at 0 //Split up together in order to maintain sync of timeline and sum. $x_months = array_slice($x_months,$start_date,$end_date); $monthly_profit = array_slice($monthly_profit,$start_date,$end_date); } echo "<br/>"; $i=0; while($i < sizeof($x_months)){ echo "Profit for ".$x_months[$i]." at index ".$i." : ".$monthly_profit[$i]."<br/>"; $i++; } And the output: Profit for Oct 07 at index 0 : 1151.519 Profit for Nov 07 at index 1 : 8177.1744 Profit for Dec 07 at index 2 : 16434.037 Profit for Jan 08 at index 3 : 29104.1746 Profit for Feb 08 at index 4 : 10674.6675 Profit for Mar 08 at index 5 : 186.456 Profit for Apr 08 at index 6 : 24345.6248 Profit for May 08 at index 7 : 5935.786 Profit for Jun 08 at index 8 : 10309.1478 Profit for Jul 08 at index 9 : 4444.859 After split: Profit for Dec 07 at index 0 : 186.456 Profit for Jan 08 at index 1 : 8177.1744 Quote Link to comment Share on other sites More sharing options...
waynew Posted July 14, 2008 Author Share Posted July 14, 2008 So, it seems that the profit of monthly_profit[5] is moving to monthly_profit[2] and monthly_profit[1] is moving to monthly_profit[3] WTF :-\ Quote Link to comment Share on other sites More sharing options...
waynew Posted July 14, 2008 Author Share Posted July 14, 2008 Okay, I've done some better debugging: Profit for Oct 07 at index 0 : 1151.519 Profit for Nov 07 at index 1 : 8177.1744 Profit for Dec 07 at index 2 : 16434.037 Profit for Jan 08 at index 3 : 29104.1746 Profit for Feb 08 at index 4 : 10674.6675 Profit for Mar 08 at index 5 : 186.456 Profit for Apr 08 at index 6 : 24345.6248 Profit for May 08 at index 7 : 5935.786 Profit for Jun 08 at index 8 : 10403.2978 Profit for Jul 08 at index 9 : 4444.859 ********** startdate once it comes in: 2 ********** enddate once it comes in: 4 enddate = 4 - 2; //calculation. Answer to above calculation is: 2 Array = array_slice(Array,2,2); // slice happens on x_months. Array = array_slice(Array,2,2); //slice happens on monthly profit After this split: Profit for Dec 07 at index 0 : 186.456 Profit for Jan 08 at index 1 : 8177.1744 Here's the code. Ignore all //DEBUG as they are only echos of the statements: //This graph shows profit, cost and end price. if(isset($_GET['startdate']) && isset($_GET['enddate'])){ //check to see if user is wanting to limit dates $dates_selected = true; //this means that the user has chosen to select a time period. Used later on. $start_date = $_GET['startdate']; //begin date. $end_date = $_GET['enddate']; //end date. echo "<br/>**********<br/><br/>startdate once it comes in as ".$start_date;//DEBUG echo "<br/>**********<br/><br/>enddate once it comes in as ".$end_date;//DEBUG echo "<br/><br/>$end_date = $end_date - $start_date; //calculation.";//DEBUG $end_date = $end_date - $start_date; echo "<br/>Answer to above calculation is: $end_date <br/>";//DEBUG //Start date and end dates are not really dates. They are index keys. //If there are three months and the user wants to see only the last two months //the start date will be 1 and the end date will be 2. indexes begin at 0 //Split up together in order to maintain sync of timeline and sum. $x_months = array_slice($x_months,$start_date,$end_date); $monthly_profit = array_slice($monthly_profit,$start_date,$end_date); echo "<br/>$x_months = array_slice($x_months,$start_date,$end_date); // slice happens on x_months. <br/>$monthly_profit = array_slice($monthly_profit,$start_date,$end_date); //slice happens on monthly profit";//DEBUG echo "<br/><br/>After this split:<br/>";//DEBUG } Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 14, 2008 Share Posted July 14, 2008 Well I have absolutely zilch idea of what is happening with yours, but I ran some tests on my local and everything worked fine. Using numbers and synced data. Quote Link to comment Share on other sites More sharing options...
waynew Posted July 14, 2008 Author Share Posted July 14, 2008 I'll go in for a better debug. Complete before and after. Quote Link to comment Share on other sites More sharing options...
waynew Posted July 14, 2008 Author Share Posted July 14, 2008 Here we go: Months array before split: Months Oct 07 is at index 0 before the split. Months Nov 07 is at index 1 before the split. Months Dec 07 is at index 2 before the split. Months Jan 08 is at index 3 before the split. Months Feb 08 is at index 4 before the split. Months Mar 08 is at index 5 before the split. Months Apr 08 is at index 6 before the split. Months May 08 is at index 7 before the split. Months Jun 08 is at index 8 before the split. Months Jul 08 is at index 9 before the split. Monthly profit array before split: Months profit 1151.519 is at index 0 before the split. Months profit 8177.1744 is at index 1 before the split. Months profit 16434.037 is at index 2 before the split. Months profit 29104.1746 is at index 3 before the split. Months profit 10674.6675 is at index 4 before the split. Months profit 186.456 is at index 5 before the split. Months profit 24345.6248 is at index 6 before the split. Months profit 5935.786 is at index 7 before the split. Months profit 10403.2978 is at index 8 before the split. Months profit 4444.859 is at index 9 before the split. startdate comes in as 2 from GET enddate comes in as 4 from GET end_date = 4 - 2; //calculation. Answer to above calculation is: 2 Array slice takes place: Array = array_slice(Array,2,2); // slice happens on x_months. Array = array_slice(Array,2,2); //slice happens on monthly profit After this split: Months array after split: Months Dec 07 is at index 0 after the split. Months Jan 08 is at index 1 after the split. Monthly profit array after split: Months 186.456 is at index 0 after the split. Months 8177.1744 is at index 1 after the split. Quote Link to comment Share on other sites More sharing options...
waynew Posted July 14, 2008 Author Share Posted July 14, 2008 However, if I add true to the end of array_slice and state that I want to maintain their index values, I get: After this split: Months array after split: Months (MISSING VALUE??) is at index 0 after the split. Months (MISSING VALUE??) is at index 1 after the split. Monthly profit array after split: Months profit (MISSING VALUE??) is at index 0 after the split. Months profit 8177.1744 is at index 1 after the split. //THIS IS CORRECT Quote Link to comment Share on other sites More sharing options...
waynew Posted July 14, 2008 Author Share Posted July 14, 2008 Man this is the biggest topic I have ever posted here. 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.