2014-09-30 91 views
1

林具有一些問題顯示多維數組...3維陣列的foreach

$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' 
             ) 
       ) 
    ); 

基本上我想顯示的一切,也將其保存在變量...

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

     }  
     echo "</ul>"; 

我試圖插入另一組foreach裏面,但它給了我一個 爲foreach提供的無效參數()

+0

嘗試foreaching'$ copyscape [ '結果'] [ '數']'。你也可以發佈數組的更大部分? – 2014-09-30 05:48:18

回答

4

嘗試使用此代碼:

$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>"; 
+0

+1在這裏使用'array_walk_recursive'的好方法! – Darren 2014-09-30 06:05:44

+0

太棒了!謝謝! – Alice 2014-09-30 06:35:34

+0

很高興幫助你:) – 2014-09-30 06:36:06

1

使用此

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 
} 
} 

} 

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 
    } 


    } 
0

在情況下,如果你想擁有列表(嵌套級別)的內部名單..

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); 
0

試試下面的代碼:

<?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>"; 
    } 
+1

很棒!謝謝! – Alice 2014-09-30 06:42:55

0

嘗試具有遞歸功能的東西,如下所示:

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); 
0

使用此:

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>"; 

代碼打印nested list包括嵌套列表的key name

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 
0

使用此

<?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>"; 
} 
?> 
0

試試這個..

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>'; 
      } 
     }   
} 
+0

很棒!謝謝! – Alice 2014-09-30 06:41:25