---
title: "How to Convert Layout XML Content into Array in Magento 2?"
url: "https://magecomp.com/blog/convert-layout-xml-content-into-array-magento-2/"
date: "2022-06-20T15:11:15+00:00"
modified: "2025-02-19T05:06:00+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 243
reading_time: "2 min read"
summary: "Hello Magento Friends,"
description: "Convert Layout XML Content into Array in Magento 2 to get layout file data. Learn more"
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to Show SKU in Order Summary in Magento 2?"
    url: "https://magecomp.com/blog/show-sku-in-order-summary-magento-2/"
  - title: "Magento 2: How to Add Custom Column at Admin Order View Item Orders"
    url: "https://magecomp.com/blog/magento-2-add-custom-column-at-admin-order-view-item-orders/"
  - title: "How to Add Product Images in Magento 2 Invoice PDF?"
    url: "https://magecomp.com/blog/add-product-images-magento-2-invoice-pdf/"
---

# How to Convert Layout XML Content into Array in Magento 2?

_Published: June 20, 2022_  
_Author: Dhiren Vasoya_  

![How to Convert Layout XML Content into Array in Magento 2](https://magecomp.com/blog/wp-content/uploads/2022/06/How-to-Convert-Layout-XML-Content-into-Array-in-Magento-2-1024x512.webp)

**Hello Magento Friends,**

In this tutorial, I will throw light on How to Convert Layout XML Content into Array in Magento 2.
[![Magento 2 Speed & Performance Optimization Services](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Speed-Performance-Optimization-Services-CTA-3.webp)](https://magecomp.com/magento-performance-optimization.html)

Sometimes we need to convert a specific layout.xml file into an array to perform some operations in Magento 2. Converting XML to an array in Magento 2 is helpful to get the layout file data and display it based on the requirement.
So let’s see How you can Convert the layout XML file into an array in Magento 2.
## Steps to Convert Layout XML Content into Array in Magento 2:
**Step 1:** We are converting layout XML to array through root script that’s why we need to create root script at the below file path
**magento_root_directory\pub\layouttojson.php**

Now, add the code as follows
```
<?php
use MagentoFrameworkAppBootstrap;

require  __DIR__ .'/../app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$utilityFiles = $objectManager->get('MagentoFrameworkAppUtilityFiles');

$fileList = $utilityFiles->getLayoutFiles(['area_name' => 'frontend'], false);
foreach ($fileList as $configFile)
{
    if (strpos($configFile, 'layout_file_name.xml') !== false)
    {
	$configXml = simplexml_load_file($configFile);
	print_r($configXml);
    }
}
```

## [![hyva theme development](https://magecomp.com/blog/wp-content/uploads/2025/02/hyva-theme-development-CTA-3.webp)](https://magecomp.com/services/hyva-theme-development/)

## Conclusion:
With the help of the above steps, you can effortlessly convert Layout XML Content into Array in Magento 2. You can also [**Convert CURL XML Response into an Array in Magento 2**](https://magecomp.com/blog/convert-curl-xml-response-into-array-in-magento-2/).
If you have any doubt let me know through the comment section. Share the tutorial with your friends and stay in touch with us!
[![Hire Magento Programmer](https://magecomp.com/blog/wp-content/uploads/2025/02/Hire-Magento-Programmer-CTA-3.webp)](https://magecomp.com/services/hire-magento-programmer/)

**Happy Coding!**


---

_View the original post at: [https://magecomp.com/blog/convert-layout-xml-content-into-array-magento-2/](https://magecomp.com/blog/convert-layout-xml-content-into-array-magento-2/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-07-26 09:34:59 UTC_  
