lotrfan Posted October 9, 2007 Share Posted October 9, 2007 I want to use 2 arrays, and then see if their indexes match. For instance (in pseudo-code): If (max($array1) and min($array2) have the same index) { do this; } Any ideas? Thanks Quote Link to comment Share on other sites More sharing options...
Barand Posted October 9, 2007 Share Posted October 9, 2007 Besides the max() and min() functions you'll need array_search() to get the indexes Quote Link to comment Share on other sites More sharing options...
lotrfan Posted October 9, 2007 Author Share Posted October 9, 2007 So, I would first search the array, yes, and then use min() and max(). How would I compare the indexes? Quote Link to comment Share on other sites More sharing options...
Barand Posted October 9, 2007 Share Posted October 9, 2007 <?php $a = array (2,1,3); $b = array (4,5,3); $index1 = array_search(min($a), $a); $index2 = array_search(max($b), $b); if ($index1==$index2) { echo "same indexes"; } ?> Quote Link to comment Share on other sites More sharing options...
lotrfan Posted October 9, 2007 Author Share Posted October 9, 2007 I hope I understand what you're saying. In your example, $a and $b are arrays, yes? $index1 and $index2 are simply variables used to hold the maximum and minimum index values, respectively. Then, you finally compare if min value is the same as max value, correct? I don't understand the second $a and $b's in these lines, though: $index1 = array_search(min($a), $a); $index2 = array_search(max($b), $b); Wouldn't just the one be sufficient? Quote Link to comment Share on other sites More sharing options...
Barand Posted October 9, 2007 Share Posted October 9, 2007 min($a) gives the min VALUE in the array. You wanted the index of the min value in array $a Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted October 9, 2007 Share Posted October 9, 2007 Look at the PHP manual documentation for the parameters expected by array_search; you will see the second parameter is the array to search in. <?php $a = Array( 1, 10, 8, 17 ); // The following two lines: $tmp = min($a); // get min value $i = array_search($tmp, $a); // search for min value in $a and return index // are just the long winded way to write: $i = array_search(min($a), $a); // basically instead of calling and assigning to a temp variable we are just passing // the returned value from one function into another ?> Quote Link to comment Share on other sites More sharing options...
lotrfan Posted October 10, 2007 Author Share Posted October 10, 2007 Ahh, I see what you are saying. The entire array_search(needle, haystack) concept does makes sense. It seems a little redundant to to need to tell PHP to search "for the minimium value of a within a". Does this have to do with multidimensional arrays that this is needed? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted October 10, 2007 Share Posted October 10, 2007 No. You're not quite getting it. You have an array: Array( 9, 3, 17, 5 ) We can see very immediately the maximum value is 17 and it is at index 2. You said you wanted to search for the max in one array and see if it had the same index as the minimum in another array. The function min() takes an array as its argument and returns the minimum value. So if we pass the array above into min() we will get 3; three is the smallest value in that array. You said you wanted the index. Now we call array_search; array_search searches the array for the value and returns the index. We know the smallest value is three; we pass that into array_search and array_search tells us the index at which it occurs, i.e. 2 in our example. To summarize: 1) Start with an array 2) Determine the minimum / maximum value 3) Search for that value in the array and determine the position or index at which it occurs. Quote Link to comment Share on other sites More sharing options...
lotrfan Posted October 10, 2007 Author Share Posted October 10, 2007 I understand now. The parameters of the functions are different and return different things. Value in one instance, and index of such value in said array in the other. Thank you both Barand and roopert! Especially roopert for "dumbing it down" to my level. I dont know what the qualifications are to be of moderator status, but roopert should definitely be considered, if only for all the help he has given to me personally and others since I joined the forum. Always learning, Will Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted October 10, 2007 Share Posted October 10, 2007 Especially roopert for "dumbing it down" to my level. I dont know what the qualifications are to be of moderator status, but roopert should definitely be considered, if only for all the help he has given to me personally and others since I joined the forum. Have you been paying people again roopurt? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted October 10, 2007 Share Posted October 10, 2007 Have you been paying people again roopurt? Hah! I wish I had spare money. 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.