I have created Custom Api which returns associativeArray. 
- If I use associativeArrayit's giving error
- If I use - string[]it's working- But I want to return - associativeArray.
Error: Class "array" does not exist. Please note that namespace must be specified
Example Methods:
 /**
 * Create multi Customers
 *
 * @param \Magento\Customer\Api\Data\CustomerInterface $customer
 * @return string[]
 */
 public function save(\Magento\Customer\Api\Data\CustomerInterface $customer);
save method working fine.
 /**
 * Create multi Customers
 *
 * @param \Magento\Customer\Api\Data\CustomerInterface $customer
 * @return array()|associativeArray()
 */
 public function save1(\Magento\Customer\Api\Data\CustomerInterface $customer);
save1 method returning error.
Error: Class "array()" does not exist. Please note that namespace must be specified
So how can I return associativeArray?
Thanks in advance.
- 
 see this answer magento.stackexchange.com/a/319266/93504Harsh Patel– Harsh Patel2023年01月16日 07:20:08 +00:00Commented Jan 16, 2023 at 7:20
2 Answers 2
Custom data interface should be created to represent key-value pairs, e.g.
<?php
namespace Vendor\Module\Api\Data;
/**
 * Interface which represents associative array item.
 */
interface AssociativeArrayItemInterface
{
 /**
 * Get key
 * 
 * @return string
 */
 public function getKey();
 /**
 * Get value
 * 
 * @return string
 */
 public function getValue();
}
Then for the new service method it is possible to specify:
 
@return \Vendor\Module\Api\Data\AssociativeArrayItemInterface[].
Service layer must comply with some rules to be exposed via SOAP and REST. It is impossible to represent associative arrays in WSDL without key-value data interfaces, all complex entities in WSDL must be described using primitive types (but this will make it more difficult for clients written in strictly typed languages to consume such API).
There is one exception supported though, but is highly discouraged and should be used only in very special cases. If return or param type will be specified as mixed, it will be transformed in WSDL into anyType.
- 
 @magentotwo did it work for you?Alex Paliarush– Alex Paliarush2015年12月17日 17:53:15 +00:00Commented Dec 17, 2015 at 17:53
- 
 I have some other, So didn't tried. I will back soon.. Thanks for your responseBojjaiah– Bojjaiah2015年12月18日 03:54:56 +00:00Commented Dec 18, 2015 at 3:54
- 
 Thank you Alex, Your answer give me a idea to solve my question :) you can check the same here magento.stackexchange.com/questions/247801/… +1 for your answer.Ramkishan Suthar– Ramkishan Suthar2018年10月25日 09:36:01 +00:00Commented Oct 25, 2018 at 9:36
- 
 Nice answered it's useful for APIBirjitsinh Zala– Birjitsinh Zala2019年12月27日 05:31:42 +00:00Commented Dec 27, 2019 at 5:31
Here is a little trick:
/**
* @return \stdClass[] options
*/
public function getOptions();
Explore related questions
See similar questions with these tags.