Cannot use string offset as an array - do I need to unserialize in foreach?
<p>I am getting this error in logs:</p>

<blockquote>
  <p>[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</p>
</blockquote>

<p>Which I think is $product['product_id']</p>

<p>When I do a print_r on the $products I get this this console: <a href="http://pastebin.com/QuTJzMX8" rel="nofollow noreferrer">http://pastebin.com/QuTJzMX8</a></p>

<p>It's the cart data I need which I think if I'm not mistaken is serialized? I need to be able to use the data I get back from the query and loop through the items. The data in the database field is stored like it returns a:2:{i:1217356819;a:24:{s:10:\"product_id\";i: etc...</p>

<p>PHP</p>

<pre><code>$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)&gt;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 .='
        &lt;tr&gt;
          &lt;td&gt;&lt;a  href="http://'.$_SERVER['SERVER_NAME'].'?dispatch=products.view&amp;product_id='.$product['product_id'].'"&gt; &lt;img title="" height="120" width="120" alt="" src="'.$product['main_pair']['detailed']['image_path'].'"&gt;&lt;/a&gt;&lt;/td&gt;
          &lt;td&gt;&lt;a href="#" style=" font-weight:bold; color:#333; font-size:13px; text-decoration:none;"&gt;'.$product['product'].'&lt;/a&gt;&lt;a href="#"&gt;&amp;nbsp;&lt;i&gt;&lt;/i&gt;&lt;/a&gt;&lt;div style=" font-weight:bold; color:#333; font-size:12px; margin-top:4px; text-decoration:none;"&gt; CODE: &lt;span&gt;'.$product['product_code'].'&lt;!--product_code_update_2512012004--&gt;&lt;/span&gt; &lt;/div&gt;&lt;/td&gt;
          &lt;td style=" text-align:center;"&gt;&lt;span style=" font-weight:bold; color:#333; font-size:12px; margin-top:4px; text-decoration:none;"&gt;$&lt;/span&gt;&lt;span style=" font-weight:bold; color:#333; font-size:12px; margin-top:4px; text-decoration:none;"&gt;'.$product['price'].'&lt;/span&gt; &lt;/td&gt;
          &lt;td&gt;&lt;div style="display: inline-block;vertical-align: top;width: 56px;"&gt;&lt;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;"&gt;&lt;/div&gt;&lt;/td&gt;
          &lt;td style="font-size:14px;  font-weight:bold; color:#333; text-align:center; font-size:13px; text-decoration:none;"&gt;&lt;span&gt;$&lt;/span&gt;&lt;span stye=" color:#000;"&gt;'.$product['price']*$product['amount'].'&lt;/span&gt; &lt;/td&gt;
        &lt;/tr&gt;';
      $sum =$sum+$product['price']*$product['amount'];
    }
  }
} 
</code></pre>

<p>I just tried something I saw on another Stack question but did not help, however I added <code>fn_print_r</code> for each as you see below and the result in console, so maybe this might help.</p>

<pre><code>$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));
</code></pre>

<p>1st print_r is the a:2 stuff, 2nd shows false and 3rd is null</p>

<p><a href="https://i.stack.imgur.com/fHXnf.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fHXnf.jpg" alt="enter image description here"></a></p>