duanfuxing2212 2013-02-19 07:00
浏览 140

php - 将字符串拆分为多维数组

Im trying to split navigation position strings into a master array.

For example, If I have an item that's position is 1.2.2

I would like to add it in the master array as follows

 1 =>
    2 =>
        2 => array()

And then if another item has '2.1'

 1 =>
    2 =>
        2 => array()
 2 =>
    1 => array()

and then another '1.2.3'

 1 =>
    2 =>
        2 => array()
        3 => array()
 2 =>
    1 => array()

does anyone know of a way for doing this?

regards

edit

lets say I have a one dimensional array of objectects, I want to loop through them and store as a structured "navigation" like nested array. Each item has a navigation position string, i.e. 1.2.3.6

I then was thinking of $depth = explode( '.', $details['navigation_pos'] ); running it through some kind of array walker to place the object in the correct position.

hope this helps

edit

maybe a better way to put it is this, but more elegantly:

$depth = explode( '.', '1.2.3.4' );
$bar = json_decode( '{"' . implode( '":{"', $depth ) . '":[]' . str_repeat( '}', sizeof( $depth ) ) );
print_r($bar);

which would give

stdClass Object
(
    [1] => stdClass Object
        (
            [2] => stdClass Object
                (
                    [3] => stdClass Object
                        (
                            [4] => Array
                                (
                                )

                        )

                )

        )

)
  • 写回答

1条回答 默认 最新

  • doujia9833 2013-02-19 07:44
    关注

    You could use the eval() construct, but beware:

    The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.

    $final_array = array(); // The output array
    
    /*
        Example:
        $big_array = array(
            '1.1' => 'One-one',
            '2.1.3.4' => 'Two-one-three-four'
        );
    */  
    
    foreach ($big_array as $position_string => $item)
    {
        $index_array = explode(".", $position_string);
    
        foreach ($index_array as $key => $value)
        {       
            // Make sure only integers are put through eval()
            $index_array[$key] = (int)$value;
        }
    
        $indexes = implode("][", $index_array);
    
        // TODO: make sure $item is safe to put through eval()!
        eval("\$final_array[{$indexes}] = \$item");
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭