retrieving an object from an array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • stuart

    retrieving an object from an array

    Hi there,
    I have an array of objects, and I would like to retreive one of the
    objects based on a given key and value (ie. name='john'). Is this
    possible and if so what is the syntax?

    thanks
    Stuart
  • CountScubula

    #2
    Re: retrieving an object from an array

    "stuart" <stuart_n@webob jectives.com.au > wrote in message
    news:stuart_n-567E59.23521506 012004@news-vip.optusnet.co m.au...[color=blue]
    > Hi there,
    > I have an array of objects, and I would like to retreive one of the
    > objects based on a given key and value (ie. name='john'). Is this
    > possible and if so what is the syntax?
    >
    > thanks
    > Stuart[/color]

    yes and no, if your your array has keys like 'john' then yes,

    other wise your question apears to be just like, can I retrieve an object
    from an arrya based on mysocks=white. I am not truying to be mean, just
    want you to understand how I see the question

    I kinda need to know how name=john is tied into your array.

    --
    Mike Bradley
    http://www.gzentools.com -- free online php tools


    Comment

    • Pedro Graca

      #3
      Re: retrieving an object from an array

      stuart wrote:[color=blue]
      > I have an array of objects, and I would like to retreive one of the
      > objects based on a given key and value (ie. name='john'). Is this
      > possible and if so what is the syntax?[/color]

      Not sure I understand you correctly, but try this


      <?php
      // using @ to avoid "Notice: Creating default object from empty value in
      // ..."
      @$obj->name = 'Ann';
      $obj->age = 7;
      $arr[0] = $obj;
      $obj->name = 'John';
      $obj->age = 17;
      $arr[1] = $obj;
      $obj->name = 'Martha';
      $obj->age = 15;
      $arr[2] = $obj;

      echo $arr[2]->name;
      echo '<pre>'; print_r($arr); echo '</pre>';
      ?>


      output is
      Martha

      Array
      (
      [0] => stdClass Object
      (
      [name] => Martha
      [age] => 15
      )

      [1] => stdClass Object
      (
      [name] => Martha
      [age] => 15
      )

      [2] => stdClass Object
      (
      [name] => Martha
      [age] => 15
      )

      )
      --
      --= my mail box only accepts =--
      --= Content-Type: text/plain =--
      --= Size below 10001 bytes =--

      Comment

      • Remon Huijts

        #4
        Re: retrieving an object from an array

        "stuart" <stuart_n@webob jectives.com.au > schreef in bericht
        news:stuart_n-567E59.23521506 012004@news-vip.optusnet.co m.au...[color=blue]
        > Hi there,
        > I have an array of objects, and I would like to retreive one of the
        > objects based on a given key and value (ie. name='john'). Is this
        > possible and if so what is the syntax?
        >
        > thanks
        > Stuart[/color]

        Strange how different people seem to understand your question differently
        (this is a hint!). This is what I think you absolutely propably need :)

        for ($i = 0; $i < count($array); $i++) {
        $object = $array[$i];
        if ($object->name == 'john') break;
        $object = NULL;
        }

        After this for loop $object contains the first object in the array with the
        name 'john', or it is NULL if no object had that name. You can test that
        very easily with the function is_null():

        if (is_null($objec t)) {
        echo 'I could not find an object with that name!';
        }

        Hope this helps.
        Remon.


        Comment

        Working...