Friday, March 3, 2017

How to handle nested arrays in PHP (Laravel)

I have a data in array format like this :

    Array
    (
        [0] => Array
            (
                [@attributes] => Array
                    (
                        [Domain] => domain1.com
                        [Available] => true
                        [ErrorNo] => 0
                        [Description] => 
                        [IsPremiumName] => false
                        [PremiumRegistrationPrice] => 0
                        [PremiumRenewalPrice] => 0
                        [PremiumRestorePrice] => 0
                        [PremiumTransferPrice] => 0
                        [IcannFee] => 0
                        [EapFee] => 0
                    )

            )

        [1] => Array
            (
                [@attributes] => Array
                    (
                        [Domain] => domain2.com
                        [Available] => true
                        [ErrorNo] => 0
                        [Description] => 
                        [IsPremiumName] => false
                        [PremiumRegistrationPrice] => 0
                        [PremiumRenewalPrice] => 0
                        [PremiumRestorePrice] => 0
                        [PremiumTransferPrice] => 0
                        [IcannFee] => 0
                        [EapFee] => 0
                    )

            )

        [2] => Array
            (
                [@attributes] => Array
                    (
                        [Domain] => domain3.com
                        [Available] => true
                        [ErrorNo] => 0
                        [Description] => 
                        [IsPremiumName] => false
                        [PremiumRegistrationPrice] => 0
                        [PremiumRenewalPrice] => 0
                        [PremiumRestorePrice] => 0
                        [PremiumTransferPrice] => 0
                        [IcannFee] => 0
                        [EapFee] => 0
                    )

            )
)

I want to use this data on my blade view in Laravel.
So I did this :

@foreach($results as $result => $datas)
                        @foreach($datas as $data => $attributes )
                            @foreach($attributes as $attribute => $value)
                                <tr>
                                    @foreach($attribute["Domain"] as $domain)
                                    <td></td>
                                    @endforeach
                                    <td>.COM</td>
                                    <td>.NET</td>
                                    <td>.ORG</td>
                                </tr>
                            @endforeach
                        @endforeach
                    @endforeach

But when I use it like : it says :

illegal string offset ...

And when I use it like : it says

Trying to get property of non-object ...

I don't know what else should I try! please help me. thanks.
P.S: I know , It's not necessary to use four foreachs here. There should be a better way to do this.



via M. Safari

Advertisement