English 中文(简体)
来自一组密钥组的 Php 阵列
原标题:Php array from set of keys
  • 时间:2012-05-26 11:08:20
  •  标签:
  • php
  • arrays

找到帮助我的这个文章: < a href="https://stackoverflow.com/ questions/10519108/split-a-string-to-for-for-disible-array-keys" > 将字符串切换成多维数组密钥?

无论如何,当涉及到字符串值时,这就像一个符咒,但如果数组键含有整数,它就会错过这些。

以下是演示:

我得到了一套密钥 :

Array
(
    [0] => variable_data
    [1] => 0
    [2] => var_type
)

创建 e 嵌套阵列的方法

function constructArray( &$array_ptr, $keys, $value )
    {
        // extract the last key
        $last_key = array_pop ( $keys );

        // walk/build the array to the specified key
        while ( $arr_key = strval( array_shift ( $keys ) ) )
        {
            if ( !array_key_exists ( strval($arr_key), $array_ptr ) )
            {
                $array_ptr[ strval($arr_key) ] = array ( );
            }
            $array_ptr = &$array_ptr[ strval($arr_key) ];
        }

        // set the final key
        $array_ptr[ $last_key ] =  $value ;
    }

我用这种方式:

$keys = array(
     variable_data ,
     0 ,
     var_type 
);
    $clean_arr = array();
    constructArray($clean_arr, $keys,  asd );

但输出看起来是这样的:

Array
(
    [variable_data] => Array
        (
            [var_desc] => $value
        )

)

正如你所见,变量数据指数并不包含 0 指数。 我测试了一切我可能知道的工作,但却没有。 谁有更好的线索呢?

最佳回答

Solution

这里的函数可以发挥魔法的作用 :

function constructArray( &$array_ptr, $keys, $value )
    {
        // extract the last key
        $last_key = array_pop ( $keys );

        foreach ( $keys as $arr_key )
        {
             unset($keys[$arr_key]);
             if ( !array_key_exists ( strval($arr_key), $array_ptr ) )
            {
                $array_ptr[ strval($arr_key) ] = array ( );
            }
            $array_ptr = &$array_ptr[ strval($arr_key) ];
        }

        // set the final key
        $array_ptr[ $last_key ] = $value;
    }

用法 :

$keys = array( variable_data ,  0 ,  var_desc );
$clean_arr = array();
constructArray($clean_arr, $keys,  asd );

// Output
Array
(
    [variable_data] => Array
        (
            [0] => Array
                (
                    [var_desc] => asd
                )

        )

)
问题回答

暂无回答




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

热门标签