I have the following array:
我有如下的数组:
$array=array("string",array(1,2,3),true,"php");
and I want to print indexes like:
我想打印索引,比如
0=>string
1.0=>1
1.1=>2
1.2=>3
2=>true
3=>php
6 个解决方案
#1
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";
}
}
Output:
输出:
0=>string
1.0=>1
1.1=>2
1.2=>3
2=>true
3=>php
#2
0
Try this
试试这个
<?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>";
}
}
#3
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>";
}
}
?>
#4
0
Try this code
试试这个代码
$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/>';
}
}
#5
0
You can try this-
你可以试试这个
<?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;
}
?>
? >
#6
0
Try this:
试试这个:
<?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);
#1
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";
}
}
Output:
输出:
0=>string
1.0=>1
1.1=>2
1.2=>3
2=>true
3=>php
#2
0
Try this
试试这个
<?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>";
}
}
#3
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>";
}
}
?>
#4
0
Try this code
试试这个代码
$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/>';
}
}
#5
0
You can try this-
你可以试试这个
<?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;
}
?>
? >
#6
0
Try this:
试试这个:
<?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);