Jump to content

[SOLVED] Small array_slice problem (Don't worry - this isn't a post full of arrays)


waynew

Recommended Posts

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?

 

Link to comment
Share on other sites

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.  :-[

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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++;
}?>

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.  :-\

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

   
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.