将数据解析为序列化数组会产生标量值警告和致命的字符串偏移量


Parsing data to a serialized array produces scalar value warnings and fatal string offset

我正试图将数据解析成一个序列化数组,但无法解析,结果出错了。我想做的是利用update_post_meta在序列化数组中填充将被解析的值

阵列的示例输出:

get_post_meta('840', '_eshop_product',true ); 

(
[sku] => 1234567885 
[products] => 
Array 
( 
    [1] => Array 
    ( 
        [option] => Retail 
        [price] => 3.99 
        [tax] => 1 
        [saleprice] =>
     )
 ) 
[description] => Astonishing X-men 
[shiprate] => A 
[featured] => Yes 
[sale] => yes 
[cart_radio] => 0 
[optset] => 
)

对于参考资料,我所能找到的只是get_post_meta的示例,我正试图利用update_post_mata将新数据解析到这些字段中。这是代码:

$meta2 = update_post_meta($post_id, '_eshop_product', true);
$meta3 = update_post_meta;
$meta3($post_id, '_eshop_stock', true); //works
$meta2['sku']=htmlspecialchars($_POST['eshop_sku']); //this is line 90
if($meta2['sku']=''){update_post_meta($meta2['_sku'], "{$id}");} //this is line 91
$numoptions=$eshopoptions['options_num'];
for($i=1;$i<=$numoptions;$i++)
{
    $meta2['products'][$i]['option']=htmlspecialchars($_POST['eshop_option_'.$i]); //this is line 96
    if($_POST['eshop_price_'.$i]='0'){update_post_meta($meta2($_POST['eshop_price_'.$i]), "{pricing_high}");}
    if($_POST['eshop_tax_'.$i]='No'){update_post_meta($meta2($_POST['eshop_tax_'.$i]), 'band 1');} //this is line 99
}
$meta2['description']=htmlspecialchars($_POST['eshop_product_description']);
if($meta2['description']==''){update_post_meta($meta2['description'], 'singles');}
$meta2['shiprate']=$_POST['eshop_shipping_rate'];
if($meta2['shiprate']='F'){update_post_data($meta2['_Shipping Rate'], 'A');}
if($_POST['eshop_sale_product']=='No'){update_post_meta( $id, '_eshop_sale', 'yes');}

这些是我得到的错误:

Warning: Cannot use a scalar value as an array in /home/***/public_html/wp-content/themes/***/mtgpage.php on line 90
Warning: Cannot use a scalar value as an array in /home/***/public_html/wp-content/themes/***/mtgpage.php on line 91
Warning: Cannot use a scalar value as an array in /home/***/public_html/wp-content/themes/***/mtgpage.php on line 96
Fatal error: Function name must be a string in /home/***/public_html/wp-content/themes/***/mtgpage.php on line 99

当我在wordpress中编辑产品时,自定义数据将位于编辑器下-出现错误,自定义字段将不会显示:

Fatal error: Cannot use string offset as an array in /home/***/public_html/wp-content/plugins/eshop/eshop-product-entry.php on line 44

那里有很多错误。前3个错误是关于函数update_post_meta(),它没有返回数组。所以$meta2不是一个数组,你不能这样对待它。因此,请检查该函数并确保它返回的是一个数组。

每一个如果你的脚本,将返回真。例如,if($meta2['shiprate']='F')始终为true,因为您将$meta2['shiprate']=分配给字符串,并且评估的测试为if ('F'),这是true。您可能想要测试$meta2['shiprate']的值,那么您必须像这样使用==if($meta2['shiprate']=='F')

对于最后一个错误,我不完全理解您试图在这里激活什么:$meta2($_POST['eshop_tax_'.$i]),但它不是对附加到字符串函数名的变量中的变量的有效使用。你可能想做$meta2[$_POST['eshop_tax_'.$i]]