How to convert string into array? Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Array in magento 2How to move minicart into topbar.phtml?Convert event in Magento2?Warning: array_merge(): Argument #2 is not an arrayget order by string/value in product_options arrayMagento 2, How to respond array in API without sortMagento 225 :Notice: Array to string conversionget translated string Magento 2Help with Magento error on an old install (Array to string conversion)Convert Magento 2 frontend with angular 7?

Special flights

Most effective melee weapons for arboreal combat? (pre-gunpowder technology)

Understanding p-Values using an example

I can't produce songs

A term for a woman complaining about things/begging in a cute/childish way

Asymptotics question

Does the Mueller report show a conspiracy between Russia and the Trump Campaign?

Nose gear failure in single prop aircraft: belly landing or nose-gear up landing?

What adaptations would allow standard fantasy dwarves to survive in the desert?

Resize vertical bars (absolute-value symbols)

Why is it faster to reheat something than it is to cook it?

Why not use the yoke to control yaw, as well as pitch and roll?

What order were files/directories output in dir?

Mounting TV on a weird wall that has some material between the drywall and stud

Co-worker has annoying ringtone

AppleTVs create a chatty alternate WiFi network

Why are vacuum tubes still used in amateur radios?

What are the main differences between the original Stargate SG-1 and the Final Cut edition?

Does silver oxide react with hydrogen sulfide?

I got rid of Mac OSX and replaced it with linux but now I can't change it back to OSX or windows

Tannaka duality for semisimple groups

What initially awakened the Balrog?

Why is a lens darker than other ones when applying the same settings?

malloc in main() or malloc in another function: allocating memory for a struct and its members



How to convert string into array?



Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?Array in magento 2How to move minicart into topbar.phtml?Convert event in Magento2?Warning: array_merge(): Argument #2 is not an arrayget order by string/value in product_options arrayMagento 2, How to respond array in API without sortMagento 225 :Notice: Array to string conversionget translated string Magento 2Help with Magento error on an old install (Array to string conversion)Convert Magento 2 frontend with angular 7?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















I am getting the below mentioned string in my API response. How can I convert this into array?



 "product_options": ""info_buyRequest":"qty":1,"giftcard_lifetime":null,"giftcard_is_redeemable":0,"giftcard_email_template":null,"giftcard_type":null"



Vendor/CustomApi/Model/OrderHistory.php




<?php

namespace VendorCustomApiModel;

use AmosCustomApiApiOrderHistoryInterface;
use MagentoCustomerModelCustomerFactory;
use MagentoCustomerApiCustomerRepositoryInterface;
use MagentoFrameworkExceptionNoSuchEntityException;

class OrderHistory implements OrderHistoryInterface

protected $customerFactory;
protected $customerRepository;

public function __construct(CustomerFactory $customerFactory, CustomerRepositoryInterface $customerRepository)
$this->customerFactory = $customerFactory;
$this->customerRepository = $customerRepository;


public function getOrderHistory($customerId, $direction, $pageSize, $currentPage)
try
$customer = $this->customerFactory->create()->getCollection()
->addAttributeToSelect("*")
->addAttributeToFilter("entity_id", array("eq" => $customerId));
$CustomerResult = $customer->getData();
$customerInfo = $this->customerRepository->getById($customerId);
$customerAttributeData = $customerInfo->__toArray();
$VesselId = $customerAttributeData['custom_attributes']['customer_vessels']['value'];
$customerData = $this->customerFactory->create()->getCollection()
->addAttributeToSelect("*")
->addAttributeToFilter("customer_vessels", array("eq" => $VesselId))
->addAttributeToFilter("group_id", array("eq" => self::GROUP_ID))->load();
$CustomerDataResult = $customerData->getData();
$captinId = $CustomerDataResult[0]['entity_id'];
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$order = $objectManager->create('MagentoSalesModelOrder')->getCollection()->addAttributeToFilter('customer_id', $captinId)->setOrder('entity_id', $direction)->setPageSize($pageSize)->setCurPage($currentPage);
$orderInfo = $order->getData();
// print_r($orderInfo);die();
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$orderItemCollection = $objectManager->create('MagentoSalesModelResourceModelOrderItemCollection')
->setOrder('entity_id', $direction);

$orderItemCollection->getSelect()->joinLeft(
'sales_order', 'sales_order.entity_id=main_table.order_id', ['increment_id']
)->where('sales_order.customer_id=?', $captinId);
$orderResult = $orderItemCollection->getData();
foreach ($orderInfo as $k => $v)
foreach ($orderResult as $k1 => $v1)
if ($v1['order_id'] == $v['entity_id'])
$orderInfo[$k]['product_details'][] = $v1;



catch (Exception $e)
throw new NoSuchEntityException(
__('Invalid customer.')
);

return json_decode($orderInfo);






Vendor/CustomApi/Api/OrderHistoryInterface.php




<?php

namespace VendorCustomApiApi;


interface OrderHistoryInterface
CONST GROUP_ID = 5;
/**
* User can login and once logged in success, user will receive their information with order history.
* @param int $customerId
* @param string $direction
* @param int $pageSize
* @param int $currentPage
* @return mixed Order History
*/
public function getOrderHistory($customerId,$direction,$pageSize,$currentPage);










share|improve this question



















  • 1





    have you tried json_decode ?

    – Shoaib Munir
    2 days ago











  • I have tried this but getting error as following: "message": "Class mixed does not exist", "code": -1,

    – Meetali Gupta
    2 days ago












  • please share your code here, it should work with json_decode

    – Shoaib Munir
    2 days ago






  • 2





    I have added my code. Please check.

    – Meetali Gupta
    2 days ago






  • 1





    I am unable to find json_decode in your code

    – Shoaib Munir
    2 days ago

















2















I am getting the below mentioned string in my API response. How can I convert this into array?



 "product_options": ""info_buyRequest":"qty":1,"giftcard_lifetime":null,"giftcard_is_redeemable":0,"giftcard_email_template":null,"giftcard_type":null"



Vendor/CustomApi/Model/OrderHistory.php




<?php

namespace VendorCustomApiModel;

use AmosCustomApiApiOrderHistoryInterface;
use MagentoCustomerModelCustomerFactory;
use MagentoCustomerApiCustomerRepositoryInterface;
use MagentoFrameworkExceptionNoSuchEntityException;

class OrderHistory implements OrderHistoryInterface

protected $customerFactory;
protected $customerRepository;

public function __construct(CustomerFactory $customerFactory, CustomerRepositoryInterface $customerRepository)
$this->customerFactory = $customerFactory;
$this->customerRepository = $customerRepository;


public function getOrderHistory($customerId, $direction, $pageSize, $currentPage)
try
$customer = $this->customerFactory->create()->getCollection()
->addAttributeToSelect("*")
->addAttributeToFilter("entity_id", array("eq" => $customerId));
$CustomerResult = $customer->getData();
$customerInfo = $this->customerRepository->getById($customerId);
$customerAttributeData = $customerInfo->__toArray();
$VesselId = $customerAttributeData['custom_attributes']['customer_vessels']['value'];
$customerData = $this->customerFactory->create()->getCollection()
->addAttributeToSelect("*")
->addAttributeToFilter("customer_vessels", array("eq" => $VesselId))
->addAttributeToFilter("group_id", array("eq" => self::GROUP_ID))->load();
$CustomerDataResult = $customerData->getData();
$captinId = $CustomerDataResult[0]['entity_id'];
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$order = $objectManager->create('MagentoSalesModelOrder')->getCollection()->addAttributeToFilter('customer_id', $captinId)->setOrder('entity_id', $direction)->setPageSize($pageSize)->setCurPage($currentPage);
$orderInfo = $order->getData();
// print_r($orderInfo);die();
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$orderItemCollection = $objectManager->create('MagentoSalesModelResourceModelOrderItemCollection')
->setOrder('entity_id', $direction);

$orderItemCollection->getSelect()->joinLeft(
'sales_order', 'sales_order.entity_id=main_table.order_id', ['increment_id']
)->where('sales_order.customer_id=?', $captinId);
$orderResult = $orderItemCollection->getData();
foreach ($orderInfo as $k => $v)
foreach ($orderResult as $k1 => $v1)
if ($v1['order_id'] == $v['entity_id'])
$orderInfo[$k]['product_details'][] = $v1;



catch (Exception $e)
throw new NoSuchEntityException(
__('Invalid customer.')
);

return json_decode($orderInfo);






Vendor/CustomApi/Api/OrderHistoryInterface.php




<?php

namespace VendorCustomApiApi;


interface OrderHistoryInterface
CONST GROUP_ID = 5;
/**
* User can login and once logged in success, user will receive their information with order history.
* @param int $customerId
* @param string $direction
* @param int $pageSize
* @param int $currentPage
* @return mixed Order History
*/
public function getOrderHistory($customerId,$direction,$pageSize,$currentPage);










share|improve this question



















  • 1





    have you tried json_decode ?

    – Shoaib Munir
    2 days ago











  • I have tried this but getting error as following: "message": "Class mixed does not exist", "code": -1,

    – Meetali Gupta
    2 days ago












  • please share your code here, it should work with json_decode

    – Shoaib Munir
    2 days ago






  • 2





    I have added my code. Please check.

    – Meetali Gupta
    2 days ago






  • 1





    I am unable to find json_decode in your code

    – Shoaib Munir
    2 days ago













2












2








2








I am getting the below mentioned string in my API response. How can I convert this into array?



 "product_options": ""info_buyRequest":"qty":1,"giftcard_lifetime":null,"giftcard_is_redeemable":0,"giftcard_email_template":null,"giftcard_type":null"



Vendor/CustomApi/Model/OrderHistory.php




<?php

namespace VendorCustomApiModel;

use AmosCustomApiApiOrderHistoryInterface;
use MagentoCustomerModelCustomerFactory;
use MagentoCustomerApiCustomerRepositoryInterface;
use MagentoFrameworkExceptionNoSuchEntityException;

class OrderHistory implements OrderHistoryInterface

protected $customerFactory;
protected $customerRepository;

public function __construct(CustomerFactory $customerFactory, CustomerRepositoryInterface $customerRepository)
$this->customerFactory = $customerFactory;
$this->customerRepository = $customerRepository;


public function getOrderHistory($customerId, $direction, $pageSize, $currentPage)
try
$customer = $this->customerFactory->create()->getCollection()
->addAttributeToSelect("*")
->addAttributeToFilter("entity_id", array("eq" => $customerId));
$CustomerResult = $customer->getData();
$customerInfo = $this->customerRepository->getById($customerId);
$customerAttributeData = $customerInfo->__toArray();
$VesselId = $customerAttributeData['custom_attributes']['customer_vessels']['value'];
$customerData = $this->customerFactory->create()->getCollection()
->addAttributeToSelect("*")
->addAttributeToFilter("customer_vessels", array("eq" => $VesselId))
->addAttributeToFilter("group_id", array("eq" => self::GROUP_ID))->load();
$CustomerDataResult = $customerData->getData();
$captinId = $CustomerDataResult[0]['entity_id'];
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$order = $objectManager->create('MagentoSalesModelOrder')->getCollection()->addAttributeToFilter('customer_id', $captinId)->setOrder('entity_id', $direction)->setPageSize($pageSize)->setCurPage($currentPage);
$orderInfo = $order->getData();
// print_r($orderInfo);die();
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$orderItemCollection = $objectManager->create('MagentoSalesModelResourceModelOrderItemCollection')
->setOrder('entity_id', $direction);

$orderItemCollection->getSelect()->joinLeft(
'sales_order', 'sales_order.entity_id=main_table.order_id', ['increment_id']
)->where('sales_order.customer_id=?', $captinId);
$orderResult = $orderItemCollection->getData();
foreach ($orderInfo as $k => $v)
foreach ($orderResult as $k1 => $v1)
if ($v1['order_id'] == $v['entity_id'])
$orderInfo[$k]['product_details'][] = $v1;



catch (Exception $e)
throw new NoSuchEntityException(
__('Invalid customer.')
);

return json_decode($orderInfo);






Vendor/CustomApi/Api/OrderHistoryInterface.php




<?php

namespace VendorCustomApiApi;


interface OrderHistoryInterface
CONST GROUP_ID = 5;
/**
* User can login and once logged in success, user will receive their information with order history.
* @param int $customerId
* @param string $direction
* @param int $pageSize
* @param int $currentPage
* @return mixed Order History
*/
public function getOrderHistory($customerId,$direction,$pageSize,$currentPage);










share|improve this question
















I am getting the below mentioned string in my API response. How can I convert this into array?



 "product_options": ""info_buyRequest":"qty":1,"giftcard_lifetime":null,"giftcard_is_redeemable":0,"giftcard_email_template":null,"giftcard_type":null"



Vendor/CustomApi/Model/OrderHistory.php




<?php

namespace VendorCustomApiModel;

use AmosCustomApiApiOrderHistoryInterface;
use MagentoCustomerModelCustomerFactory;
use MagentoCustomerApiCustomerRepositoryInterface;
use MagentoFrameworkExceptionNoSuchEntityException;

class OrderHistory implements OrderHistoryInterface

protected $customerFactory;
protected $customerRepository;

public function __construct(CustomerFactory $customerFactory, CustomerRepositoryInterface $customerRepository)
$this->customerFactory = $customerFactory;
$this->customerRepository = $customerRepository;


public function getOrderHistory($customerId, $direction, $pageSize, $currentPage)
try
$customer = $this->customerFactory->create()->getCollection()
->addAttributeToSelect("*")
->addAttributeToFilter("entity_id", array("eq" => $customerId));
$CustomerResult = $customer->getData();
$customerInfo = $this->customerRepository->getById($customerId);
$customerAttributeData = $customerInfo->__toArray();
$VesselId = $customerAttributeData['custom_attributes']['customer_vessels']['value'];
$customerData = $this->customerFactory->create()->getCollection()
->addAttributeToSelect("*")
->addAttributeToFilter("customer_vessels", array("eq" => $VesselId))
->addAttributeToFilter("group_id", array("eq" => self::GROUP_ID))->load();
$CustomerDataResult = $customerData->getData();
$captinId = $CustomerDataResult[0]['entity_id'];
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$order = $objectManager->create('MagentoSalesModelOrder')->getCollection()->addAttributeToFilter('customer_id', $captinId)->setOrder('entity_id', $direction)->setPageSize($pageSize)->setCurPage($currentPage);
$orderInfo = $order->getData();
// print_r($orderInfo);die();
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$orderItemCollection = $objectManager->create('MagentoSalesModelResourceModelOrderItemCollection')
->setOrder('entity_id', $direction);

$orderItemCollection->getSelect()->joinLeft(
'sales_order', 'sales_order.entity_id=main_table.order_id', ['increment_id']
)->where('sales_order.customer_id=?', $captinId);
$orderResult = $orderItemCollection->getData();
foreach ($orderInfo as $k => $v)
foreach ($orderResult as $k1 => $v1)
if ($v1['order_id'] == $v['entity_id'])
$orderInfo[$k]['product_details'][] = $v1;



catch (Exception $e)
throw new NoSuchEntityException(
__('Invalid customer.')
);

return json_decode($orderInfo);






Vendor/CustomApi/Api/OrderHistoryInterface.php




<?php

namespace VendorCustomApiApi;


interface OrderHistoryInterface
CONST GROUP_ID = 5;
/**
* User can login and once logged in success, user will receive their information with order history.
* @param int $customerId
* @param string $direction
* @param int $pageSize
* @param int $currentPage
* @return mixed Order History
*/
public function getOrderHistory($customerId,$direction,$pageSize,$currentPage);







magento2.2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago







Meetali Gupta

















asked 2 days ago









Meetali GuptaMeetali Gupta

587




587







  • 1





    have you tried json_decode ?

    – Shoaib Munir
    2 days ago











  • I have tried this but getting error as following: "message": "Class mixed does not exist", "code": -1,

    – Meetali Gupta
    2 days ago












  • please share your code here, it should work with json_decode

    – Shoaib Munir
    2 days ago






  • 2





    I have added my code. Please check.

    – Meetali Gupta
    2 days ago






  • 1





    I am unable to find json_decode in your code

    – Shoaib Munir
    2 days ago












  • 1





    have you tried json_decode ?

    – Shoaib Munir
    2 days ago











  • I have tried this but getting error as following: "message": "Class mixed does not exist", "code": -1,

    – Meetali Gupta
    2 days ago












  • please share your code here, it should work with json_decode

    – Shoaib Munir
    2 days ago






  • 2





    I have added my code. Please check.

    – Meetali Gupta
    2 days ago






  • 1





    I am unable to find json_decode in your code

    – Shoaib Munir
    2 days ago







1




1





have you tried json_decode ?

– Shoaib Munir
2 days ago





have you tried json_decode ?

– Shoaib Munir
2 days ago













I have tried this but getting error as following: "message": "Class mixed does not exist", "code": -1,

– Meetali Gupta
2 days ago






I have tried this but getting error as following: "message": "Class mixed does not exist", "code": -1,

– Meetali Gupta
2 days ago














please share your code here, it should work with json_decode

– Shoaib Munir
2 days ago





please share your code here, it should work with json_decode

– Shoaib Munir
2 days ago




2




2





I have added my code. Please check.

– Meetali Gupta
2 days ago





I have added my code. Please check.

– Meetali Gupta
2 days ago




1




1





I am unable to find json_decode in your code

– Shoaib Munir
2 days ago





I am unable to find json_decode in your code

– Shoaib Munir
2 days ago










2 Answers
2






active

oldest

votes


















2














Just use this below code where you want to convert to array :



/**
* @var MagentoFrameworkJsonHelperData
*/
protected $jsonHelper;

/**
* [__construct description]
* @param MagentoFrameworkJsonHelperData $jsonHelper [description]
*/
public function __construct(
.......
MagentoFrameworkJsonHelperData $jsonHelper,
.......
)
.......
$this->jsonHelper = $jsonHelper;
.......


public function yourFunction()
$strToArr = $this->jsonHelper->jsonDecode('your data');
print_r($strToArr);






share|improve this answer






























    0














    Replace this code




    Vendor/CustomApi/Model/OrderHistory.php




    <?php

    namespace VendorCustomApiModel;

    use AmosCustomApiApiOrderHistoryInterface;
    use MagentoCustomerModelCustomerFactory;
    use MagentoCustomerApiCustomerRepositoryInterface;
    use MagentoFrameworkExceptionNoSuchEntityException;

    class OrderHistory implements OrderHistoryInterface

    protected $customerFactory;
    protected $customerRepository;

    public function __construct(CustomerFactory $customerFactory, CustomerRepositoryInterface $customerRepository)
    $this->customerFactory = $customerFactory;
    $this->customerRepository = $customerRepository;


    public function getOrderHistory($customerId, $direction, $pageSize, $currentPage)
    try
    $customer = $this->customerFactory->create()->getCollection()
    ->addAttributeToSelect("*")
    ->addAttributeToFilter("entity_id", array("eq" => $customerId));
    $CustomerResult = $customer->getData();
    $customerInfo = $this->customerRepository->getById($customerId);
    $customerAttributeData = $customerInfo->__toArray();
    $VesselId = $customerAttributeData['custom_attributes']['customer_vessels']['value'];
    $customerData = $this->customerFactory->create()->getCollection()
    ->addAttributeToSelect("*")
    ->addAttributeToFilter("customer_vessels", array("eq" => $VesselId))
    ->addAttributeToFilter("group_id", array("eq" => self::GROUP_ID))->load();
    $CustomerDataResult = $customerData->getData();
    $captinId = $CustomerDataResult[0]['entity_id'];
    $objectManager = MagentoFrameworkAppObjectManager::getInstance();
    $order = $objectManager->create('MagentoSalesModelOrder')->getCollection()->addAttributeToFilter('customer_id', $captinId)->setOrder('entity_id', $direction)->setPageSize($pageSize)->setCurPage($currentPage);
    $orderInfo = $order->getData();
    // print_r($orderInfo);die();
    $objectManager = MagentoFrameworkAppObjectManager::getInstance();
    $orderItemCollection = $objectManager->create('MagentoSalesModelResourceModelOrderItemCollection')
    ->setOrder('entity_id', $direction);

    $orderItemCollection->getSelect()->joinLeft(
    'sales_order', 'sales_order.entity_id=main_table.order_id', ['increment_id']
    )->where('sales_order.customer_id=?', $captinId);
    $orderResult = $orderItemCollection->getData();
    foreach ($orderInfo as $k => $v)
    foreach ($orderResult as $k1 => $v1)
    if ($v1['order_id'] == $v['entity_id'])
    $orderInfo[$k]['product_details'][] = $v1;



    catch (Exception $e)
    throw new NoSuchEntityException(
    __('Invalid customer.')
    );

    header('Content-Type: application/json'); echo json_encode($orderInfo);








    share|improve this answer























    • It's better to use magento standard way instead of json_encode() core php function ;) & There are little bit different output of json_encode() and MagentoFrameworkJsonHelperData class

      – Rohan Hapani
      2 days ago






    • 1





      Are it is regarding API so I am using this one :) for POSTMAN

      – Aaditya
      2 days ago











    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "479"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f270637%2fhow-to-convert-string-into-array%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Just use this below code where you want to convert to array :



    /**
    * @var MagentoFrameworkJsonHelperData
    */
    protected $jsonHelper;

    /**
    * [__construct description]
    * @param MagentoFrameworkJsonHelperData $jsonHelper [description]
    */
    public function __construct(
    .......
    MagentoFrameworkJsonHelperData $jsonHelper,
    .......
    )
    .......
    $this->jsonHelper = $jsonHelper;
    .......


    public function yourFunction()
    $strToArr = $this->jsonHelper->jsonDecode('your data');
    print_r($strToArr);






    share|improve this answer



























      2














      Just use this below code where you want to convert to array :



      /**
      * @var MagentoFrameworkJsonHelperData
      */
      protected $jsonHelper;

      /**
      * [__construct description]
      * @param MagentoFrameworkJsonHelperData $jsonHelper [description]
      */
      public function __construct(
      .......
      MagentoFrameworkJsonHelperData $jsonHelper,
      .......
      )
      .......
      $this->jsonHelper = $jsonHelper;
      .......


      public function yourFunction()
      $strToArr = $this->jsonHelper->jsonDecode('your data');
      print_r($strToArr);






      share|improve this answer

























        2












        2








        2







        Just use this below code where you want to convert to array :



        /**
        * @var MagentoFrameworkJsonHelperData
        */
        protected $jsonHelper;

        /**
        * [__construct description]
        * @param MagentoFrameworkJsonHelperData $jsonHelper [description]
        */
        public function __construct(
        .......
        MagentoFrameworkJsonHelperData $jsonHelper,
        .......
        )
        .......
        $this->jsonHelper = $jsonHelper;
        .......


        public function yourFunction()
        $strToArr = $this->jsonHelper->jsonDecode('your data');
        print_r($strToArr);






        share|improve this answer













        Just use this below code where you want to convert to array :



        /**
        * @var MagentoFrameworkJsonHelperData
        */
        protected $jsonHelper;

        /**
        * [__construct description]
        * @param MagentoFrameworkJsonHelperData $jsonHelper [description]
        */
        public function __construct(
        .......
        MagentoFrameworkJsonHelperData $jsonHelper,
        .......
        )
        .......
        $this->jsonHelper = $jsonHelper;
        .......


        public function yourFunction()
        $strToArr = $this->jsonHelper->jsonDecode('your data');
        print_r($strToArr);







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 2 days ago









        Rohan HapaniRohan Hapani

        7,01631865




        7,01631865























            0














            Replace this code




            Vendor/CustomApi/Model/OrderHistory.php




            <?php

            namespace VendorCustomApiModel;

            use AmosCustomApiApiOrderHistoryInterface;
            use MagentoCustomerModelCustomerFactory;
            use MagentoCustomerApiCustomerRepositoryInterface;
            use MagentoFrameworkExceptionNoSuchEntityException;

            class OrderHistory implements OrderHistoryInterface

            protected $customerFactory;
            protected $customerRepository;

            public function __construct(CustomerFactory $customerFactory, CustomerRepositoryInterface $customerRepository)
            $this->customerFactory = $customerFactory;
            $this->customerRepository = $customerRepository;


            public function getOrderHistory($customerId, $direction, $pageSize, $currentPage)
            try
            $customer = $this->customerFactory->create()->getCollection()
            ->addAttributeToSelect("*")
            ->addAttributeToFilter("entity_id", array("eq" => $customerId));
            $CustomerResult = $customer->getData();
            $customerInfo = $this->customerRepository->getById($customerId);
            $customerAttributeData = $customerInfo->__toArray();
            $VesselId = $customerAttributeData['custom_attributes']['customer_vessels']['value'];
            $customerData = $this->customerFactory->create()->getCollection()
            ->addAttributeToSelect("*")
            ->addAttributeToFilter("customer_vessels", array("eq" => $VesselId))
            ->addAttributeToFilter("group_id", array("eq" => self::GROUP_ID))->load();
            $CustomerDataResult = $customerData->getData();
            $captinId = $CustomerDataResult[0]['entity_id'];
            $objectManager = MagentoFrameworkAppObjectManager::getInstance();
            $order = $objectManager->create('MagentoSalesModelOrder')->getCollection()->addAttributeToFilter('customer_id', $captinId)->setOrder('entity_id', $direction)->setPageSize($pageSize)->setCurPage($currentPage);
            $orderInfo = $order->getData();
            // print_r($orderInfo);die();
            $objectManager = MagentoFrameworkAppObjectManager::getInstance();
            $orderItemCollection = $objectManager->create('MagentoSalesModelResourceModelOrderItemCollection')
            ->setOrder('entity_id', $direction);

            $orderItemCollection->getSelect()->joinLeft(
            'sales_order', 'sales_order.entity_id=main_table.order_id', ['increment_id']
            )->where('sales_order.customer_id=?', $captinId);
            $orderResult = $orderItemCollection->getData();
            foreach ($orderInfo as $k => $v)
            foreach ($orderResult as $k1 => $v1)
            if ($v1['order_id'] == $v['entity_id'])
            $orderInfo[$k]['product_details'][] = $v1;



            catch (Exception $e)
            throw new NoSuchEntityException(
            __('Invalid customer.')
            );

            header('Content-Type: application/json'); echo json_encode($orderInfo);








            share|improve this answer























            • It's better to use magento standard way instead of json_encode() core php function ;) & There are little bit different output of json_encode() and MagentoFrameworkJsonHelperData class

              – Rohan Hapani
              2 days ago






            • 1





              Are it is regarding API so I am using this one :) for POSTMAN

              – Aaditya
              2 days ago















            0














            Replace this code




            Vendor/CustomApi/Model/OrderHistory.php




            <?php

            namespace VendorCustomApiModel;

            use AmosCustomApiApiOrderHistoryInterface;
            use MagentoCustomerModelCustomerFactory;
            use MagentoCustomerApiCustomerRepositoryInterface;
            use MagentoFrameworkExceptionNoSuchEntityException;

            class OrderHistory implements OrderHistoryInterface

            protected $customerFactory;
            protected $customerRepository;

            public function __construct(CustomerFactory $customerFactory, CustomerRepositoryInterface $customerRepository)
            $this->customerFactory = $customerFactory;
            $this->customerRepository = $customerRepository;


            public function getOrderHistory($customerId, $direction, $pageSize, $currentPage)
            try
            $customer = $this->customerFactory->create()->getCollection()
            ->addAttributeToSelect("*")
            ->addAttributeToFilter("entity_id", array("eq" => $customerId));
            $CustomerResult = $customer->getData();
            $customerInfo = $this->customerRepository->getById($customerId);
            $customerAttributeData = $customerInfo->__toArray();
            $VesselId = $customerAttributeData['custom_attributes']['customer_vessels']['value'];
            $customerData = $this->customerFactory->create()->getCollection()
            ->addAttributeToSelect("*")
            ->addAttributeToFilter("customer_vessels", array("eq" => $VesselId))
            ->addAttributeToFilter("group_id", array("eq" => self::GROUP_ID))->load();
            $CustomerDataResult = $customerData->getData();
            $captinId = $CustomerDataResult[0]['entity_id'];
            $objectManager = MagentoFrameworkAppObjectManager::getInstance();
            $order = $objectManager->create('MagentoSalesModelOrder')->getCollection()->addAttributeToFilter('customer_id', $captinId)->setOrder('entity_id', $direction)->setPageSize($pageSize)->setCurPage($currentPage);
            $orderInfo = $order->getData();
            // print_r($orderInfo);die();
            $objectManager = MagentoFrameworkAppObjectManager::getInstance();
            $orderItemCollection = $objectManager->create('MagentoSalesModelResourceModelOrderItemCollection')
            ->setOrder('entity_id', $direction);

            $orderItemCollection->getSelect()->joinLeft(
            'sales_order', 'sales_order.entity_id=main_table.order_id', ['increment_id']
            )->where('sales_order.customer_id=?', $captinId);
            $orderResult = $orderItemCollection->getData();
            foreach ($orderInfo as $k => $v)
            foreach ($orderResult as $k1 => $v1)
            if ($v1['order_id'] == $v['entity_id'])
            $orderInfo[$k]['product_details'][] = $v1;



            catch (Exception $e)
            throw new NoSuchEntityException(
            __('Invalid customer.')
            );

            header('Content-Type: application/json'); echo json_encode($orderInfo);








            share|improve this answer























            • It's better to use magento standard way instead of json_encode() core php function ;) & There are little bit different output of json_encode() and MagentoFrameworkJsonHelperData class

              – Rohan Hapani
              2 days ago






            • 1





              Are it is regarding API so I am using this one :) for POSTMAN

              – Aaditya
              2 days ago













            0












            0








            0







            Replace this code




            Vendor/CustomApi/Model/OrderHistory.php




            <?php

            namespace VendorCustomApiModel;

            use AmosCustomApiApiOrderHistoryInterface;
            use MagentoCustomerModelCustomerFactory;
            use MagentoCustomerApiCustomerRepositoryInterface;
            use MagentoFrameworkExceptionNoSuchEntityException;

            class OrderHistory implements OrderHistoryInterface

            protected $customerFactory;
            protected $customerRepository;

            public function __construct(CustomerFactory $customerFactory, CustomerRepositoryInterface $customerRepository)
            $this->customerFactory = $customerFactory;
            $this->customerRepository = $customerRepository;


            public function getOrderHistory($customerId, $direction, $pageSize, $currentPage)
            try
            $customer = $this->customerFactory->create()->getCollection()
            ->addAttributeToSelect("*")
            ->addAttributeToFilter("entity_id", array("eq" => $customerId));
            $CustomerResult = $customer->getData();
            $customerInfo = $this->customerRepository->getById($customerId);
            $customerAttributeData = $customerInfo->__toArray();
            $VesselId = $customerAttributeData['custom_attributes']['customer_vessels']['value'];
            $customerData = $this->customerFactory->create()->getCollection()
            ->addAttributeToSelect("*")
            ->addAttributeToFilter("customer_vessels", array("eq" => $VesselId))
            ->addAttributeToFilter("group_id", array("eq" => self::GROUP_ID))->load();
            $CustomerDataResult = $customerData->getData();
            $captinId = $CustomerDataResult[0]['entity_id'];
            $objectManager = MagentoFrameworkAppObjectManager::getInstance();
            $order = $objectManager->create('MagentoSalesModelOrder')->getCollection()->addAttributeToFilter('customer_id', $captinId)->setOrder('entity_id', $direction)->setPageSize($pageSize)->setCurPage($currentPage);
            $orderInfo = $order->getData();
            // print_r($orderInfo);die();
            $objectManager = MagentoFrameworkAppObjectManager::getInstance();
            $orderItemCollection = $objectManager->create('MagentoSalesModelResourceModelOrderItemCollection')
            ->setOrder('entity_id', $direction);

            $orderItemCollection->getSelect()->joinLeft(
            'sales_order', 'sales_order.entity_id=main_table.order_id', ['increment_id']
            )->where('sales_order.customer_id=?', $captinId);
            $orderResult = $orderItemCollection->getData();
            foreach ($orderInfo as $k => $v)
            foreach ($orderResult as $k1 => $v1)
            if ($v1['order_id'] == $v['entity_id'])
            $orderInfo[$k]['product_details'][] = $v1;



            catch (Exception $e)
            throw new NoSuchEntityException(
            __('Invalid customer.')
            );

            header('Content-Type: application/json'); echo json_encode($orderInfo);








            share|improve this answer













            Replace this code




            Vendor/CustomApi/Model/OrderHistory.php




            <?php

            namespace VendorCustomApiModel;

            use AmosCustomApiApiOrderHistoryInterface;
            use MagentoCustomerModelCustomerFactory;
            use MagentoCustomerApiCustomerRepositoryInterface;
            use MagentoFrameworkExceptionNoSuchEntityException;

            class OrderHistory implements OrderHistoryInterface

            protected $customerFactory;
            protected $customerRepository;

            public function __construct(CustomerFactory $customerFactory, CustomerRepositoryInterface $customerRepository)
            $this->customerFactory = $customerFactory;
            $this->customerRepository = $customerRepository;


            public function getOrderHistory($customerId, $direction, $pageSize, $currentPage)
            try
            $customer = $this->customerFactory->create()->getCollection()
            ->addAttributeToSelect("*")
            ->addAttributeToFilter("entity_id", array("eq" => $customerId));
            $CustomerResult = $customer->getData();
            $customerInfo = $this->customerRepository->getById($customerId);
            $customerAttributeData = $customerInfo->__toArray();
            $VesselId = $customerAttributeData['custom_attributes']['customer_vessels']['value'];
            $customerData = $this->customerFactory->create()->getCollection()
            ->addAttributeToSelect("*")
            ->addAttributeToFilter("customer_vessels", array("eq" => $VesselId))
            ->addAttributeToFilter("group_id", array("eq" => self::GROUP_ID))->load();
            $CustomerDataResult = $customerData->getData();
            $captinId = $CustomerDataResult[0]['entity_id'];
            $objectManager = MagentoFrameworkAppObjectManager::getInstance();
            $order = $objectManager->create('MagentoSalesModelOrder')->getCollection()->addAttributeToFilter('customer_id', $captinId)->setOrder('entity_id', $direction)->setPageSize($pageSize)->setCurPage($currentPage);
            $orderInfo = $order->getData();
            // print_r($orderInfo);die();
            $objectManager = MagentoFrameworkAppObjectManager::getInstance();
            $orderItemCollection = $objectManager->create('MagentoSalesModelResourceModelOrderItemCollection')
            ->setOrder('entity_id', $direction);

            $orderItemCollection->getSelect()->joinLeft(
            'sales_order', 'sales_order.entity_id=main_table.order_id', ['increment_id']
            )->where('sales_order.customer_id=?', $captinId);
            $orderResult = $orderItemCollection->getData();
            foreach ($orderInfo as $k => $v)
            foreach ($orderResult as $k1 => $v1)
            if ($v1['order_id'] == $v['entity_id'])
            $orderInfo[$k]['product_details'][] = $v1;



            catch (Exception $e)
            throw new NoSuchEntityException(
            __('Invalid customer.')
            );

            header('Content-Type: application/json'); echo json_encode($orderInfo);









            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 2 days ago









            AadityaAaditya

            4,36621139




            4,36621139












            • It's better to use magento standard way instead of json_encode() core php function ;) & There are little bit different output of json_encode() and MagentoFrameworkJsonHelperData class

              – Rohan Hapani
              2 days ago






            • 1





              Are it is regarding API so I am using this one :) for POSTMAN

              – Aaditya
              2 days ago

















            • It's better to use magento standard way instead of json_encode() core php function ;) & There are little bit different output of json_encode() and MagentoFrameworkJsonHelperData class

              – Rohan Hapani
              2 days ago






            • 1





              Are it is regarding API so I am using this one :) for POSTMAN

              – Aaditya
              2 days ago
















            It's better to use magento standard way instead of json_encode() core php function ;) & There are little bit different output of json_encode() and MagentoFrameworkJsonHelperData class

            – Rohan Hapani
            2 days ago





            It's better to use magento standard way instead of json_encode() core php function ;) & There are little bit different output of json_encode() and MagentoFrameworkJsonHelperData class

            – Rohan Hapani
            2 days ago




            1




            1





            Are it is regarding API so I am using this one :) for POSTMAN

            – Aaditya
            2 days ago





            Are it is regarding API so I am using this one :) for POSTMAN

            – Aaditya
            2 days ago

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Magento Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f270637%2fhow-to-convert-string-into-array%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

            Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

            Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?