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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里