Alice
Alice

Reputation: 13

3 dimension Array foreach

Im having some problem displaying a multidimensional array...

$copyscape = array (
                'query' => 'www.example.html',
                'querywords' => 444,
                'count' => 230,             
                'result' => array(
                                        'number' => array(
                                                'index' => 1,   
                                                'url' => 'http://www.archives.gov/exhibits/charters/declaration_transcript.html',
                                                'title' => 'Declaration of Independence - Text Transcript',
                                                'minwordsmatched' => 406,
                                                'viewurl' => 'http://view.copyscape.com/compare/w4med9eso0/1'
                                        )
                )
    );

Basically I want to display everything and also save it in a variable...

echo "<ul>";
        foreach($copyscape as $name => $value)
        {
            echo "<li>" . $name . " : ". $value . "</li>";      

        }       
        echo "</ul>";

I tried inserting another set of foreach inside but it gives me an Invalid argument supplied for foreach()

Upvotes: 1

Views: 129

Answers (8)

kums
kums

Reputation: 2691

Use this:

function print_stuff($arr){
    foreach($arr as $key => $val){
        if(is_array($val)){
            echo "$key: <ul>";
            print_stuff($val);
            echo "</ul>";
        } else {
            echo "<li>$key : $val</li>\n";
        } 
    }
}

echo "<ul>";
print_stuff($copyscape);
echo "</ul>";

The code prints a nested list including the key name of the nested lists:

query : www.example.html
querywords : 444
count : 230
result:
    number:
        index : 1
        url : http://www.archives.gov/exhibits/charters/declaration_transcript.html
        title : Declaration of Independence - Text Transcript
        minwordsmatched : 406
        viewurl : http://view.copyscape.com/compare/w4med9eso0/1

Upvotes: 0

noufalcep
noufalcep

Reputation: 3536

try this..

echo '<ul>';
displayList($copyscape);
echo '</ul>';

function displayList($arr)
{
      foreach($arr as $name => $value) {
          if (is_array($value)) {
                displayList($value);
          }  
          else {
                echo '<li>';
                echo $name . ' : ' . $value;  
                echo '</li>';
          }  
      }        
}

Upvotes: 0

Hardik Solanki
Hardik Solanki

Reputation: 3195

Try with this code :

$copyscape = array (
                'query' => 'www.example.html',
                'querywords' => 444,
                'count' => 230,             
                'result' => array(
                                        'number' => array(
                                                'index' => 1,   
                                                'url' => 'http://www.archives.gov/exhibits/charters/declaration_transcript.html',
                                                'title' => 'Declaration of Independence - Text Transcript',
                                                'minwordsmatched' => 406,
                                                'viewurl' => 'http://view.copyscape.com/compare/w4med9eso0/1'
                                        )
                )
    );
function test_print($item, $key)
{
    echo "<li>" . $key . " : ". $item . "</li>";
}
echo "<ul>";
array_walk_recursive($copyscape, 'test_print');
echo "</ul>";

Upvotes: 4

Roshan Dandgavhal
Roshan Dandgavhal

Reputation: 559

Use this

<?php
$copyscape = array (
                    'query' => 'www.example.html',
                    'querywords' => 444,
                    'count' => 230,             
                    'result' => array(
                                            'number' => array(
                                                    'index' => 1,   
                                                    'url' => 'http://www.archives.gov/exhibits/charters/declaration_transcript.html',
                                                    'title' => 'Declaration of Independence - Text Transcript',
                                                    'minwordsmatched' => 406,
                                                    'viewurl' => 'http://view.copyscape.com/compare/w4med9eso0/1'
                                            )
                    )
        );
PrintMultiDimArray($copyscape);
function PrintMultiDimArray($copyscape) {
    echo "<ul>";
    foreach ($copyscape as $key=>$val) {
        if(is_array($val)){
            echo "<li>";
            PrintMultiDimArray($val);
            echo "</li>";
        }else{
            echo "<li>" . $key . " : ". $val . "</li>";
        }
    }
    echo "</ul>";
}
?>

Upvotes: 0

S.Pols
S.Pols

Reputation: 3434

Try something with a recursive function, like this:

function printArray($array)
{
    echo "<ul>";

    foreach($array as $name=>$value)
    {   
        if(is_array($value))
        {
            printArray($value);   
        }
        else
        {
            echo "<li>" . $name . " : ". $value . "</li>";    
        }
    }

    echo "</ul>";
}

$copyscape = array (
                'query' => 'www.example.html',
                'querywords' => 444,
                'count' => 230,             
                'result' => array(
                                        'number' => array(
                                                'index' => 1,   
                                                'url' => 'http://www.archives.gov/exhibits/charters/declaration_transcript.html',
                                                'title' => 'Declaration of Independence - Text Transcript',
                                                'minwordsmatched' => 406,
                                                'viewurl' => 'http://view.copyscape.com/compare/w4med9eso0/1'
                                        )
                )
    );

printArray($copyscape);

Upvotes: 0

Bhumi Shah
Bhumi Shah

Reputation: 9476

Try following code:

<?php
    $copyscape = array (
                    'query' => 'www.example.html',
                    'querywords' => 444,
                    'count' => 230,             
                    'result' => array(
                                            'number' => array(
                                                    'index' => 1,   
                                                    'url' => 'http://www.archives.gov/exhibits/charters/declaration_transcript.html',
                                                    'title' => 'Declaration of Independence - Text Transcript',
                                                    'minwordsmatched' => 406,
                                                    'viewurl' => 'http://view.copyscape.com/compare/w4med9eso0/1'
                                            )
                    )
        );
    MultiDimRecuArray($copyscape);
    function MultiDimRecuArray($copyscape) {
        echo "<ul>";
        foreach ($copyscape as $key=>$val) {
            if(is_array($val)){
                MultiDimRecuArray($val);
            }else{
                echo "<li>" . $key . " : ". $val . "</li>";
            }
        }
        echo "</ul>";
    }

Upvotes: 0

Cheery
Cheery

Reputation: 16214

In case if you want to have lists inside of lists (nested levels)..

function outputLevel($arr)
{
  echo '<ul>';
  foreach($arr as $name => $value)
  {
   echo '<li>';
   if (is_array($value))
      outputLevel($value);
   else 
      echo $name . ' : ' . $value;
   echo '</li>'      
  }        
  echo '</ul>';
}

outputLevel($copyscape);

Upvotes: 0

Harutyun Abgaryan
Harutyun Abgaryan

Reputation: 2023

use this

foreach ($copyscape as $key=>$value){

echo "<li>" . $key. " : ". $value. "</li>";      // this for main Array

if (is_array ($value))
{
  foreach ($value as $childArrayKey => $childArrayValue ){
echo "<li>" . $childArrayKey . " : ". $childArrayValue . "</li>"; // this for childe array 
}
}

}

or

 foreach ($copyscape as $key=>$value){

    echo "<li>" . $key. " : ". $value. "</li>";      // this for main Array


      foreach ($value['result']['number'] as $childArrayKey => $childArrayValue ){
    echo "<li>" . $childArrayKey . " : ". $childArrayValue . "</li>"; // this for childe array 
    }


    }

Upvotes: 1

Related Questions