Link to home
Start Free TrialLog in
Avatar of Robert Granlund
Robert GranlundFlag for United States of America

asked on

PHP Array in Select return

I have a query that has an array in the return.  I'm not sure how to get at the items in the array and create variables from them.
My Query:
<?php
$open_order_id = $this->db->select('*')
					->where('member_id', $member_id)
					->where('status', 'open')
					->get('exp_cartthrob_subscriptions');
						
				$open_orders = $open_order_id->result_array();
				
				$open_order_id->free_result();
			//echo '<pre>';
			//var_dump($open_order_id);
			//echo '</pre>';
				
				foreach($open_orders as $row)	{

			$extra = _unserialize($row['serialized_item'], TRUE);
			
			// $keep_extra seems to be undefined, I'm going to set it to false up above so it writes out the row[$keys]
			if ($keep_extra)
			{
				$row['extra'] = $extra;
			}
			else
			{
				foreach ($extra as $key => $value)
				{
					if ( ! isset($row[$key]))
					{
						$row[$key] = $value;
					}
				}
				unset($row['serialized_item']);			
			}
echo '<pre>';
var_dump($extra);
echo '</pre>';
}
?>

Open in new window


From The var_dump I get the following:
array(17) {
  ["row_id"]=>
  int(0)
  ["quantity"]=>
  float(1)
  ["product_id"]=>
  string(2) "29"
  ["site_id"]=>
  string(1) "1"
  ["shipping"]=>
  int(0)
  ["weight"]=>
  string(0) ""
  ["price"]=>
  float(317)
  ["no_tax"]=>
  bool(true)
  ["no_shipping"]=>
  bool(true)
  ["item_options"]=>
  array(45) {
    ["insured_first_name"]=>
    string(10) "First Name"
    ["insured_last_name"]=>
    string(9) "Last Name"
    ["location_address"]=>
    string(16) "123 Test Address"
    ["location_city"]=>
    string(9) "Test City"
    ["location_state"]=>
    string(2) "AR"
    ["location_zip"]=>
    string(5) "65401"
    ["location_phone"]=>

Open in new window


I want to know how I get at the items_options array /     ["insured_first_name"]=>
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Do you mean this?

echo $extra['items_options']["insured_first_name"];

Open in new window

In php you have foreach for getting elements of an array one by one:
http://php.net/manual/en/control-structures.foreach.php

I think you should just use this:

$items_options = $row['item_options'];
foreach ($items_options as $item_option){
      foreach ($item_option as $key => $value)
      {
            if ( ! isset($row[$key]))
            {
                  $myoptions[$key] = $value;
            }
      }
}

Is that what you want?
Avatar of Robert Granlund

ASKER

I wrote it like this but this does not work.  Maybe you can elaborate a little more?
<?php
$open_order_id = $this->db->select('*')
					->where('member_id', $member_id)
					->where('status', 'open')
					->get('exp_cartthrob_subscriptions');
						
				$open_orders = $open_order_id->result_array();
				
				$open_order_id->free_result();
			//echo '<pre>';
			//var_dump($open_order_id);
			//echo '</pre>';
				
			foreach($open_orders as $row)	{

			$extra = _unserialize($row['serialized_item'], TRUE);
			
			// $keep_extra seems to be undefined, I'm going to set it to false up above so it writes out the row[$keys]
			if ($keep_extra)
			{
				$row['extra'] = $extra;
			}
			else
			{
				foreach ($extra as $key => $value)
				{
					if ( ! isset($row[$key]))
					{
						$row[$key] = $value;
					}
				}
				unset($row['serialized_item']);
			$id = $extra['items_options']["insured_first_name"];				
			}
			
			echo '<pre>';
			var_dump($id);
			echo '</pre>';		
	
				}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial