我正在调用一个 magento soap v2 api 并作为返回得到一个对象的响应,我试图在关联数组中转换该对象。但是当我尝试使用 json 编码/解码方法执行此操作时,它返回一个空数组。
$result = Magento::call()->catalogProductList();
$array = json_decode(json_encode($result), true);
1)对象不为空。 2) 类型转换对我来说不是一个选项,因为我试图避免在前面加上 *。
更新
这是我尝试编码的结果值。
Tinyrocket\Magento\Objects\MagentoObjectCollection Object
(
[collection:protected] => Array
(
[0] => Tinyrocket\Magento\Objects\MagentoObject Object
(
[data:protected] => Array
(
[product_id] => 9
[sku] => Tilapia
[name] => Tilapia
[set] => 4
[type] => simple
[category_ids] => Array
(
[0] => 2
[1] => 4
)
[website_ids] => Array
(
[0] => 1
)
)
)
[1] => Tinyrocket\Magento\Objects\MagentoObject Object
(
[data:protected] => Array
(
[product_id] => 10
[sku] => Deshi Rui
[name] => Deshi Rui
[set] => 4
[type] => simple
[category_ids] => Array
(
[0] => 2
[1] => 4
)
[website_ids] => Array
(
[0] => 1
)
)
)
...
...
更新 2
var_export($result) 的输出
Tinyrocket\Magento\Objects\MagentoObjectCollection::__set_state(array(
'collection' =>
array (
0 =>
Tinyrocket\Magento\Objects\MagentoObject::__set_state(array(
'data' =>
array (
'product_id' => '9',
'sku' => 'Tilapia',
'name' => 'Tilapia',
'set' => '4',
'type' => 'simple',
'category_ids' =>
array (
0 => '2',
1 => '4',
),
'website_ids' =>
array (
0 => '1',
),
),
)),
1 =>
Tinyrocket\Magento\Objects\MagentoObject::__set_state(array(
'data' =>
array (
'product_id' => '10',
'sku' => 'Deshi Rui',
'name' => 'Deshi Rui',
'set' => '4',
'type' => 'simple',
'category_ids' =>
array (
0 => '2',
1 => '4',
),
'website_ids' =>
array (
0 => '1',
),
),
)),
'count' => 2,
))
最佳答案
json_encode
将您的对象相应地转换为其属性的可见性。
因为 Tinyrocket\Magento\Objects\MagentoObjectCollection
的 $collection
是一个 protected 属性,它的值不会被 json_encode
读取。
对于这个问题我有两个解决方案,其中一个需要修改 Magento 的源代码,所以我不推荐它,因为它可能会产生错误,或者每次更新 CMS 时都会中断。
第一个解决方案使用 Reflection
,因此您将需要 PHP 5,这应该不是问题,因为 Magento needs PHP 5.4 .
以下函数循环遍历 \Tinyrocket\Magento\Objects\MagentoObjectCollection
对象以读取所有属性,并返回一个数组。
function magentoObjectCollectionToArray(\Tinyrocket\Magento\Objects\MagentoObjectCollection $object)
{
// The basic structure of your array.
$array = array(
'collection' => array()
);
// Since $collection is a protected property, we need to reflect it to read the value.
$collection_reflection = new \ReflectionProperty('\Tinyrocket\Magento\Objects\MagentoObjectCollection', 'collection');
// This method allows you to read protected and private properties for the current ReflectionProperty object.
$collection_reflection->setAccessible(true);
// Now we need to loop through all objects...
foreach ($collection_reflection->getValue($object) as $property => $value)
{
// Two cases : either a \Tinyrocket\Magento\Objects\MagentoObject object, or the $count property.
if ($value instanceof \Tinyrocket\Magento\Objects\MagentoObject)
{
// Same here, since $data is also a protected property, we need to reflect it.
$data_reflection = new \ReflectionProperty('\Tinyrocket\Magento\Objects\MagentoObject', 'data');
$data_reflection->setAccessible(true);
$array['collection'][$property] = array(
'data' => $data_reflection->getValue($value)
);
}
else
{
// We don't forget the $count property.
$array['collection'][$property] = $value;
}
}
// And you have your array without using JSON.
return $array;
}
PHP 文档链接:
第二种解决方案使用JsonSerializable
,因此您将需要 PHP 5.4,这也不成问题。
一旦jsonSerialize
方法在实现 JsonSerializable 的类上实现,json_encode
将对 jsonSerialize
的返回值进行编码。
因此,您可以修改 Magento 的核心,并制作 \Tinyrocket\Magento\Objects\MagentoObjectCollection
和 \Tinyrocket\Magento\Objects\MagentoObject
类来实现 \JsonSerializable
,并将此 JsonSerialize
方法添加到他们的源代码中:
class XXX implements \JsonSerializable
{
public function JsonSerialize()
{
// Returns all variables. Since we're in the object context, we've access to all of them.
return get_object_vars($this);
}
}
然后你通过调用 json_encode()
/json_decode()
得到你的数组:
json_decode(json_encode($result), true)
虽然此解决方案可能有助于解决您的问题,但我不会推荐它,因为它需要修改 Magento 的核心,这可能会破坏其他模块并且在更新后不再工作。 您应该改为创建自己的插件并使用第一个解决方案,这是最好的方法。
两种解决方案都返回这个数组:
array (
'collection' =>
array (
0 =>
array (
'data' =>
array (
'product_id' => '9',
'sku' => 'Tilapia',
'name' => 'Tilapia',
'set' => '4',
'type' => 'simple',
'category_ids' =>
array (
0 => '2',
1 => '4',
),
'website_ids' =>
array (
0 => '1',
),
),
),
1 =>
array (
'data' =>
array (
'product_id' => '10',
'sku' => 'Deshi Rui',
'name' => 'Deshi Rui',
'set' => '4',
'type' => 'simple',
'category_ids' =>
array (
0 => '2',
1 => '4',
),
'website_ids' =>
array (
0 => '1',
),
),
),
'count' => 2,
),
)
关于php - 将对象转换为关联数组返回空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30137310/