Jump to content

Recommended Posts

Helo, first post here, although i m a usual reader.

I have a small pickle with a script i m writting.. i need to intersect timestamps..

Let me explain :

i have objects in my catalog. These objects change in percentaje in time intervals. So basically you could have for any given object

from time 1 to time 10 --> 30%, from time 11 to time 24 ---> 40 %, from time 25 to time 50--> 10%

 

This is modeled in my array in the folowing way

$arr [1,10,11,24,25,50].  and i have another array with percentajes ( might change it to 2 dimensional single arrya soonish...)

 

Now the problem is that i want to be able to create new intervals, so say for example in that example i want to insert from time 51 to70 another percentaje ( finall array beeing $arr [1,10,11,24,25,50,51,70])

or in the middle of an existing interval : 3->8 ($arr [1,2,3,8,9,10,11,24,25,50])

 

or in between 2 intervals 8 ->15 ($arr [1,7,8,15,16,24,25,50])

 

I don t know if i m making myself clear . hope i am, thanks for reading

 

Charles

Link to comment
https://forums.phpfreaks.com/topic/70485-merging-arrays/
Share on other sites

I think I understand. I also think you are over-complicating this.

 

You don't need an array to hold the percentage, you should just create a function to detemine the percentages and call it when you need that data. Also, your array for determining the intervals does not need beginning and ending points. Either enter the beginning points or the ending points, not both. If you enter only the beginning points, then it is assumed that the end point is the number directly preceeding the next point.

 

This is a quickly written example, I'm sure there are errors, but the logic should be valid

//Only the start points
$intervals = array (1,11,25,50);

function returnPercentage($dataAry, $intervalsAry) {
  global $intervalStart, $intervalEnd;

  //this function will process the data array
  //and create an array of percentages to be returned

  $percentagesAry = array();
  $totalCount = $count($dataAry);

  $intervals = count($intervalsAry);
  for ($i=0; $i<count($intervals); $i++) {
    $intervalStart = $intervalsAry[$i];
    if ($i<($intervals-1)) {
      $intervalEnd = $intervalsAry[$i+1];
    } else {
      $intervalEnd = false;
    }
    $intAry = array_filter($intervalStart, "getIntArray");
    $intCount = count($intAry);
    $percentagesAry[] = $intCount / $totalCount;
  }
  return $percentagesAry;
}

function getIntArray ($var) {
  global $intervalStart, $intervalEnd;
  return ($var['intColName']>=$intervalStart && $var['intColName']>=$intervalEnd);
}

Link to comment
https://forums.phpfreaks.com/topic/70485-merging-arrays/#findComment-354139
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.