2014-09-30 51 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