473,320 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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!
Feb 24 '08 #1
8 1752
Markus
6,050 Expert 4TB
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
$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
4,258 Expert 4TB
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
That looks right to me.

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

Thanks,
Feb 24 '08 #5
ronverdonk
4,258 Expert 4TB
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
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
6,050 Expert 4TB
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
4,258 Expert 4TB
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

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

Similar topics

4
by: Jody Gelowitz | last post by:
I am having a problem with printing selected pages. Actually, the problem isn't with printing selected pages as it is more to do with having blank pages print for those pages that have not been...
9
by: Jody Gelowitz | last post by:
I am trying to find the definition of "Safe Printing" and cannot find out exactly what this entitles. The reason is that I am trying to print contents from a single textbox to no avail using the...
4
by: Arif | last post by:
I C# code prints very slow as compared to a third party barcode printing software. That software prints approximately 10 labels in 2 seconds while my C# code prints 10 labels in 5 to 6 seconds. And...
1
by: mark | last post by:
I can't seem to get the logic for printing multiple pages. I know I have to do a comparison between the bottom margin and the position of the next line to be printed and initiate a has more pages...
6
by: Siv | last post by:
Hi, I am getting into printing with VB.NET 2005 and want to implement the usual capability that a user can select a selection of pages. I have a report that is generated by my application that if...
10
by: Pedro Pinto | last post by:
Hi there! I'm creating a function that copies some information to a buffer and enters a delimiter string "//" between results. My issue here is when i print the buffer it appears this weird...
4
by: Charles | last post by:
I'm trying an example from the book "Learning PHP5" but it doesn't work as Expected. Snippet: <form method="POST" action="index.php"> <select name="lunch" multiple> <option value="pork">BBQ...
0
by: =?Utf-8?B?TWFkRG9nc0RhZA==?= | last post by:
I'm new to VB2005 and was told to look for a dotnet group. Am I in the right place!?! I have a form with a variety of controls which provide criteria for a data search, which is triggered by a...
3
by: jmooney5115 | last post by:
Hey. I am new to php and am trying to learn. What I'm doing is querying(did I spell this right?) a mySQL database and putting the results into a table on a webpage. I have worked for hours on this...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.