多维数组搜索和替换不起作用

时间:2013-08-25 17:13:11

标签: php arrays search multidimensional-array replace

我有一个识别我的$ 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
                )

        )

)

我的功能有什么问题?

3 个答案:

答案 0 :(得分:0)

我认为问题如下:

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


是上述一个键或值中的以下数组?

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

答案 1 :(得分:0)

可以用,

替换值
 // 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;
}

答案 2 :(得分:0)

我查看了你的代码并意识到需要更改它。我重写了它,这很有效。希望这会有所帮助: 对代码进行更改,它将适用于每个$ 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));