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

Why Your Business Needs a SaaS/E-commerce Marketing Agency?

In today’s era, having a solid online presence and effective marketing strategies are vital factors…

2 hours ago

Handling File Uploads in a Remix Application

In this blog, I will explain about handling file uploads in Shopify Remix App. File…

2 hours ago

Upgrade Your E-commerce Store with Magento 2 Hyvä Theme

Magento 2 Hyvä Theme is quickly becoming a popular choice among e-commerce businesses for its…

1 day ago

A Complete Guide of Hyvä Themes

In the rapidly evolving world of e-commerce, the success of an online store greatly hinges…

1 day ago

Magento 2: How to Add Custom Button to Download Custom Created PDF Programmatically in Admin Sales Order View

Hello Magento Friends, Ever wanted to provide admins with a quick way to download custom…

1 day ago

Mastering Tailwind CSS in Laravel: A Comprehensive Guide

Tailwind CSS has emerged as a powerful utility-first CSS framework, offering developers a unique approach…

6 days ago