By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,521 Members | 1,492 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,521 IT Pros & Developers. It's quick & easy.

Printing out results from an array.

P: 9
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!
Feb 24 '08 #1
Share this Question
Share on Google+
8 Replies


Markus
Expert 5K+
P: 6,050
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]
Feb 24 '08 #2

P: 9
$api = new duckSoup; // create a new object

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

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

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

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


print_r($content);
Feb 24 '08 #3

ronverdonk
Expert 2.5K+
P: 4,258
markusn00b: is this what you mean:
Expand|Select|Wrap|Line Numbers
  1. Array
  2. (
  3.     [result] => Array
  4.         (
  5.             [queryterm] => Britney Spears
  6.             [days] => 7
  7.             [searchurl] => http://technorati.com/search/Britney+Spears
  8.         )
  9.     [item] => Array
  10.         (
  11.             [0] => Array
  12.                 (
  13.                     [date] => 2008-02-24
  14.                     [count] => 889
  15.                 )
  16.             [1] => Array
  17.                 (
  18.                     [date] => 2008-02-23
  19.                     [count] => 2403
  20.                 )
  21.             [2] => Array
  22.                 (
  23.                     [date] => 2008-02-22
  24.                     [count] => 2539
  25.                 )
  26.             [3] => Array
  27.                 (
  28.                     [date] => 1985
  29.                     [count] => 1775
  30.                 )
  31.             [4] => Array
  32.                 (
  33.                     [date] => 2008-02-20
  34.                     [count] => 1231
  35.                 )
  36.             [5] => Array
  37.                 (
  38.                     [date] => 2008-02-19
  39.                     [count] => 2814
  40.                 )
  41.             [6] => Array
  42.                 (
  43.                     [date] => 2008-02-18
  44.                     [count] => 2510
  45.                 )
  46.         )
  47. )
Ronald
Feb 24 '08 #4

P: 9
That looks right to me.

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

Thanks,
Feb 24 '08 #5

ronverdonk
Expert 2.5K+
P: 4,258
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<ul>";
foreach($arr as $key => $val)
if(is_array($val))
show_arr($val, $lvl + 1);
else
print "$padding<li>$key - $val</li>";
print "$padding</ul>";
}[/php]
Ronald
Feb 24 '08 #6

P: 9
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.
Feb 24 '08 #7

Markus
Expert 5K+
P: 6,050
Sorry to stray from point - just trying to fathom out a way of doing this by myself.
Why does this give me
Expand|Select|Wrap|Line Numbers
  1. Parse error: parse error, unexpected T_ARRAY, expecting ')' in C:\Program Files\EasyPHP 2.0b1\www\test\arrau.php on line 20
  2.  
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($products);
[/php]
I thought you were allowed to nest arrays?
Feb 24 '08 #8

ronverdonk
Expert 2.5K+
P: 4,258
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
Feb 24 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.