0

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

        )

)

我的功能有什么问题?

4

3 回答 3

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;
}
于 2013-08-26T10:01:30.300 回答
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));
于 2013-08-26T11:23:28.180 回答
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',
     )
于 2013-08-25T17:23:37.043 回答