Я получаю xml от curl, а затем я использую simplexml_load_string для получения значений. В этом я получаю все свои данные о продукте и общую сумму. И я получаю тип продукта из своей базы данных. Но я не знаю, как добавьте этот результат (который является объектом) в массив productdetail для каждого продукта. Я застрял здесь. Может ли любая помощь помочь?Как перенести объект в массив в php?
SimpleXMLElement Object
(
[PersonId] => 454545
[SyncedAt] => 2015-10-29T19:31:12
[TotalQuantity] => 3
[Tax] => 8.27
[Discount] => 0
[Total] => 91
[Contacts] => SimpleXMLElement Object
(
[Email] => SimpleXMLElement Object
(
)
[Phones] => SimpleXMLElement Object
(
[Mobile] => SimpleXMLElement Object
(
)
)
)
[ProductDetails] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Type] => Array
[TotalRows] => 2
)
[ProductDetail] => Array
(
[0] => SimpleXMLElement Object
(
[Id] => 1
[ProductId] => 2880
[ColourId] => 2880
[SkuId] => 2880
[ProductCode] => 11FD
[ProductName] => Badminton
[Quantity] => 2
[Price] => 23
[Discount] => 0
[TotalPrice] => 46
)
[1] => SimpleXMLElement Object
(
[Id] => 2
[ProductId] => 2005
[ColourId] => 2005
[SkuId] => 2005
[ProductCode] => 55OK
[ProductName] => GLOVES
[Quantity] => 1
[Price] => 45
[Discount] => 0
[TotalPrice] => 45
)
)
)
[DeliveryOption] => SimpleXMLElement Object
(
[Id] => 522
[Name] => DeliveryCharges
[Value] => 0
)
[DeliveryOptions] => SimpleXMLElement Object
(
[DeliveryType] => SimpleXMLElement Object
(
[Id] => 522
[Name] => DeliveryCharges
[Value] => 0
)
)
[TaxAdjustment] => 0
)
Мой контроллер:
$xml = simplexml_load_string($output);
$cartdetail_arr=array();
$data['total'] = $xml->Total;
foreach ($xml->ProductDetails->ProductDetail as $curr_detail) {
//$cartdetail_arr[] = (array)$curr_detail;
$style = $curr_detail->ProductCode;
$prod_type = $this->cart_model->get_prod_type($style);
//print_r($prod_type);exit;
}
$data['cart_detail']= $cartdetail_arr;
Моя модель:
public function get_prod_type($style){
$sql = "SELECT product_type from product_master where style='$style'";
$query = $this->db->query($sql);
if($query->num_rows() == 0):
return false;
else:
return $query->row();
endif;
}
Выход из запроса:
stdClass Object ([prod_type] => Sports)
Желаемый результат:
Array
(
[0] => Array
(
[Id] => 1
[ProductId] => 2880
[ColourId] => 2880
[SkuId] => 2880
[ProductCode] => 11FD
[ProductName] => Badminton
[Quantity] => 2
[Price] => 23
[Discount] => 0
[TotalPrice] => 46
[prod_type] => School
)
[1] => Array
(
[Id] => 2
[ProductId] => 2005
[ColourId] => 2005
[SkuId] => 2005
[ProductCode] => 55OK
[ProductName] => GLOVES
[Quantity] => 1
[Price] => 45
[Discount] => 0
[TotalPrice] => 45
[prod_type] => Sports
)
)
Я попытался это, но я не получаю желаемый результат:
foreach ($xml->ProductDetails->ProductDetail as $curr_detail) {
$cartdetail_arr[] = (array)$curr_detail;
$style = $curr_detail->ProductCode;
$prod_type = new stdClass();
$prod_type = $this->cart_model->get_prod_type($style);
$cartdetail_arr[] = clone $prod_type;
}
И получаю это как выход
Array
(
[0] => Array
(
[Id] => 1
[ProductId] => 2880
[ColourId] => 2880
[SkuId] => 2880
[ProductCode] => 11FD
[ProductName] => Badminton
[Quantity] => 2
[Price] => 23
[Discount] => 0
[TotalPrice] => 46
)
[1] => stdClass Object
(
[prod_type] => School
)
[2] => Array
(
[Id] => 2
[ProductId] => 2005
[ColourId] => 2005
[SkuId] => 2005
[ProductCode] => 55OK
[ProductName] => GLOVES
[Quantity] => 1
[Price] => 45
[Discount] => 0
[TotalPrice] => 45
)
[3] => stdClass Object
(
[prod_type] => Sports
)
)
Нет никого, кто мог бы мне помочь? .. – piya