Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP将对象转换为基于特定键的数组_Php_Arrays_Magento_Object - Fatal编程技术网

使用PHP将对象转换为基于特定键的数组

使用PHP将对象转换为基于特定键的数组,php,arrays,magento,object,Php,Arrays,Magento,Object,我对PHP非常陌生,我正在努力将一个对象转换成一个数组,以便在代码中进一步使用该数组 我的脚本从Magento API中检索产品数据,在结果变量上使用print\r后显示如下结果: 现在我可以看到这是以“array”开始的,但是当我尝试对已分配结果的变量使用echo时,我收到了错误可捕获的致命错误:stdClass类的对象无法转换为string 如何将对象转换为数组,然后拆分数组,以便将所有sku键的值作为数组作为最终结果 我曾尝试对结果变量使用array(),然后尝试使用数组函数对其进行操作

我对PHP非常陌生,我正在努力将一个对象转换成一个数组,以便在代码中进一步使用该数组

我的脚本从Magento API中检索产品数据,在结果变量上使用
print\r
后显示如下结果:

现在我可以看到这是以“array”开始的,但是当我尝试对已分配结果的变量使用echo时,我收到了错误
可捕获的致命错误:stdClass类的对象无法转换为string

如何将对象转换为数组,然后拆分数组,以便将所有
sku
键的值作为数组作为最终结果

我曾尝试对结果变量使用
array()
,然后尝试使用数组函数对其进行操作,但无论我尝试什么,都会收到错误,因此我认为我没有正确理解这一点

感谢您提供的任何见解。如果您需要查看,我的代码如下:

<?php
    // Global variables.
    $client = new SoapClient('xxx'); // Magento API URL.
    $session_id = $client->login('xxx', 'xxx'); // API Username and API Key.

    // Filter where the 'FMA Stock' attribute is set to 'Yes'.
    $fma_stock_filter = array('complex_filter'=>
        array(
            array('key'=>'fma_stock', 'value'=>array('key' =>'eq', 'value' => 'Yes')),
        ),
    );

    // Assign the list of products to $zoey_product_list.
    $zoey_product_list = $client->catalogProductList($session_id, $fma_stock_filter);

    echo $zoey_product_list[0];
?>

如果您只需要将所有SKU重写到一个数组中,您可以这样做:

$skus = [];
foreach ($items as $item) { // not sure which variable from your code contains print_r'd data, so assuming $items
    $skus[] = $item->sku;
}
像这样的一行代码也可以:

$skus = array_map(function($item) { return $item->sku; }, $items);
但是,如果您确实想要转换标准对象或这些对象的数组,那么一个非优雅但有效且简单的解决方案是将其转换为JSON并返回:

$array = json_decode(json_encode($objects), true); 

您正在接收一个对象数组(请参见
stdClass对象
)。因此,一个简单的类型转换应该适用于您的:
$array=(array)$zoey_product_列表[0]谢谢,现在这更有意义了:)