How To

Magento 2: How to Pass Multi-Dimensional Array as an Argument in REST API

Hello Magento Friends,

In this Magento 2 tutorial guide, I will show How to Pass a Multi-Dimensional Array as an Argument in REST API in Magento 2.

A multi-dimensional array is an array that contains one or more arrays as its elements. In other words, each element of a multi-dimensional array can itself be an array.  REST API allows you to interact with Magento programmatically. 

However, there are instances where you might need to pass multi-dimensional arrays as arguments in your REST API calls, and this can be a bit tricky. In this blog post, we’ll explore how to do just that.

Steps to Pass Multi-Dimensional Array as an Argument in REST API in Magento 2:

Step 1: First, we need to create a “webapi.xml“ file inside our extension at the following path

app\code\Vendor\Extension\etc\webapi.xml

And add the code as follows

<?xml version="1.0" ?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
    <route method="POST" url="/V1/vender-extension/submitform">
        <service class="Vendor\Extension\Api\PostManagementInterface" method="setFormData"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
         <data>
            <parameter name="orderarray" force="true">%orderarray%</parameter>
        </data>
    </route>
</routes>

Step 2: Now, we need to create a “di.xml“ file inside our extension at the following path

app\code\Vendor\Extension\etc\di.xml

Then include the following piece of code

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Vendor\Extension\Api\PostManagementInterface" type="Vendor\Extension\Model\PostManagement" />
</config>

Step 3: Now, we need to create a “PostManagementInterface.php“ file inside our extension at the following path

app/code/Vendor/Extension/Api/PostManagementInterface.php

And add the code as mentioned below

<?php

namespace Vendor\Extension\Api;

interface PostManagementInterface
{

    /**
     * SET form data
     * @param mixed $orderarray
     * @return mixed
     */    public function setFormData($orderarray);
}

Step 4: Now, we need to create a “PostManagementInterface.php“ file inside our extension at the following path

app/code/Vendor/Extension/Model/PostManagement.php

After that, add the following code

<?php

namespace Vendor\Extension\Model;

use Vendor\Extension\Api\PostManagementInterface;

class PostManagement implements PostManagementInterface
{
    public function setFormData($orderarray)
    {
        $data = [];
        $requestData = json_decode(file_get_contents('php://input'), true);
        $getOrderArray = $requestData['orderarray'];
        print_r($getOrderArray);

        $data = ["status"=>true, "message"=> "passes data successfully."];
        return [$data];
    }
}

You need to call above API as per below parameters.

{
    "orderarray": [
        {"orderId": "1", "comment": "test order"},
        {"orderId": "2", "comment": "demo order"}
    ]
}

Conclusion:

Hope you understood How to Pass Multi-Dimensional Array as an Argument in REST API in Magento 2. If you have any doubt, let me know through the comment section and I will quickly get back to you with the solution. Alternatively, Hire a Magento Developer for the custom requirements of your Magento 2 store. Share the tutorial with your friends and stay connected with us for more such Magento 2 solutions.

Happy Coding!

Click to rate this post!
[Total: 1 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

How to Add a Custom Field to the Cart Price Rules Form in Magento 2?

Hello Magento Friends, In this blog, we will learn How to Add a Custom Field…

15 hours ago

How to Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

4 days ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

5 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

6 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

1 week ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago