我从curl获取xml,然后我使用simplexml_load_string来获取值。我得到了所有产品详细信息和购物车总数。我从我的数据库中获取产品类型。但我不知道知道如何在productdetail数组中为每个产品添加结果(这是一个对象)。我被困在这里。可以帮忙吗?
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
)
)
答案 0 :(得分:0)
只需在控制器中设置为对象属性:
$xml = simplexml_load_string($output);
$cartdetail_arr=array();
$data['total'] = $xml->Total;
foreach ($xml->ProductDetails->ProductDetail as $curr_detail) {
$style = $curr_detail->ProductCode;
// You are using just one property from the returning result
$curr_detail->prod_type = $this->cart_model->get_prod_type($style)->prod_type;
$cartdetail_arr[] = $curr_detail;
}
$data['cart_detail']= $cartdetail_arr;
答案 1 :(得分:0)
你可以这样试试..
$xml = simplexml_load_string($output);
$cartdetail_arr=array();
$data['total'] = $xml->Total;
foreach ($xml->ProductDetails->ProductDetail as $curr_detail) {
$temp = (array) $curr_detail;
$style = $curr_detail->ProductCode;
$temp["prod_type"] = $this->cart_model->get_prod_type($style)->prod_type;
$cartdetail_arr[] = $temp;
}
$data['cart_detail']= $cartdetail_arr;
希望它会有用。