【问题标题】:Multidimensional array search and replace does not works多维数组搜索和替换不起作用
【发布时间】:2013-08-28 04:50:25
【问题描述】:

我有一个函数可以识别我的 $schema,

根据我的 $schema['replace'] 它替换值。

我的功能失败了。没有按预期工作。

任何人都可以帮助我吗?完成我的功能

$schema = array(
    array(
        'tag' => 'div',
        'class' => 'lines',
        'repeat' => array(
            'tag' => 'div',
             array(
                'tag' => 'span',
                'style' => 'margin:10px; padding:10px',
                'key' => 'title',
            ),
            'key' => 'subject',
        )
    )
);

$repeat = array('Country Name' => 'Usa', 'City Name' => 'Newyork');

function repeat($schema, $repeat){
    foreach($schema as $k => $v){
        if($k == 'repeat'){
            foreach($repeat as $rk => $rv){
                $repeat[] = array_replace($schema,array_fill_keys(array_keys($schema, 'title'),$rk));
                $repeat[] = array_replace($schema,array_fill_keys(array_keys($schema, 'subject'),$rv)); 
            }
        }
    }
    unset($schema[0]['repeat']);
    $schema['repeat'] = $repeat;
    return $schema;
}

print_r(repeat($schema, $repeat));

预期输出

Array
(
    [0] => Array
        (
            [tag] => div
            [class] => lines
            [0] => Array
                (
                    [tag] => div
                    [0] => Array
                        (
                            [tag] => span
                            [style] => margin:10px; padding:10px
                            [key] => Country Name
                        )

                    [key] => Usa
                )

            [1] => Array
                (
                    [tag] => div
                    [0] => Array
                        (
                            [tag] => span
                            [style] => margin:10px; padding:10px
                            [key] => City Name
                        )

                    [key] => Newyork
                )

        )

)

我的功能有什么问题?

【问题讨论】:

    标签: php arrays search multidimensional-array replace


    【解决方案1】:

    我浏览了您的代码并意识到它需要更改。我已经重写了它,这很有效。希望这会有所帮助: 对代码进行了更改,它将适用于每个 $schema 数组。请测试。真是一团糟:)

        <?php
    
    $repeat = array('title1'=>'subject1','title2'=>'subject2','title3'=>'subject3','title4'=>'subject4');
    
    $schema = array(
                    array(
                        array(
                            array(
                                'tag' => 'div',
                                'class' => 'lines',
                                'repeat' => array(
                                    'tag' => 'div',
                                     array(
                                        'tag' => 'span',
                                        'style' => 'margin:10px; padding:10px',
                                        'key' => 'title',
                                    ),
                                    'key' => 'subject',
                                )
                            )
                        ),
    
                        array(
                            'tag' => 'section',
                            'id' => 'user-section',
                            'rel' => 'user'
                        )
                    )
    );
    
    
    
    function repeat($schema, $repeat) {
    
        // Create a duplicate
        $output = $schema;
    
        foreach($schema as $key => $schemaValue) {
    
            // Duplicate $schema on a new array
            $output[$key] = $schemaValue;
    
            // Skip none arrays
            if (!is_array($output[$key]))
                continue;
    
            // Repeat key not in the array: Call recursive function repeat when repeat does not exist
            if (!array_key_exists('repeat', $output[$key])) {
    
                // Recursive call
                $output[$key] = repeat($output[$key], $repeat);
            } else {
    
                // Repeat key in the array: Parsing the repeat on the schema
                parseRepeat($repeat, $output[$key]);
    
                // Exit foreach after repeat parse is done
                break;
            }
    
        } 
    
        return $output;
    
    }   
    
    function parseRepeat($repeat, &$output) {
        foreach ($repeat as $title => $subject) {
    
                // Adding new repeat Item
                $repeatItem = $output['repeat'];
                array_walk_recursive($repeatItem, 'parse_fields', array($subject, $title));
    
                // Adding the item to output array
                $output[] = $repeatItem;
            }
    
            unset($output['repeat']);
    }
    
    function parse_fields(&$item, $key, $userdata)
    {
        switch ($item) {
            case 'subject':
                $item = $userdata[0];
                break;
            case 'title':
                $item = $userdata[1];
                break;
        }
    }
    
    
    
    // Output result
    echo '<pre>';
    print_r(repeat($schema, $repeat));
    

    【讨论】:

    • Ardi 非常感谢您的回答。但是我的数组不是恒定的。 $schema[0]['repeat'] 可能类似于 $schema[0][0]['repeat'];我找到了一个搜索密钥的功能,但我找不到替换的方法。你能做到吗? php.net/manual/en/function.array-search.php#109720
    • 我对代码进行了更改。现在无论您的数组结构如何,它都可以工作。
    【解决方案2】:

    我认为问题如下:

    'repeat' => array('tag' => 'div',array('tag' => 'span', 'style' => 'margin:10px; padding:10px', 'key' => 'title', )
    


    下面的数组是否在提到的一个键或值中?

    array( 
             'tag' => 'span',
             'style' => 'margin:10px; padding:10px',
             'key' => 'title',
         )
    

    【讨论】:

    • 我有 2 个变量(1 - 国家名称 2 - 美国)标题必须替换为国家名称,主题必须替换为美国
    【解决方案3】:

    可以将值替换为,

     // Recursive String Replace - recursive_array_replace(mixed, mixed, array);
    function recursive_array_replace($find, $replace, $array){
        if (!is_array($array)){
            return str_replace($find, $replace, $array);
        }
        $newArray = array();
        foreach ($array as $key => $value) {
            $newArray[$key] = recursive_array_replace($find, $replace, $value);
        }
        return $newArray;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-19
      • 1970-01-01
      • 2014-04-11
      • 2018-02-24
      • 1970-01-01
      • 2013-08-07
      • 2020-11-30
      • 2016-05-03
      相关资源
      最近更新 更多