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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • khalidbaloch
    New Member
    • Oct 2006
    • 61

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

    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


    Code:
    <?php
    // Parsing Yahoo! REST Web Service results using
    // unserialize. PHP4/PHP5
    // Author: Jason Levitt
    // February 1, 2006
    
    // output=php means that the request will return serialized PHP
    $request =  'http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=dboyzclan&query=php sample&results=4&output=php';
    
    $response = file_get_contents($request);
    
    if ($response === false) {
    	die('Request failed');
    }
    
    $phpobj = unserialize($response);
    
    echo '<pre>';
    //this will print the arrays
    print_r($phpobj);
    echo '</pre>';
    ?>
    allthogh this will show the result
    Code:
    <?php
    
    //show result no 1
    //this will show the url of result 1
    echo $phpobj['ResultSet']['Result']['0']['Url'];
    echo '<br>';
    //this will show the title of result no 1
    echo $phpobj['ResultSet']['Result']['0']['Title'];
    echo '<br>';
    
    //this will show the Summary of result no 1
    echo $phpobj['ResultSet']['Result']['0']['Summry'];
    echo '<br>';
    
    //Result no 2
    
    
    //this will show the url of result 2
    echo $phpobj['ResultSet']['Result']['1']['Url'];
    echo '<br>';
    //this will show the title of result no 2
    echo $phpobj['ResultSet']['Result']['1']['Title'];
    echo '<br>';
    
    //this will show the Summary of result no 2
    echo $phpobj['ResultSet']['Result']['1']['Summry'];
    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
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    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={$entr y['Url']}<br>";
    echo "TIT={$entr y['Title']}<br>";
    echo "SUM={$entr y['Summry']}<br>";
    }
    }
    }

    ?>[/php]
    Ronald :cool:

    Comment

    Working...