Jump to content



Photo

Linear Relationship Of Array Values


  • Please log in to reply
3 replies to this topic

#1 kobena

kobena

    Member

  • Members
  • PipPip
  • 25 posts

Posted 20 September 2011 - 6:22 AM

Hello all;

I have the following arrays:

volume_array=(100,200,350,400); and
height_array=(1,5,8,10);

a height_array value corresponds to a value in the volume_array with the same key.

Does someone know how to efficiently get the corresponding volume for a height of 7 assuming a linear relationship?

I have been looking at the php array functions for sometime now without any luck.
  • 0

#2 Larry

Larry

    Administrator/Writer

  • Administrators
  • 3,962 posts
  • LocationState College, PA (USA)

Posted 21 September 2011 - 8:10 AM

This fetches the key from the array where the value is 7:
$key = array_keys($height_array, 7);

Then $volume_array[$key] gets you the value you need.
  • 0

#3 kobena

kobena

    Member

  • Members
  • PipPip
  • 25 posts

Posted 21 September 2011 - 12:12 PM

Thanks Larry for your response.
When i try i get the message "Warning: Illegal offset type"

This i believe is due to the fact that ( i may be wrong though) "7" is not in the height_array it is to be extrapolated between 5 and 8.
  • 0

#4 kobena

kobena

    Member

  • Members
  • PipPip
  • 25 posts

Posted 28 September 2011 - 9:56 AM

Thanks all; i found the solution. Will post it in due course.
  • 0