Jump to content

[SOLVED] Array Question


lotrfan

Recommended Posts

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?

Link to comment
Share on other sites

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
?>

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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? :P

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.