[英]Write serialized multidimensional array values to file with key path
我的问题是我如何才能成功爬网此序列化数据的所有级别并将每个叶级别写到文件中的单独字符串中,其中每一行都包含数组“键路径”和值。 基本上,我需要将每个值包装在i18n函数中以进行翻译。
我在mySQL数据库中有一些序列化的数据,这是一个有问题的值的示例:
stdClass Object
(
{...}
[fields] => Array
(
[0] => stdClass Object
(
{...}
[choices] => Array
(
[0] => stdClass Object
(
[text] => My string
[value] => 7
[isSelected] =>
[price] =>
)
) {...}
预期的结果是,每个叶子值都用这样的密钥层次结构写入到PHP文件中,因此我可以将其转换为数组:
$form['fields'][0]['choices'][0]['text'] = __( "My string", "tjxgallery" );
这是我的代码,试图做到这一点
$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( "/\n/", '', $value ) . '", "tjxgallery" );' . PHP_EOL;
}
我现在得到的结果是:$ form ['fields'] [0] ['text'] = __(“ My string”,“ tjxgallery”);
因此,它缺少['choices'][0]
部分。
任何帮助表示赞赏
谢谢你的时间
我在代码的各处添加了echo语句,以跟踪发生问题的位置。 我认为有两个问题:一个, $path
在内部循环中并不断重置,第二,每次$path
都被替换,而不是附加文本。
这对我有用:
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( "/\n/", '', $value ) . '", "tjxgallery" );' . "\n";
}
}
接受的答复可以使我受益匪浅,但是我仍然需要检查键是否为数字,因为它必须为[0]
而不是['0']
同样,仍然需要弄清楚为什么键的顺序不正确:
$form['fields'][0]['choices'][0]['text'] = __( "String 1", "tjxgallery" );
$form['fields'][1]['choices'][0]['text'] = __( "String 2", "tjxgallery" );
$form['fields'][2]['choices'][0]['text'] = __( "String 3", "tjxgallery" );
$form['fields'][3]['choices'][0]['text'] = __( "String 4", "tjxgallery" );
应为(请注意,数字键已交换:
$form['fields'][0]['choices'][0]['text'] = __( "String 1", "tjxgallery" );
$form['fields'][0]['choices'][1]['text'] = __( "String 2", "tjxgallery" );
$form['fields'][0]['choices'][2]['text'] = __( "String 3", "tjxgallery" );
$form['fields'][0]['choices'][3]['text'] = __( "String 4", "tjxgallery" );
这是需要更改的内容:
if ( is_numeric( $subkey ) ) {
$path = '[' . $subkey . ']' . $path;
} else {
$path = '[\'' . $subkey . '\']' . $path;
}
因此,反向添加键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.