Как поместить объект в массив в php?

Я получаю XML от curl, а затем я использую 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
)

)

2

Решение

Вы можете просто попробовать, как это ..

$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;

Надеюсь, это будет полезно.

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;
0

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector