Accessing stdClass object vars

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

    Accessing stdClass object vars

    Hi,

    I have the following object, I need to extract the SysMessage
    property.

    stdClass Object ( [UnknownConsignm ent] =stdClass Object
    ( [SysMessage] =Your consignment could not be found on our
    system ) )

    I could do this:
    $object->UnknownConsign ment->SysMessage

    However, depending on the condition, the "UnknownConsign ment" property
    might be called something else.
    Therefore, I need a solution, something like an array:
    $object->[0]->SysMessage

    Thanks in advance!

  • Hendri Kurniawan

    #2
    Re: Accessing stdClass object vars

    get_object_vars will actually give you an array of properties an object have

    Hendri

    JamesG wrote:
    Hi,
    >
    I have the following object, I need to extract the SysMessage
    property.
    >
    stdClass Object ( [UnknownConsignm ent] =stdClass Object
    ( [SysMessage] =Your consignment could not be found on our
    system ) )
    >
    I could do this:
    $object->UnknownConsign ment->SysMessage
    >
    However, depending on the condition, the "UnknownConsign ment" property
    might be called something else.
    Therefore, I need a solution, something like an array:
    $object->[0]->SysMessage
    >
    Thanks in advance!
    >

    Comment

    • JamesG

      #3
      Re: Accessing stdClass object vars

      On Mar 1, 1:58 am, Hendri Kurniawan <ask...@email.c omwrote:
      get_object_vars will actually give you an array of properties an object have
      >
      Hendri
      >
      JamesG wrote:
      Hi,
      >
      I have the following object, I need to extract the SysMessage
      property.
      >
      stdClass Object ( [UnknownConsignm ent] =stdClass Object
      ( [SysMessage] =Your consignment could not be found on our
      system ) )
      >
      I could do this:
      $object->UnknownConsign ment->SysMessage
      >
      However, depending on the condition, the "UnknownConsign ment" property
      might be called something else.
      Therefore, I need a solution, something like an array:
      $object->[0]->SysMessage
      >
      Thanks in advance!

      Thanks for the tip.
      I'm trying to use this, but it's not working how I like.

      The array is now as follows:
      Array ( [UnknownConsignm ent] =stdClass Object ( [SysMessage] =Your
      consignment could not be found on our system ) )

      So you would assume by using: $array[0] would access the object, and:
      $array[0]->SysMessage contains the message I want.
      This does not work! I know it's something silly, so any further help
      is appreciated!

      Thanks again

      Comment

      • Hendri Kurniawan

        #4
        Re: Accessing stdClass object vars

        JamesG wrote:
        On Mar 1, 1:58 am, Hendri Kurniawan <ask...@email.c omwrote:
        >get_object_var s will actually give you an array of properties an object have
        >>
        >Hendri
        >>
        >JamesG wrote:
        >>Hi,
        >>I have the following object, I need to extract the SysMessage
        >>property.
        >>stdClass Object ( [UnknownConsignm ent] =stdClass Object
        >>( [SysMessage] =Your consignment could not be found on our
        >>system ) )
        >>I could do this:
        >>$object->UnknownConsign ment->SysMessage
        >>However, depending on the condition, the "UnknownConsign ment" property
        >>might be called something else.
        >>Therefore, I need a solution, something like an array:
        >>$object->[0]->SysMessage
        >>Thanks in advance!
        >
        >
        Thanks for the tip.
        I'm trying to use this, but it's not working how I like.
        >
        The array is now as follows:
        Array ( [UnknownConsignm ent] =stdClass Object ( [SysMessage] =Your
        consignment could not be found on our system ) )
        >
        So you would assume by using: $array[0] would access the object, and:
        $array[0]->SysMessage contains the message I want.
        This does not work! I know it's something silly, so any further help
        is appreciated!
        >
        Thanks again
        >
        Hi James,

        It doesn't work because it returns associated array.
        What you have to do is get the name of that keys using array_keys.

        $variables = get_object_vars ($object);
        $keys = array_keys($var iables);
        print $variables[$key[0]]->SysMessage;

        Hendri Kurniawan

        Comment

        • JamesG

          #5
          Re: Accessing stdClass object vars

          On Mar 1, 3:37 am, Hendri Kurniawan <ask...@email.c omwrote:
          JamesG wrote:
          On Mar 1, 1:58 am, Hendri Kurniawan <ask...@email.c omwrote:
          get_object_vars will actually give you an array of properties an object have
          >
          Hendri
          >
          JamesG wrote:
          >Hi,
          >I have the following object, I need to extract the SysMessage
          >property.
          >stdClass Object ( [UnknownConsignm ent] =stdClass Object
          >( [SysMessage] =Your consignment could not be found on our
          >system ) )
          >I could do this:
          >$object->UnknownConsign ment->SysMessage
          >However, depending on the condition, the "UnknownConsign ment" property
          >might be called something else.
          >Therefore, I need a solution, something like an array:
          >$object->[0]->SysMessage
          >Thanks in advance!
          >
          Thanks for the tip.
          I'm trying to use this, but it's not working how I like.
          >
          The array is now as follows:
          Array ( [UnknownConsignm ent] =stdClass Object ( [SysMessage] =Your
          consignment could not be found on our system ) )
          >
          So you would assume by using: $array[0] would access the object, and:
          $array[0]->SysMessage contains the message I want.
          This does not work! I know it's something silly, so any further help
          is appreciated!
          >
          Thanks again
          >
          Hi James,
          >
          It doesn't work because it returns associated array.
          What you have to do is get the name of that keys using array_keys.
          >
          $variables = get_object_vars ($object);
          $keys = array_keys($var iables);
          print $variables[$key[0]]->SysMessage;
          >
          Hendri Kurniawan
          Thanks Hendri, works perfectly.

          Comment

          • Hendri Kurniawan

            #6
            Re: Accessing stdClass object vars

            My pleasure

            Comment

            Working...