gpt4 book ai didi

php - 不能将字符串偏移量用作数组 - 我需要在 foreach 中反序列化吗?

转载 作者:行者123 更新时间:2023-11-29 02:18:40 25 4
gpt4 key购买 nike

我在日志中收到此错误:

[Mon Feb 08 22:16:08 2016] [error] [client 2.125.15.96] PHP Fatal error: Cannot use string offset as an array in /home/ambcom/public_html/staging/beanbags/app/addons/abandoned_cart_extended/controllers/backend/ac.php on line 43, referer: /admin.php?dispatch=cart.cart_list

我认为是 $product['product_id']

当我在 $products 上执行 print_r 时,我得到这个控制台:http://pastebin.com/QuTJzMX8

这是我需要的购物车数据,我想如果我没记错的话是序列化的?我需要能够使用从查询中返回的数据并循环遍历项目。数据库字段中的数据存储为返回 a:2:{i:1217356819;a:24:{s:10:\"product_id\";i: etc...

PHP

$acId = $_REQUEST['id'];

$products = db_get_fields("SELECT cart FROM ?:abandoned_cart WHERE user_id = ?s", $acId);
fn_print_r($products);
//$products = unserialize($products);

if(count($products)>0){

$shippingCost = db_get_field("SELECT shipping FROM ?:abandoned_cart WHERE user_id = ?s", $acId);
$tax = db_get_field("SELECT tax FROM ?:abandoned_cart WHERE user_id = ?s", $acId);
$orderTotal = db_get_field("SELECT order_total FROM ?:abandoned_cart WHERE user_id = ?s", $acId);
$email = db_get_field("SELECT email FROM ?:abandoned_cart WHERE user_id = ?s", $acId);
$firstName = db_get_field("SELECT first_name FROM ?:abandoned_cart WHERE user_id = ?s", $acId);
$LastName = db_get_field("SELECT last_name FROM ?:abandoned_cart WHERE user_id = ?s", $acId);


$sum=0;
//echo $products;
if (!empty($products)) {

foreach ($products as $product) {
$text .='
<tr>
<td><a href="http://'.$_SERVER['SERVER_NAME'].'?dispatch=products.view&product_id='.$product['product_id'].'"> <img title="" height="120" width="120" alt="" src="'.$product['main_pair']['detailed']['image_path'].'"></a></td>
<td><a href="#" style=" font-weight:bold; color:#333; font-size:13px; text-decoration:none;">'.$product['product'].'</a><a href="#">&nbsp;<i></i></a><div style=" font-weight:bold; color:#333; font-size:12px; margin-top:4px; text-decoration:none;"> CODE: <span>'.$product['product_code'].'<!--product_code_update_2512012004--></span> </div></td>
<td style=" text-align:center;"><span style=" font-weight:bold; color:#333; font-size:12px; margin-top:4px; text-decoration:none;">$</span><span style=" font-weight:bold; color:#333; font-size:12px; margin-top:4px; text-decoration:none;">'.$product['price'].'</span> </td>
<td><div style="display: inline-block;vertical-align: top;width: 56px;"><input type="text" disabled value="'.$product['amount'].'" size="3" style="border:1px solid #c2c9d0; box-shadow:0 1px 3px rgba(0, 0, 0, 0.1) inset; border-radius:3px; float: left;height: 33px;text-align: center;width: 36px;"></div></td>
<td style="font-size:14px; font-weight:bold; color:#333; text-align:center; font-size:13px; text-decoration:none;"><span>$</span><span stye=" color:#000;">'.$product['price']*$product['amount'].'</span> </td>
</tr>';
$sum =$sum+$product['price']*$product['amount'];
}
}
}

我只是尝试了我在另一个 Stack 问题上看到的东西,但没有帮助,但是我为每个问题添加了 fn_print_r,如下所示,结果在控制台中,所以这可能会有所帮助。

$productsFetch = db_get_field("SELECT cart FROM ?:abandoned_cart WHERE user_id = ?s", $acId);
fn_print_r($productsFetch);

$productsArray = unserialize($productsFetch);
fn_print_r($productsArray);

$products = var_export($productsArray);
fn_print_r(var_export($productsArray));

第一个 print_r 是 a:2 的东西,第二个显示 false,第三个是 null

enter image description here

最佳答案

pastebin.com 上显示的$products 变量具有以下结构:

array(
array(
"serialised data"
)
)

因此,要获取内部数据,请在检查 $products 不为空后添加此语句:

$products = unserialize($products[0][0]);

如果我 print_r($products) 之后,我得到:

Array
(
[1217356819] => Array
(
[product_id] => 372
[product_code] => 16002
[product] => Acoustic Sofa - Eco Weave
[amount] => 1
[product_options] => Array
(
[365] => 732
)

[price] => 249
[stored_price] => N
[main_pair] => Array
(
[pair_id] => 417
[image_id] => 0
[detailed_id] => 1513
[position] => 0
[detailed] => Array
(
[image_path] => http://beanbags.ambientlounge.com/images/detailed/1/Acoustic-eco-weave.jpg?t=1454562784
[alt] =>
[image_x] => 1500
[image_y] => 1500
[http_image_path] => http://beanbags.ambientlounge.com/images/detailed/1/Acoustic-eco-weave.jpg?t=1454562784
[https_image_path] => https://beanbags.ambientlounge.com/images/detailed/1/Acoustic-eco-weave.jpg?t=1454562784
[absolute_path] => /home/ambcom/public_html/staging/beanbags/images/detailed/1/Acoustic-eco-weave.jpg
[relative_path] => detailed/1/Acoustic-eco-weave.jpg
)

)

[extra] => Array
(
[product_options] => Array
(
[365] => 732
)

[unlimited_download] => N
)

[stored_discount] => N
[company_id] => 1
[amount_total] => 1
[options_type] => P
[exceptions_type] => F
[modifiers_price] => 0
[is_edp] => N
[edp_shipping] => N
[discount] => 0
[promotions] => Array
(
)

[tax_summary] => Array
(
[included] => 22.64
[added] => 0
[total] => 22.64
)

[base_price] => 249
[category_ids] => Array
(
[0] => 16
)

[display_price] => 249
[taxes] => Array
(
[6] => Array
(
[rate_type] => P
[rate_value] => 10.000
[price_includes_tax] => Y
[regnumber] => 60499021423
[priority] => 0
[tax_subtotal] => 22.64
[description] => GST
)

)

)

[2512012004] => Array
(
[product_id] => 337
[product_code] => 8001
[product] => 300 Litres of PREMIUM Microbead filling
[amount] => 1
[product_options] => Array
(
)

[price] => 79
[stored_price] => N
[main_pair] => Array
(
[pair_id] => 1238
[image_id] => 0
[detailed_id] => 2334
[position] => 0
[detailed] => Array
(
[image_path] => http://beanbags.ambientlounge.com/images/detailed/2/300-filling-1.jpg?t=1402299012
[alt] =>
[image_x] => 1500
[image_y] => 1500
[http_image_path] => http://beanbags.ambientlounge.com/images/detailed/2/300-filling-1.jpg?t=1402299012
[https_image_path] => https://beanbags.ambientlounge.com/images/detailed/2/300-filling-1.jpg?t=1402299012
[absolute_path] => /home/ambcom/public_html/staging/beanbags/images/detailed/2/300-filling-1.jpg
[relative_path] => detailed/2/300-filling-1.jpg
)

)

[extra] => Array
(
[product_options] => Array
(
)

[unlimited_download] => N
)

[stored_discount] => N
[company_id] => 1
[amount_total] => 1
[options_type] => P
[exceptions_type] => F
[modifiers_price] => 0
[is_edp] => N
[edp_shipping] => N
[discount] => 0
[promotions] => Array
(
)

[tax_summary] => Array
(
[included] => 7.18
[added] => 0
[total] => 7.18
)

[base_price] => 79
[category_ids] => Array
(
[0] => 37
[1] => 32
)

[display_price] => 79
[taxes] => Array
(
[6] => Array
(
[rate_type] => P
[rate_value] => 10.000
[price_includes_tax] => Y
[regnumber] => 60499021423
[priority] => 0
[tax_subtotal] => 7.18
[description] => GST
)

)

)

)

然后继续 foreach 语句。

64/32 位问题:

我发现该代码适用于 64 位 PHP 安装,但不适用于 32 位 PHP 安装。 unserialize 失败,没有错误消息。

我发现原因出在上面数据中第二个顶级数组元素的键值中。该键值为 2512012004。在 32 位 PHP 安装上,PHP_INT_MAX 的值低于该值:2512012004,因此反序列化失败。

eval.in(32 位)上,我放置了使用您提供的数据的代码,但该代码对数据进行了一次替换,以使这个数字更小。然后它成功反序列化数据并打印结果数组。检查一下。

当然,您最终会得到一个错误的键值(如果这对您很重要)。

结论

看看您是否可以安装 64 位 PHP。

关于php - 不能将字符串偏移量用作数组 - 我需要在 foreach 中反序列化吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35280555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com