30 users online (1 members and 29 guests)  


  Results 1 to 9 of 9

Related

  1. Replies: 4
  2. Help with CGI script for searching    Forum: CGI Perl Forum
    Replies: 1
  3. Searching    Forum: Website Scripts Forum
    Replies: 1
  1. #1
    deliriousthunder's Avatar
    Junior Member

    Status
    Offline
    Join Date
    Feb 2009
    Posts
    29

    'Searching' an array

    Hi,

    I have an array containing the properties of a list of objects. The objects in question each have an ID (a positive integer), a name (string), and a few other properties.

    I need to write a function to look through that array and find if a specific object is in it.

    For example, say I have an object with ID = 3, and Name = "flowers". I need to look through the array just to see if that object appears in the list.

    Seems a pretty basic thing to do, but for some reason I'm having a huge mental block

    Any help appreciated,
    Thanks,
    Tom

  2. #2
    jthayne's Avatar

    Status
    Offline
    Join Date
    Aug 2008
    Location
    Texas
    Posts
    508

    Re: 'Searching' an array

    There are several ways to do this. To get you the best one, could you post the basic structure of your array?

  3. #3
    deliriousthunder's Avatar
    Junior Member

    Status
    Offline
    Join Date
    Feb 2009
    Posts
    29

    Re: 'Searching' an array

    Sure, though I am not sure of the best way to type it out, I'll do my best..

    The array contains several objects.

    Each object has/contains eight properties, six of which are not important in the search.

    The two important ones are "value" and "text", (which are being used in a drop down menu at the moment). Both are strings, though the "value" happens to always be a +ve integer.

    I'm grabbing a "value" (id) and corresponding "text" (name) from a URL, and need to check that the corresponding object exists in the array.

    For your info, I'm using this to check if the current user has permission to do something to the object. If the object exists in this array, then the user has permission.

    Alternatively I could write another function just to check permissions, (which would involve a relatively complex database query). However, since the array already exists, I figured this is the best way.

  4. #4
    jthayne's Avatar

    Status
    Offline
    Join Date
    Aug 2008
    Location
    Texas
    Posts
    508

    Re: 'Searching' an array

    It sounds like you are always looking for a very specific key in the array. Use isset() to determine if the value exists in the array.

  5. #5
    deliriousthunder's Avatar
    Junior Member

    Status
    Offline
    Join Date
    Feb 2009
    Posts
    29

    Re: 'Searching' an array

    Thanks, I've looked at isset() before but I don't think it will work for this.

    I'll try and write out the array in a more code friendly way, and maybe it'll be easier to understand me.

    PHP Code:
    $array[object1][value] = $value_string // the id property
    $array[object1][text] = $text_string // the name property
    $array[object1][property1] = $some_string // irrelevant property
    // etc. etc. etc.

    $array[object2][value] = $value_string2 // the id property
    $array[object2][text] = $text_string // the name property
    $array[object2][property1] = $some_string // irrelevant property
    // etc. etc. etc.

    // etc. etc. etc. 
    Now, the array could contain any number of "object#"s and all of the properties will be set for each object.

    Being fed to the page through the URL, I have a value for the id property, (value), and for the name property, (text).

    What I need to do is, run through the array checking each object to see if any of them "match up". That is to say, check $array[object1][value] AND $array[object1][text] to see if they are equal to the values I have been given by the URL. If they are, that's all I need to know.

    If they are not, I need to check $array[object2] and the rest of the objects till I find one that matches. If none of them match, then the user doesn't have the proper authorities.

    It seems likely to me that this will involve a foreach statement, but I can't seem to get my head around what exactly to do

    Thanks again,
    Tom

  6. #6
    jthayne's Avatar

    Status
    Offline
    Join Date
    Aug 2008
    Location
    Texas
    Posts
    508

    Re: 'Searching' an array

    Correct. A foreach is the best for what you are trying to accomplish.

    Using your same example, do the following:

    PHP Code:
    $permission_granted FALSE;
    foreach(
    $array as $key => $value) {
      if ((
    $value['text'] == $urltext) && ($value['value'] == $urlvalue)) {
        
    $permission_granted TRUE;
      }

    You then have whether or not the user has permission stored as the value of $permission_granted.

  7. #7
    deliriousthunder's Avatar
    Junior Member

    Status
    Offline
    Join Date
    Feb 2009
    Posts
    29

    Re: 'Searching' an array

    Thanks

    That exact syntax didn't work, but with a bit of deduction I managed to get it working.

    Out of curiosity could you explain this error message to me?

    "Cannot use object of type stdClass as array"

    What is a stdClass object?

    Thanks,
    Tom

  8. #8
    jthayne's Avatar

    Status
    Offline
    Join Date
    Aug 2008
    Location
    Texas
    Posts
    508

    Re: 'Searching' an array

    Apparently, the array you are wanting to search through is not an array at all. Either that, or the error is referring to another location in your code. I am working blind here, and am basing all my troubleshooting on assumptions that you have made.

    As for the error, somewhere in your PHP code, is a declaration similar to the following:

    PHP Code:
    $variable = new stdClass
    stdClass is apparently a class (or object) that is being declared in your code. You are then treating it as an array rather than as an object.

    I really can't get any more detailed without seeing the actual code.

  9. #9
    deliriousthunder's Avatar
    Junior Member

    Status
    Offline
    Join Date
    Feb 2009
    Posts
    29

    Re: 'Searching' an array

    Ah, yes, the object contains an array though. If that makes sense.

    I'm using joomla. All the necessary info is passed from one file to the front page in an object, (called "$this"). I am dealing with $this->myarray.

    As I said, I got it working in the end, just had to be a bit more careful about how I referenced things.

    Thanks again



Tags for this Thread