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

using foreach loop to get values of a multi-dimensional arrays

khalidbaloch
P: 61
hi every one, how are you folf , hope fine

dear Friends i want to get values of multi-dimensional arrays using foreach loop and after that print out the html using an other while loop ,
i tried alot but did not successed accutly i want to create an yahoo api websearch application i got a sample from developer.yahoo.com for this purpose ,this sample uses unserialize/serialize php
here is the code exapmle


Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Parsing Yahoo! REST Web Service results using
  3. // unserialize. PHP4/PHP5
  4. // Author: Jason Levitt
  5. // February 1, 2006
  6.  
  7. // output=php means that the request will return serialized PHP
  8. $request =  'http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=dboyzclan&query=php sample&results=4&output=php';
  9.  
  10. $response = file_get_contents($request);
  11.  
  12. if ($response === false) {
  13.     die('Request failed');
  14. }
  15.  
  16. $phpobj = unserialize($response);
  17.  
  18. echo '<pre>';
  19. //this will print the arrays
  20. print_r($phpobj);
  21. echo '</pre>';
  22. ?>
allthogh this will show the result
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. //show result no 1
  4. //this will show the url of result 1
  5. echo $phpobj['ResultSet']['Result']['0']['Url'];
  6. echo '<br>';
  7. //this will show the title of result no 1
  8. echo $phpobj['ResultSet']['Result']['0']['Title'];
  9. echo '<br>';
  10.  
  11. //this will show the Summary of result no 1
  12. echo $phpobj['ResultSet']['Result']['0']['Summry'];
  13. echo '<br>';
  14.  
  15. //Result no 2
  16.  
  17.  
  18. //this will show the url of result 2
  19. echo $phpobj['ResultSet']['Result']['1']['Url'];
  20. echo '<br>';
  21. //this will show the title of result no 2
  22. echo $phpobj['ResultSet']['Result']['1']['Title'];
  23. echo '<br>';
  24.  
  25. //this will show the Summary of result no 2
  26. echo $phpobj['ResultSet']['Result']['1']['Summry'];
  27. echo '<br>';?>
and there numberiuos thing in that array which can be print by print_r(); function

but what i want to do increament dynamically the third value of array using a loop , but how to do that any help would be highly appriciated
Jan 8 '07 #1
Share this Question
Share on Google+
1 Reply


ronverdonk
Expert 2.5K+
P: 4,258
I do not really understand what you are asking for, so I'll guess: you want to list the url, title and summary entries of this array? I'll show a cde snippet that does that, but is this what you mean and want?
[php]<?php
// ----------- Setup ther test array
$phpobj=array();
$phpobj['ResultSet']['Result']['0']['Url'] = 'URL no 0';
$phpobj['ResultSet']['Result']['0']['Title']='TITLE no 0';
$phpobj['ResultSet']['Result']['0']['Summry']='SUMMARY no 0';
$phpobj['ResultSet']['Result']['1']['Url'] = 'URL no 1';
$phpobj['ResultSet']['Result']['1']['Title']='TITLE no 1';
$phpobj['ResultSet']['Result']['1']['Summry']='SUMMARY no 1';
$phpobj['ResultSet']['Result']['2']['Url'] = 'URL no 2';
$phpobj['ResultSet']['Result']['2']['Title']='TITLE no 2';
$phpobj['ResultSet']['Result']['2']['Summry']='SUMMARY no 2';

// Print out the entries
foreach ($phpobj as $resultset) {
foreach ($resultset as $result) {
foreach ($result as $entry) {
echo "URL={$entry['Url']}<br>";
echo "TIT={$entry['Title']}<br>";
echo "SUM={$entry['Summry']}<br>";
}
}
}

?>[/php]
Ronald :cool:
Jan 8 '07 #2

Post your reply

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