English 中文(简体)
PHP 复读阵列
原标题:PHP recursive array copy

我有多阵列(资料来源如下),我的关键和价值观是任意的。 我想建立一个新的阵列(见下文),其等级不变,但每个科索沃警察部队都改组为自己的阵列,关键本身成为所有权价值,任何分支都继续在儿童下进行。

我如何利用回收功能来完成这项工作,这种功能既要花钱又花钱,还要花钱和花钱,并相应地把目的地集中起来?

资料来源:Array。

Array (
    [Alpha] => Array (
        [Red] => one
        [Blue] => two
    )
    [Bravo] => Array (
        [Blue] => three
    )
)

希望:

Array (
    [0] => Array (
        [title] => Alpha
        [children] => Array (
                    [0] Array([title] => Red, [children]= > false)
                    [1] Array([title] => Blue, [children]= > false)
        )
    )
    [1] => Array (
        [title] => Bravo
                    [0] Array([title] => Blue, [children]= > false)
        )
    )
)

注:我不关心我新阵列的最后节点/传单。

最佳回答

您可以在不传递对目标数组的引用的情况下进行te转换。

function convert_array($from){
    if(!is_array($from)){
        return false;
    }
    $to = array();
    foreach($from as $k=>$v){
        $to[] = array(
             title  => $k,
             children  => convert_array($v)
        );
    }
    return $to;
}

http://codepad.org/cdY8F6E1”rel=“nofollow”>Codepad example

问题回答

我重新规划了阵列。

$a = array(
     Alpha  => array( Red  =>  one ,  Blue  =>  two ),
     Bravo  => array( Blue  =>  three ));


function copyarray($datas){
    $arr = array();

            if(is_array($datas)){
                foreach($datas as $key => $val){
                    if(is_array($val)){
                    $arr[$key] = copyarray($val);
                    }else
                    $arr[$key]=$val;
                }
            }
            return $arr;
}

echo "<pre>";print_r(copyarray($a));




相关问题
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 ...

热门标签