dongxu7121 2014-08-26 14:39
浏览 54
已采纳

使用密钥路径将序列化的多维数组值写入文件

My question is how can I successfully crawl all levels of this serialized data and write each leaf level to separate strings in a file, where each line contains the array "key path" and the value. Basically, I need each value wrapped in a i18n function for translation purposes.

I have some serialized data in a mySQL database, here's a sample of a problematic value:

stdClass Object
(
 {...}
 [fields] => Array
    (
        [0] => stdClass Object
            (
            {...}
            [choices] => Array
                    (
                        [0] => stdClass Object
                            (
                                [text] => My string
                                [value] => 7
                                [isSelected] => 
                                [price] => 
                            )
     ) {...}

The expected result is each leaf value written to a PHP file with it's key hierearchy like this, so I can then reconvert it to an array:

$form['fields'][0]['choices'][0]['text'] = __( "My string", "tjxgallery" );

And here is my code that attempts to do that

$iterator = new RecursiveIteratorIterator( new RecursiveArrayIterator( $form_fields ) );

$strings_to_translate = array(
    '<?php' . PHP_EOL
);

foreach ( $iterator as $key => $value ) {

    // Fields to skip, as they don't contain any translatable strings
    $unwanted_fields = array(
        'inputName',
        'type',
        'size',
        'inputType',
        'descriptionPlacement',
        'postCustomFieldName',
        'allowedExtensions',
        'actionType',
        'operator',
        'logicType',
        'conditionalLogic',
    );

    // Only proceed if array item is a string and it's not empty and it's not a number and it's not in the ignored fields
    if ( ! in_array( $key, $unwanted_fields ) && ( is_string( $value ) && ( 0 < strlen( $value ) ) &&  ! is_numeric( $value ) ) ) {

        // Iterate through the sub arrays
        for ( $i = $iterator->getDepth() - 1; $i >= 0; $i -- ) {

            $path = '';

            // get the parent key of current item
            $subkey = $iterator->getSubIterator( $i )->key();

            // Build a string with the full key path - e.g. [0]['choices'][0]['text']
            if ( is_numeric( $subkey ) ) {

                if ( empty( $path ) ) {

                    $path = '[' . $subkey . '][\'' . $key;

                } else {

                    $path = '[' . $subkey . ']' . $key;

                }

            } else {

                if ( empty( $path ) ) {

                    $path = '[\'' . $subkey . '\'][\'' . $key;

                } else {

                    $path = '[\'' . $subkey . '\']' . $key;

                }

            }

        }

        // Build an array of translation ready strings e.g. $form['fields'][0]['text'] = __( "Give Up Clothes For Good – Cancer Research UK", "tjxgallery" );
        $strings_to_translate[] = '$form[\'fields\']' . $path . '\'] = __( "' . preg_replace( "/
/", '', $value ) . '", "tjxgallery" );' . PHP_EOL;

    }

The result I get now is this: $form['fields'][0]['text'] = __( "My string", "tjxgallery" );

So , it's missing the ['choices'][0] part.

Any help appreciated

thanks for your time

  • 写回答

2条回答 默认 最新

  • douxuanpa8298 2014-08-26 15:20
    关注

    I added echo statements all over the place in your code to track down where the problem was occurring. I think there were two issues: one, $path is in an inner loop and keeps getting reset, and two, $path was being replaced each time, rather than having text appended to it.

    This is what worked for me:

    foreach ( $iterator as $key => $value ) {
    
        // Fields to skip, as they don't contain any translatable strings
        $unwanted_fields = array(
            ...
        );
        // Only proceed if array item is a string and it's not empty
        // and it's not a number and it's not in the ignored fields
        if ( ! in_array( $key, $unwanted_fields ) && ( is_string( $value ) && ( 0 < strlen( $value ) ) &&  ! is_numeric( $value ) ) ) {
    
            // start a new path from $key here.
            $path = '';
    
            // Iterate through the sub arrays
            for ( $i = $iterator->getDepth() - 1; $i >= 0; $i -- ) {
    
                // get the parent key of current item
                $subkey = $iterator->getSubIterator( $i )->key();
                // Build a string with the full key path - e.g. [0]['choices'][0]['text']
                $path .= '[\'' . $subkey . '\']';
            }
            // OK, we've constructed the path. Add $key to the end.
            $path .= '[\'' . $key . '\']';
    
            // Build an array of translation ready strings e.g. $form['fields'][0]['text'] = __( "Give Up Clothes For Good – Cancer Research UK", "tjxgallery" );
            echo '$form[\'fields\']' . $path . ' = __( "' . preg_replace( "/
    /", '', $value ) . '", "tjxgallery" );' . "
    ";
    
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有人会SIRIUS 5.8.0这个软件吗
  • ¥30 comsol仿真等离激元
  • ¥15 静电纺丝煅烧后如何得到柔性纤维
  • ¥15 (标签-react native|关键词-镜像源)
  • ¥100 照片生成3D人脸视频
  • ¥15 伪装视频时长问题修改MP4的时长问题,
  • ¥15 JETSON NANO
  • ¥15 VS开发qt时如何在paintgl函数中用pushbutton控制切换纹理
  • ¥20 关于 openpyxl 处理excel文件地问题
  • ¥15 MS中不知道高分子的构型怎么构建模型