Printing out results from an array.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • a2rodger
    New Member
    • Dec 2007
    • 9

    Printing out results from an array.

    I am relatively new to php and am struggling with printing out each result from the
    following array.

    Array ( [result] => Array ( [queryterm] => Britney Spears [days] => 7 [searchurl] => http://technorati.com/search/Britney+Spears ) [item] => Array ( [0] => Array ( [date] => 2008-02-24 [count] => 889 ) [1] => Array ( [date] => 2008-02-23 [count] => 2403 ) [2] => Array ( [date] => 2008-02-22 [count] => 2539 ) [3] => Array ( [date] => 2008-02-21 [count] => 1775 ) [4] => Array ( [date] => 2008-02-20 [count] => 1231 ) [5] => Array ( [date] => 2008-02-19 [count] => 2814 ) [6] => Array ( [date] => 2008-02-18 [count] => 2510 ) ) )

    I've tried using a foreach loop like 'foreach ($content as $key_val => $value) '

    but that doesn't seem to work.

    Pleas help!
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Originally posted by a2rodger
    I am relatively new to php and am struggling with printing out each result from the
    following array.

    Array ( [result] => Array ( [queryterm] => Britney Spears [days] => 7 [searchurl] => http://technorati.com/search/Britney+Spears ) [item] => Array ( [0] => Array ( [date] => 2008-02-24 [count] => 889 ) [1] => Array ( [date] => 2008-02-23 [count] => 2403 ) [2] => Array ( [date] => 2008-02-22 [count] => 2539 ) [3] => Array ( [date] => 2008-02-21 [count] => 1775 ) [4] => Array ( [date] => 2008-02-20 [count] => 1231 ) [5] => Array ( [date] => 2008-02-19 [count] => 2814 ) [6] => Array ( [date] => 2008-02-18 [count] => 2510 ) ) )

    I've tried using a foreach loop like 'foreach ($content as $key_val => $value) '

    but that doesn't seem to work.

    Pleas help!
    Post the array structure you use to get the above.
    ie.
    [php]
    $_arr = array(...
    [/php]

    Comment

    • a2rodger
      New Member
      • Dec 2007
      • 9

      #3
      $api = new duckSoup; // create a new object

      $api->api_key = "XXX"; // your API key

      $api->type = 'dailycounts'; // what API?

      $api->params = array('q' => ''.$name.'','da ys' => '7'); // the parameters

      $content = $api->get_content( ); // The 'content' Array


      print_r($conten t);

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        markusn00b: is this what you mean:
        Code:
        Array
        (
            [result] => Array
                (
                    [queryterm] => Britney Spears
                    [days] => 7
                    [searchurl] => http://technorati.com/search/Britney+Spears
                )
            [item] => Array
                (
                    [0] => Array
                        (
                            [date] => 2008-02-24
                            [count] => 889
                        )
                    [1] => Array
                        (
                            [date] => 2008-02-23
                            [count] => 2403
                        )
                    [2] => Array
                        (
                            [date] => 2008-02-22
                            [count] => 2539
                        )
                    [3] => Array
                        (
                            [date] => 1985
                            [count] => 1775
                        )
                    [4] => Array
                        (
                            [date] => 2008-02-20
                            [count] => 1231
                        )
                    [5] => Array
                        (
                            [date] => 2008-02-19
                            [count] => 2814
                        )
                    [6] => Array
                        (
                            [date] => 2008-02-18
                            [count] => 2510
                        )
                )
        )
        Ronald

        Comment

        • a2rodger
          New Member
          • Dec 2007
          • 9

          #5
          That looks right to me.

          Any clue how to loop through it and print each result?

          Thanks,

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            This is a routine I found on the web (by Twey at Dynamic Drive). You can rework it to output what you want it to output.
            [php]function show_arr($arr, $lvl = 0) {
            $padding = '';
            for($i = 0; $i < $lvl; ++$i)
            $padding .= "&nbsp;";
            print "$padding<u l>";
            foreach($arr as $key => $val)
            if(is_array($va l))
            show_arr($val, $lvl + 1);
            else
            print "$padding<li>$k ey - $val</li>";
            print "$padding</ul>";
            }[/php]
            Ronald

            Comment

            • a2rodger
              New Member
              • Dec 2007
              • 9

              #7
              That is close to what I am looking for.

              However, I need the date $val and the count $val to have seperate
              variables.

              I am inserting both of them into seperate rows of my mysql database.

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Sorry to stray from point - just trying to fathom out a way of doing this by myself.
                Why does this give me
                Code:
                Parse error: parse error, unexpected T_ARRAY, expecting ')' in C:\Program Files\EasyPHP 2.0b1\www\test\arrau.php on line 20
                When using this:
                [php]
                <?php

                $products = array
                (
                "Result" =>
                array
                (
                "queryterm" => 'Britney Spears',
                "days" => 6,
                "searchurl" => "http://technorati.com/search/Britney+Spears"
                ),
                "Item" =>
                array
                (
                array
                (
                "date" => "2008-02-28",
                "count" => 899,
                )
                array
                (
                "date" => "2008-02-23",
                "count" => 2403,
                )
                )

                );
                echo "<pre>";
                print_r($produc ts);
                [/php]
                I thought you were allowed to nest arrays?

                Comment

                • ronverdonk
                  Recognized Expert Specialist
                  • Jul 2006
                  • 4259

                  #9
                  Originally posted by a2rodger
                  That is close to what I am looking for.

                  However, I need the date $val and the count $val to have seperate
                  variables.

                  I am inserting both of them into seperate rows of my mysql database.
                  As I stated in my earlier post, this is a sample I plucked from the web and you can change that code to do whatever you want. Like checking the $key for the string 'date' or 'count' and then act accordingly.

                  Ronald

                  Comment

                  Working...