stdClass Object - get_object_vars

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

    #1

    stdClass Object - get_object_vars

    hello, i have a d/l'd class that gives an array which prints like this:

    stdClass Object
    (
    [first name]] => stdClass Object
    (
    [inputtype] => text
    [value] =>
    [debug] => fisrt name
    [validate] => yes
    )

    [Buttons\0]] => stdClass Object
    (
    [Count] => 41
    [Button] => click
    )

    )

    i cannot seem to access the values using
    $thearray['first name']['inputtype']
    messed with get_object_vars () a bit, but didnt get past the first
    dimension.

    I'm calling the class like so:

    $ini = new Ini('forms.ini' );
    $ini->parse();
    $thearray= $ini->get();

    echo "<pre>";
    print_r($thearr ay);
    echo "</pre>";


    Here is the class, oh, and maybe someone can see why it's putting two
    end brackets on the [first name]], etc.

    <?php
    /**
    * Ini parser
    *
    * Parse Ini files into a workable object
    *
    * @author mcvdmvs <mick@vandermos tvanspijk.nl>
    * @version 0.0.1
    * @package sys
    * @copyright GPL
    * @see http://cloanto.com/specs/ini.html
    */
    class Ini {
    /**
    * _object->sectionname->key] = value
    * _object->sectionname->key[0] = value_one
    * _object->sectionname->key[1] = value_two
    */
    var $_data;

    /**
    * store filename
    */
    var $file;

    /**
    * keep track of last section
    */
    var $_last_section = '';

    /**
    * Start Ini and set file
    */
    function Ini ($file) {
    $this->file = $file;
    }

    /**
    * Open file and load in object
    */
    function parse() {
    $fp = fopen ($this->file, 'r');

    while ($data = fgets($fp, 4096)) {
    $this->_parseIni($dat a);
    }

    return $this->_data;
    }

    /**
    * Internal function to handle different section
    */
    function _parseIni($data ) {
    // trim beginning and ending spaces
    $data = trim($data);

    // skip white lines
    if (empty($data))
    return;

    // skip comment lines
    //if (substr($data, 0, 1) == ';')
    if (substr($data, 0, 1) == '#')
    return;

    // section
    if ((substr($data, 0, 1) == '[') AND (substr($data, -1) == ']')) {
    $this->_last_sectio n = substr($data, 1, (strlen($data) - 1));
    return;
    }

    // entry
    $pos = strpos($data, '=');
    if ($pos !== FALSE) { // boolean false
    // set name
    $name = substr($data, 0, $pos);

    // set value
    $value = substr ($data, ($pos + 1), (strlen($data) - $pos - 1));

    // check for comma's and spaces in value, if so make array of it
    if (strpos($value, ", ")) {
    $list = explode (",", $value);

    // unset value
    $value = array();

    // loop through values and add them to array
    foreach ($list as $val) {
    $value[] = trim($val);
    }
    }

    // store value
    $this->_data->{$this->_last_sectio n}->$name = $value;
    }
    }

    /**
    * return parsed ini file
    */
    function get() {
    return $this->_data;
    }
    }
    ?>


    --
    thanks,
    juglesh

  • Janwillem Borleffs

    #2
    Re: stdClass Object - get_object_vars

    juglesh wrote:[color=blue]
    > hello, i have a d/l'd class that gives an array which prints like
    > this:
    >
    > stdClass Object
    > (
    >[/color]

    This is not an array, but an object
    [color=blue]
    > i cannot seem to access the values using
    > $thearray['first name']['inputtype']
    >[/color]

    This works for arrays For objects, you should do this as follows:

    $theobject->{'first name'}->inputtype;

    Note that I used curly braces here because 'first name' contains a space
    character.

    If this wasn't the case, you could do it like this:

    $theobject->firstname->inputtype;
    [color=blue]
    > Here is the class, oh, and maybe someone can see why it's putting two
    > end brackets on the [first name]], etc.
    >[/color]
    [...][color=blue]
    > // section
    > if ((substr($data, 0, 1) == '[') AND (substr($data, -1) == ']')) {
    > $this->_last_sectio n = substr($data, 1, (strlen($data) - 1));
    > return;
    > }
    >[/color]

    $this->_last_sectio n = substr($data, 1, - 1);

    BTW, you should also take a look at the parse_ini_file function:




    JW



    Comment

    • juglesh

      #3
      Re: stdClass Object - get_object_vars


      Janwillem Borleffs wrote:[color=blue]
      > juglesh wrote:[color=green]
      > > hello, i have a d/l'd class that gives an array which prints like
      > > this:
      > >
      > > stdClass Object
      > > (
      > >[/color]
      >
      > This is not an array, but an object
      >[color=green]
      > > i cannot seem to access the values using
      > > $thearray['first name']['inputtype']
      > >[/color]
      >
      > This works for arrays For objects, you should do this as follows:
      >
      > $theobject->{'first name'}->inputtype;
      >
      > Note that I used curly braces here because 'first name' contains a space
      > character.
      >
      > If this wasn't the case, you could do it like this:
      >
      > $theobject->firstname->inputtype;
      >[color=green]
      > > Here is the class, oh, and maybe someone can see why it's putting two
      > > end brackets on the [first name]], etc.
      > >[/color]
      > [...][color=green]
      > > // section
      > > if ((substr($data, 0, 1) == '[') AND (substr($data, -1) == ']')) {
      > > $this->_last_sectio n = substr($data, 1, (strlen($data) - 1));
      > > return;
      > > }
      > >[/color]
      >
      > $this->_last_sectio n = substr($data, 1, - 1);[/color]

      both suggestions work fine, thank you very much. in fact, the first
      fix didnt work till I did the second.[color=blue]
      >
      > BTW, you should also take a look at the parse_ini_file function:
      >
      > http://www.php.net/manual/en/functio...e-ini-file.php[/color]

      i am aware of this, thanks. The main reason i liked the class above
      was:
      // check for comma's and spaces in value, if so make array of it
      if (strpos($value, ", ")) {
      $list = explode (",", $value);
      though, really, I could just send the results of parse-ini-file through
      that. Also, for this thing i'm doing, I might need even more
      dimensions with named keys, so, I guess I'll need to modify the code to
      look for [key] within the values, etc.

      Comment

      Working...