Php foreach正在返回空
我对我正在执行的循环有点怀疑,因为它返回的是空值 首先,我收到的物品如下:Php foreach正在返回空,php,laravel,foreach,Php,Laravel,Foreach,我对我正在执行的循环有点怀疑,因为它返回的是空值 首先,我收到的物品如下: $items = Session::get('items'); $deco = json_decode($items, true); 如果我返回$deco,我会得到以下结果: { "items":[ { "id":1, "inventoryID":1, "title":"Product 1", "quantity":1, "u
$items = Session::get('items');
$deco = json_decode($items, true);
如果我返回$deco
,我会得到以下结果:
{
"items":[
{
"id":1,
"inventoryID":1,
"title":"Product 1",
"quantity":1,
"unit_price":20,
"image":"img.png"
},
{
"id":2,
"inventoryID":1,
"title":"Product2",
"quantity":1,
"unit_price":25,
"image":"img.png"
}
]
}
现在,在循环中(我使用它来更改paypal集成的一些键的值),我有以下内容:
$results = [];
foreach($deco['items'] as $element) {
$name=$element['title'];
$quantity=$element['quantity'];
$sku= $element['id'];
$price=$element['unit_price'];
$item = new Item();
$item -> setName($name)
->setCurrency('USD')
->setQuantity($quantity)
->setSku($sku)
->setPrice($price);
$results[]=$item;
}
如果我返回return$item
(在循环之外),我只得到一个值,而不是所有集合:
{
"name": "Product2",
"currency": "USD",
"quantity": 1,
"sku": 2,
"price": "25"
}
但是,如果我返回所需的变量return$results
,它会给我这个(它根据需要执行{}项,但返回空):
整个代码看起来像:
public function test(){
$items = Session::get('items');
$deco = json_decode($items, true);
$results = [];
foreach($deco['items'] as $element) {
$name=$element['title'];
$quantity=$element['quantity'];
$sku= $element['id'];
$price=$element['unit_price'];
$item = new Item();
$item -> setName($name)
->setCurrency('USD')
->setQuantity($quantity)
->setSku($sku)
->setPrice($price);
$results[]=$item;
}
return $results;
}
编辑:位于\vendor\paypal\restapi sdk php\lib\paypal\api
的Item.php
模型是默认的paypal:
class Item extends PayPalModel
{
/**
* Stock keeping unit corresponding (SKU) to item.
*
* @param string $sku
*
* @return $this
*/
public function setSku($sku)
{
$this->sku = $sku;
return $this;
}
/**
* Stock keeping unit corresponding (SKU) to item.
*
* @return string
*/
public function getSku()
{
return $this->sku;
}
/**
* Item name. 127 characters max.
*
* @param string $name
*
* @return $this
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Item name. 127 characters max.
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Description of the item. Only supported when the `payment_method` is set to `paypal`.
*
* @param string $description
*
* @return $this
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Description of the item. Only supported when the `payment_method` is set to `paypal`.
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Number of a particular item. 10 characters max.
*
* @param string $quantity
*
* @return $this
*/
public function setQuantity($quantity)
{
$this->quantity = $quantity;
return $this;
}
/**
* Number of a particular item. 10 characters max.
*
* @return string
*/
public function getQuantity()
{
return $this->quantity;
}
/**
* Item cost. 10 characters max.
*
* @param string|double $price
*
* @return $this
*/
public function setPrice($price)
{
NumericValidator::validate($price, "Price");
$price = FormatConverter::formatToPrice($price, $this->getCurrency());
$this->price = $price;
return $this;
}
/**
* Item cost. 10 characters max.
*
* @return string
*/
public function getPrice()
{
return $this->price;
}
/**
* 3-letter [currency code](https://developer.paypal.com/docs/integration/direct/rest_api_payment_country_currency_support/).
*
* @param string $currency
*
* @return $this
*/
public function setCurrency($currency)
{
$this->currency = $currency;
return $this;
}
/**
* 3-letter [currency code](https://developer.paypal.com/docs/integration/direct/rest_api_payment_country_currency_support/).
*
* @return string
*/
public function getCurrency()
{
return $this->currency;
}
//More similar stuff
提前感谢您这仅仅是因为JMS序列化。 我猜在控制器级别,您可能需要检查是否应用了任何序列化组。答案(感谢Ionica on discord的laravel社区):
如果我返回$item(在循环之外)
@CaddyDZ,会让我觉得这里有一些{
}
不匹配或发生了什么事情……我认为item
是一个脏模型,没有保存,请在$results[]之前尝试运行dd($item)
=$item
查看$item
是否为空对象如何检查返回结果?在我看来,你在使用json_编码之类的东西。考虑使用VARYDUMP来检查返回结果。您可以同时发布模型类吗?是否在模型中隐藏了带有$hidden属性的字段?
class Item extends PayPalModel
{
/**
* Stock keeping unit corresponding (SKU) to item.
*
* @param string $sku
*
* @return $this
*/
public function setSku($sku)
{
$this->sku = $sku;
return $this;
}
/**
* Stock keeping unit corresponding (SKU) to item.
*
* @return string
*/
public function getSku()
{
return $this->sku;
}
/**
* Item name. 127 characters max.
*
* @param string $name
*
* @return $this
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Item name. 127 characters max.
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Description of the item. Only supported when the `payment_method` is set to `paypal`.
*
* @param string $description
*
* @return $this
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Description of the item. Only supported when the `payment_method` is set to `paypal`.
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Number of a particular item. 10 characters max.
*
* @param string $quantity
*
* @return $this
*/
public function setQuantity($quantity)
{
$this->quantity = $quantity;
return $this;
}
/**
* Number of a particular item. 10 characters max.
*
* @return string
*/
public function getQuantity()
{
return $this->quantity;
}
/**
* Item cost. 10 characters max.
*
* @param string|double $price
*
* @return $this
*/
public function setPrice($price)
{
NumericValidator::validate($price, "Price");
$price = FormatConverter::formatToPrice($price, $this->getCurrency());
$this->price = $price;
return $this;
}
/**
* Item cost. 10 characters max.
*
* @return string
*/
public function getPrice()
{
return $this->price;
}
/**
* 3-letter [currency code](https://developer.paypal.com/docs/integration/direct/rest_api_payment_country_currency_support/).
*
* @param string $currency
*
* @return $this
*/
public function setCurrency($currency)
{
$this->currency = $currency;
return $this;
}
/**
* 3-letter [currency code](https://developer.paypal.com/docs/integration/direct/rest_api_payment_country_currency_support/).
*
* @return string
*/
public function getCurrency()
{
return $this->currency;
}
//More similar stuff
$items = Session::get('items');
$deco = json_decode($items,true);
$itemList = new ItemList();
foreach($deco['items'] as $element) {
$item = new Item();
$item->setName($element['title'])
->setCurrency('USD')
->setQuantity($element['quantity'])
->setSku($element['id'])
->setPrice($element['unit_price']);
$itemList->addItem($item);
}
$arr = $itemList->toArray();
return $arr;