English 中文(简体)
在 php 中动态读取嵌套数组
原标题:Read nested array dynamicly in php
  • 时间:2012-05-22 13:09:42
  •  标签:
  • php
  • arrays

我需要读取嵌套阵列, 却不知道阵列的外观 。

例如;

$data = array(
         Data1_lvl1  => array(
                             Data1_lvl2  => "value",
                             Data2_lvl2  => array(
                                                 Data1_lvl3  => "value"
                                            )
                        ),
         Data2_lvl1  =>  value 
    );

需要格式化为字符串类型 :

  1. Data1_lvl1/Data1_lvl2/
  2. Data1_lvl1/Data2_lvl2/Data1_lvl3/
  3. Data2_lvl1/

但数组可以是任意大小的, 里面有数个嵌套数组 。

最佳回答
$data = array(
         Data1_lvl1  => array(
                             Data1_lvl2  => "value",
                             Data2_lvl2  => array(
                                                 Data1_lvl3  => "value"
                                            )
                        ),
         Data2_lvl1  =>  value 
    );


function printArray($array)
{
    foreach ($array as $key=>$value)
    {
       echo $key. / ;
       if (is_array($value))
       {
          printArray($value);           
       } else {    
        echo  <br> ;
        }

    }

}


printArray($data);
问题回答

如果您想要只输出数组元素的名称, 这个递归函数将执行此操作 。

<强> 您的数据:

$data = array(
         Data1_lvl1  => array(
                             Data1_lvl2  => "value",
                             Data2_lvl2  => array(
                                                 Data1_lvl3  => "value"
                                            )
                        ),
         Data2_lvl1  =>  value 
    );

<强 > 功能:

function array2str($array, $str) {
  foreach($array as $key => $val) {
    if (is_array($val) ) {
        $str .= $key .  / ;
        array2str($val, $str);
    }
  }
  echo $str. <br /> ;  
  return $str;
}

array2str($data);

您可以看到, 当浏览器查看结果时, ECHO 本身会使用 来打破线条 。

一种方式是循序走过阵列, 其功能类似 :

<?php
function f($d, $str =   ) {
        foreach ($d as $key => $val) {
                if (is_array($val)) f($val, $str .  /  . $key); // If this element is array parse next level
                else print_r($str .  /  . $key .  / ); // Output current string or do what you need to do with it..
        }
}

$data = array(
         Data1_lvl1  => array(
                             Data1_lvl2  => "value",
                             Data2_lvl2  => array(
                                                 Data1_lvl3  => "value"
                                            )
                        ),
         Data2_lvl1  =>  value 
);

f($data);

具有此函数的:

<?php
function print_tree ($data, $prefix =   , &$index = 1) {
  foreach($data as $key => $datum) {
    echo $index++ .  .   . ($new_prefix =  $prefix . $key .  / ) . PHP_EOL;
    if (is_array($datum)) {
      print_tree ($datum, $new_prefix, $index);
    }
  }
}

我得到

  1. Data1_lvl1/
  2. Data1_lvl1/Data1_lvl2/
  3. Data1_lvl1/Data2_lvl2/
  4. Data1_lvl1/Data2_lvl2/Data1_lvl3/
  5. Data2_lvl1/




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签