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 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭