简体   繁体   中英

How could I get each value of an array?

I have an array like this :

array(3) {
    ["data"]=> array(1) {
        ["mesq_G3SC"]=> array(1) {
            ["data"]=> object(stdClass)#30 (19) {
                ["cart_id"]=> string(13) "mesq_G3SC"
                ["qty"]=> string(4) "2.00"
                ["price"]=> string(5) "11400"
                ["product_id"]=> string(1) "6"
                ["member_id"]=> string(1) "5"
                ["session_id"]=> string(32) "4dedde2a2eb12b25e940b7051a5f65a1"
                ["date_added"]=> string(19) "2016-09-28 10:39:06"
                ["date_modified"]=> string(19) "2016-09-28 10:39:06" 
                ["status"]=> string(7) "pending"
                ["category_id"]=> string(1) "2" 
                ["location"]=> string(9) "England"
                ["square"]=> string(3) "300"
                ["stock"]=> string(3) "260"
                ["folder_name"]=> string(23) "assets/uploads/products"
                ["photo1"]=> string(11) "photo1.jpg"
                ["photo2"]=> string(11) "pc.jpg"
                ["category_name"]=> string(6) "PC"
                ["is_published"]=> string(1) "1"
                ["modified_by"]=> NULL
            }
        }
    }
    ["total_item"]=> int(1)
    ["total_price"]=> string(8) "11400.00"
}

I would to get each value of that array with foreach but I got error

Trying to get property of non-object...

I developed this with Codeigniter and this is my foreach :

foreach ($data as $row) {
  echo $row->product_id;
}

This is what I get if I do print_r($row) :

Array ( [mesq_G3SC] => Array ( [data] => stdClass Object ( [cart_id] => mesq_G3SC [qty] => 2.00 [price] => 11400 [product_id] => 6 [member_id] => 5 [session_id] => 4dedde2a2eb12b25e940b7051a5f65a1 [date_added] => 2016-09-28 10:39:06 [date_modified] => 2016-09-28 10:39:06 [status] => pending [category_id] => 2 [lokasi_lahan] => England [square] => 300 [stock] => 260 [folder_name] => assets/uploads/products [photo1] => photo1.jpg [photo2] => photo2.jpg [category_name] => PC [is_published] => 1 [modified_by] => ) ) ) 1 11400.00

Anyone knows how to get each value of that array?

if mesq_G3SC is the key you want to iterate over - i suggest you do the following

$obj = new stdClass();
$obj->cart_id = "mesq_G3SC";
$obj->qty = "2.00";
$obj->price = "11400";
$obj->product_id = "6";
$obj->member_id = "5";
$obj->session_id = "4dedde2a2eb12b25e940b7051a5f65a1";
$obj->date_added = "2016-09-28 10:39:06";
$obj->date_modified = "2016-09-28 10:39:06";
$obj->status = "pending";
$obj->category_id = "2";
$obj->location = "England";
$obj->square = "300";
$obj->stock = "260";
$obj->folder_name = "assets/uploads/products";
$obj->photo1 = "photo1.jpg";
$obj->photo2 = "pc.jpg";
$obj->category_name = "PC";
$obj->is_published = "1";
$obj->modified_by = NULL;


$data = array(
    "mesq_G3SC"=> array(
        "data"=> $obj
    ),
    "total_item" =>  1,
    "total_price" => "11400.00"
);

foreach($data['mesq_G3SC'] AS $key => $obj)
{
    echo $key;
    print_r($obj);
}

Update: i simulated your array - and it works like a charm - there is something wrong with the keys - instead of var_dump try to print_r your data array and show us the output pls

I re-created your multidimensional array, if you want to get the deepest values, here it is:

$deepest_values = array();
    foreach($arr_ as $first_level_key => $first_level_value) {
        if(is_array($first_level_value)) {
             foreach($first_level_value as $second_level_key => $second_level_value) {
                if(is_array($second_level_value)) {
                    foreach($second_level_value as $third_level_key => $third_level_value) {
                        if(is_array($third_level_value)) {
                            foreach($third_level_value as $fourth_level_key => $fourth_level_value) {
                                $deepest_values[$fourth_level_key] = $fourth_level_value;
                            }
                        }

                    }
                }
             }
         }
    }

echo '<pre>';
print_r($deepest_values);
echo '</pre>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM