ahmadajah03 Wed Jan 2021 1 year ago

Bagaimana Cara Convert file XML ke Array di Laravel?

Hai,

Hari ini, saya akan memberi tahu Anda contoh cara membaca file xml di laravel. saya menjelaskan secara sederhana tentang file xml baca laravel. Anda akan belajar laravel membaca contoh xml. Kami akan menggunakan file read xml di laravel. Mari kita lihat contoh di bawah ini, convert file xml ke php array laravel.

Anda dapat dengan mudah membaca file xml di versi laravel 6, laravel 7 dan laravel 8.

Dalam contoh ini, saya akan memberi Anda contoh yang sangat sederhana untuk mengubah array xml ke php di laravel. jadi di sini, kami akan menyimpan satu file contoh sederhana dan mengubahnya menjadi array php.

Mari kita lihat sekarang:

Tambahkan File Xml Sederhana:

Anda dapat membuat atau mengunduh file xml sederhana seperti di bawah ini:

Kode Controller:

sekarang Anda dapat melihat bagaimana mengkonversi xml ke array menggunakan kode di bawah ini:

<?php
  
namespace App\Http\Controllers;
   
use Illuminate\Http\Request;
  
class XMLController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $xmlString = file_get_contents(public_path('sample.xml'));
        $xmlObject = simplexml_load_string($xmlString);
                   
        $json = json_encode($xmlObject);
        $phpArray = json_decode($json, true); 
   
        dd($phpArray);
    }
}

Anda akan melihat output di bawah ini:

Array
(
    [food] => Array
        (
            [0] => Array
                (
                    [name] => Belgian Waffles
                    [price] => $5.95
                    [description] => Two of our famous Belgian Waffles with plenty of real maple syrup
                    [calories] => 650
                )
            [1] => Array
                (
                    [name] => Strawberry Belgian Waffles
                    [price] => $7.95
                    [description] => Light Belgian waffles covered with strawberries and whipped cream
                    [calories] => 900
                )
            [2] => Array
                (
                    [name] => Berry-Berry Belgian Waffles
                    [price] => $8.95
                    [description] => Light Belgian waffles covered with an assortment of fresh berries and whipped cream
                    [calories] => 900
                )

            [3] => Array
                (
                    [name] => French Toast
                    [price] => $4.50
                    [description] => Thick slices made from our homemade sourdough bread
                    [calories] => 600
                )
            [4] => Array
                (
                    [name] => Homestyle Breakfast
                    [price] => $6.95
                    [description] => Two eggs, bacon or sausage, toast, and our ever-popular hash browns
                    [calories] => 950
                )
        )
)

Anda dapat memeriksa dengan kode ini.

Saya harap ini dapat membantu Anda ...

 

conver xml file to array laravel laravel