1

My DB returns an array of objects with arrays within.

Var_dump

array (size=2)
  0 => 
    object(stdClass)[22]
      public 'customer_id' => string '10' (length=2)
      public 'cart' => string 'a:1:{s:32:"f9bb1d342b1c2a0bfe982ef405369ec0";a:9:{s:5:"rowid";s:32:"f9bb1d342b1c2a0bfe982ef405369ec0";s:2:"id";s:9:"101_30524";s:3:"qty";s:1:"1";s:5:"price";s:5:"104.5";s:4:"name";s:13:"Business Card";s:5:"image";s:18:"business-cards.gif";s:4:"ship";a:3:{s:6:"Ground";d:9.730000000000000426325641456060111522674560546875;s:11:"2nd Day Air";d:18.53999999999999914734871708787977695465087890625;s:9:"Overnight";d:26.269999999999999573674358543939888477325439453125;}s:7:"options";a:2:{s:17:"Print Description";s:16'... (length=761)
      public 'shipping_type' => string 'Ground' (length=6)
      public 'shipping_cost' => string '9.73' (length=4)
      public 'order_sub_total' => string '104.50' (length=6)
      public 'order_total' => string '114.23' (length=6)
      public 'id' => string '28' (length=2)
      public 'timestamp' => string '2012-10-12 20:10:30' (length=19)
  1 => 
    object(stdClass)[23]
      public 'customer_id' => string '10' (length=2)
      public 'cart' => string 'a:2:{s:32:"22d2d3d8584f6e0819c4e46af4d2fda2";a:9:{s:5:"rowid";s:32:"22d2d3d8584f6e0819c4e46af4d2fda2";s:2:"id";s:9:"101_94980";s:3:"qty";s:1:"1";s:5:"price";s:2:"64";s:4:"name";s:13:"Business Card";s:5:"image";s:18:"business-cards.gif";s:4:"ship";a:3:{s:6:"Ground";d:9.730000000000000426325641456060111522674560546875;s:11:"2nd Day Air";d:18.53999999999999914734871708787977695465087890625;s:9:"Overnight";d:26.269999999999999573674358543939888477325439453125;}s:7:"options";a:2:{s:17:"Print Description";s:164:"'... (length=1506)
      public 'shipping_type' => string 'Ground' (length=6)
      public 'shipping_cost' => string '19.46' (length=5)
      public 'order_sub_total' => string '148.25' (length=6)
      public 'order_total' => string '167.71' (length=6)
      public 'id' => string '29' (length=2)
      public 'timestamp' => string '2012-10-12 20:29:10' (length=19)

Notice cart is a multidimensional array. How do I loop through these objects and arrays and create a table?

<?php foreach($all_orders as $key => $val) : ?>

    <?php echo $key; ?>  <?php echo $val; ?>

<?php endforeach; ?>

This causes the following error: A PHP Error was encountered Severity: 4096 Message: Object of class stdClass could not be converted to string

4 Answers 4

2

Access the properties of the stdClass object using the -> operator:

<?php foreach($all_orders as $key => $val) : ?>
Customer ID <?php echo $val->customer_id ?> has a total of <?php echo $val->order_total ?><br />
<?php endforeach ?>
Sign up to request clarification or add additional context in comments.

9 Comments

Hey that works great ... except how to echo out cart? Cart is a serialized array. <?php echo unserialize($val->cart); ?> It throws an error * Array to string conversion *
It looks like you would have to do something like: $cart = unserialize($val->cart); $key = array_keys($cart)[0]; // PHP 5.4+ echo $cart[$key]['price'];
Reason being it appears the cart array is keyed by a hash code so you can't do $cart[0]
That's causing Undefined offset: 0 error and the following: ` $key = array_keys($cart)[0];` causes a SYNTAX error
Ok check this out if i do echo $cart['f9bb1d342b1c2a0bfe982ef405369ec0']['price']; I get the right output. So $key is not passing the right index So how do I get the correct index from the foreach loop? thanks for your help btw! :)
|
2

You can try

echo "<pre>";
foreach ( $cart as  $all_orders ) {
    foreach ( $all_orders as $key => $value ) {
        echo $key, " = ", $value, PHP_EOL;
    }
}

4 Comments

That kinda/sorta of works. I will play with this and get back to you. Thank you very much.
if you're still up please take a look at the comments between drew010 and I. Please advise if you know what I need to do. Thanks!
@fabio can see your answer is already selected ... need to go to bed now .. good day ..
yea just got it. Thank you for looking. Enjoy your evening. Thanks again!
0

PHP 5 allows one to iterate over an object's public properties using a foreach loop.

For more information (and code examples): http://php.net/manual/en/language.oop5.iterations.php

Comments

0

I use PHP's ArrayObject in my MVC:
us3.php.net/manual/en/class.arrayobject.php

        $arrayobject = new ArrayObject($dataRows);
        for ($iterator = $arrayobject->getIterator(); $iterator->valid(); $iterator->next()) {

        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.