如何在php

时间:2016-08-05 09:12:05

标签: php arrays multi-level

我有以下数组:

$array=array("string",array(1,2,3),true,"php");

我想要打印索引,如:

0=>string


1.0=>1


1.1=>2


1.2=>3


2=>true


3=>php

6 个答案:

答案 0 :(得分:0)

<?php

$array=array("string",array(1,2,3),true,"php");

foreach($array as $key=>$value)
{
  if(is_array($value))
  {
    foreach($value as $childkey=>$childvalue)
    {
      echo $key . "." . $childkey . "=>" . $childvalue . "\n";
    }
  }
  elseif(is_bool($value))
  {
    echo $key . "=>" . ($value ? "true" : "false") . "\n";
  }
  else
  {
    echo $key . "=>" . $value . "\n";
  }
}

输出:

0=>string
1.0=>1
1.1=>2
1.2=>3
2=>true
3=>php

答案 1 :(得分:0)

试试这个

<?php 
$array=array("string",array(1,2,3),true,"php");
foreach($array as $key=>$value){
    if(is_array($value)){
        foreach($value as $key1=>$value1){
            echo $key.".".$key1." => ".$value1."</br>";             
        }           
    }
    else{
        echo $key." => ".$value."</br>";            
    }       
}

答案 2 :(得分:0)

<?php 

$array=array("string",array(1,2,3),"true","php");

foreach($array as $key=>$value){
   if(is_array($value)){
       foreach($value as $key1=>$loop){
         echo $key.'.'.$key1 .'=>'.$loop."<br>";

       }
   }else{
      echo $key .'=>'.$value."<br>";
   }

}
?>

答案 3 :(得分:0)

试用此代码

$array=array("string",array(1,2,3),true,"php");

foreach($array as $key=>$val){
       if(is_array($val)){
           foreach($val as $key1=>$val1){
             echo $key.'.'.$key1 .'=>'.$val1.'<br/>';

           }
       }else{
          echo $key .'=>'.$val.'<br/>';
       }
}

答案 4 :(得分:0)

你可以试试这个 -

<?php
$arr=array("string",array(1,2,3),true,"php");
$res=convArray($arr);
foreach($res as $k=>$v){
    echo $k."=>".$v."\n";
}

function convArray($arr)
{
    foreach($arr as $k1=>$v1){
        if(is_array($v1)){
            foreach($v1 as $k2=>$v2){
                $res[$k1.'.'.$k2]=$v2;
            }       
        }else{
            $res[$k1]=$v1;
        }
    }
    return $res;
}

&GT;

答案 5 :(得分:0)

试试这个:

<?php
$array=array("string",array(1,2,3,array('a','b','c')),true,"php");
$kt = array();
function showarray($arr,$k) {
    global $kt;
    foreach($arr as $key => $v) {
        $nk = $k == '' ? $key:$k.'.'.$key;
        if(is_array($v)) {
            showarray($v,$nk);
        } else {
            $kt[$nk]  = $v;
        }
    }
}
showarray($array,"");
print_r($kt);