www.webdeveloper.com
Results 1 to 5 of 5

Thread: php associative array parse issue

  1. #1
    Join Date
    May 2010
    Posts
    23

    Question php associative array parse issue

    Hello everybody,

    I have a quick php array issue:

    I want parse the following array:

    Code:
     Array ( [Heading1] => Someheading [0] => Array ( [0] => stdClass Object ( [par] => 3 [content] => hello world [id] => 1 ) ) )

    I have tried a foreach loop but only returns 'heading1', should there be a counter within this loop?


    note: the array can have more than one heading.



    thanks in advance

  2. #2
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,279
    I think that you mean by "parse" something different from what I mean. What exactly are you trying to accomplish?
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  3. #3
    Join Date
    May 2010
    Posts
    23

    Exclamation

    Quote Originally Posted by Charles View Post
    I think that you mean by "parse" something different from what I mean. What exactly are you trying to accomplish?
    by parsing i mean looping and outping each of the array value.


    oops i should have showed a example,

    i want the array to be outputted in the following format:

    [Heading1] Someheading
    [par] 3
    [content] hello world
    [id] 1


    Heading1 will be array which contain a array inside it storing the information about that heading, there can be more than one Heading1 arrays that why i need a loop.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,634
    Something like this? (I know, it's ugly.)
    PHP Code:
    <?php
    /*
    Array (
       [Heading1] => Someheading
       [0] => Array (
          [0] => stdClass Object (
             [par] => 3
             [content] => hello world
             [id] => 1
          )
       )
    )
    */

    // create test data:
    $obj = new stdClass();
    $obj->par 3;
    $obj->content 'hello world';
    $obj->id 1;
    $data = array(
       
    'Heading1' => 'Someheading',
       
    => array(
          
    => $obj
       
    )
    );

    // extract data:
    foreach($data as $key => $value) {
       if(
    is_array($value)) {
          foreach(
    $value as $key2 => $val2) {
             if(
    is_object($val2)) {
                
    $vars get_object_vars($val2);
                foreach(
    $vars as $key3 => $val3) {
                   echo 
    "$key3$val3<br />\n";
                }
             }
          }
       }
       else echo 
    "$key$value<br />\n";
    }
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    May 2010
    Posts
    23
    Quote Originally Posted by NogDog View Post
    Something like this? (I know, it's ugly.)
    PHP Code:
    <?php
    /*
    Array (
       [Heading1] => Someheading
       [0] => Array (
          [0] => stdClass Object (
             [par] => 3
             [content] => hello world
             [id] => 1
          )
       )
    )
    */

    // create test data:
    $obj = new stdClass();
    $obj->par 3;
    $obj->content 'hello world';
    $obj->id 1;
    $data = array(
       
    'Heading1' => 'Someheading',
       
    => array(
          
    => $obj
       
    )
    );

    // extract data:
    foreach($data as $key => $value) {
       if(
    is_array($value)) {
          foreach(
    $value as $key2 => $val2) {
             if(
    is_object($val2)) {
                
    $vars get_object_vars($val2);
                foreach(
    $vars as $key3 => $val3) {
                   echo 
    "$key3$val3<br />\n";
                }
             }
          }
       }
       else echo 
    "$key$value<br />\n";
    }


    thanks i will try this out,

    but im still trying to work out a more clean solution.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles