Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用WordPress post元数据函数的HTML表单中的关联数组_Php_Mysql_Wordpress_Wordpress Theming_Associative Array - Fatal编程技术网

Php 使用WordPress post元数据函数的HTML表单中的关联数组

Php 使用WordPress post元数据函数的HTML表单中的关联数组,php,mysql,wordpress,wordpress-theming,associative-array,Php,Mysql,Wordpress,Wordpress Theming,Associative Array,我有一个对象集合,需要在自定义post类型的wp_Posteta表中保存/检索 示例结构: array( array( 'firstname' => 'Johnny', 'middlename' => 'William' ), array( 'firstname' => 'Jane', 'middlename' => 'Alice' ) ) 我希望能够像这样迭

我有一个对象集合,需要在自定义post类型的wp_Posteta表中保存/检索

示例结构:

array(
    array( 
        'firstname'   => 'Johnny',
        'middlename'  => 'William'
    ),
    array( 
        'firstname'   => 'Jane',
        'middlename'  => 'Alice'
    )
)
我希望能够像这样迭代对象:

$children = get_post_meta( $postid, '_children', true);

$arrlength = count($children);
for($x = 0; $x < $arrlength; $x++)
{
    echo '<input type="text" name="_children[][firstname]" id="_children[][firstname]" value="' . $meta_values['_children'][0][$x][firstname] . '" /><br />';
    echo '<input type="text" name="_children[][middlename]" id="_children[][middlename]" value="' . $meta_values['children'][0][$x][middlename] . '" /><br />';
}
我知道上面的说法也不正确

这里的问题与上一个问题中的问题相同,但这次的最后一个参数应该是
false
。因为您正在读取/创建
数组
值和而不是
字符串

在代码中:

$children = get_post_meta( $postid, '_children', true);
您需要删除函数中的最后一个参数,因为默认值是
false

相反,您将拥有:

$children = get_post_meta( $postid, '_children');

$arrlength = count($children);
for($x = 0; $x < $arrlength; $x++)
{
    echo '<input type="text" name="_children[][firstname]" id="_children[][firstname]" value="' . $meta_values['_children'][0][$x][firstname] . '" /><br />';
    echo '<input type="text" name="_children[][middlename]" id="_children[][middlename]" value="' . $meta_values['children'][0][$x][middlename] . '" /><br />';
}
$children=get_post_meta($postid,'u children');
$arrlength=计数($children);
对于($x=0;$x<$arrlength;$x++)
{
回声“
”; 回声“
”; }

参考资料:


请尝试
$meta\u值[''u children'][$x][firstname]
只需删除
[0]
@伊斯梅尔博:我想知道[0]。数据库显示值为NULL,因此我认为我发布的代码的
post\u save
行中存在问题。
$\u post[''u children']
是数组吗?如何输入初始值!是的,要在
for
循环中输出表单,必须在DB中有一些值!我做错什么了吗?是的!我认为您保存post\u meta的代码是正确的!但在您的表单中,您需要将
name=“_children[][firstname]”更改为
name=“_children['.$x.][firstname]”和
name=“_children[][middlename]”更改为
name=“_children['.$x.][middlename]”以将
middlename
firstname
保存在同一数组中!我在
for
循环中看到您指定了一个额外的维度。您引用的
[0]
维度是将get\u post\u meta()最后一个参数设置为false的结果?谢谢你的解释和参考。
$children = get_post_meta( $postid, '_children');

$arrlength = count($children);
for($x = 0; $x < $arrlength; $x++)
{
    echo '<input type="text" name="_children[][firstname]" id="_children[][firstname]" value="' . $meta_values['_children'][0][$x][firstname] . '" /><br />';
    echo '<input type="text" name="_children[][middlename]" id="_children[][middlename]" value="' . $meta_values['children'][0][$x][middlename] . '" /><br />';
}