Home > front end >  How to access to specific data in variable position in object php
How to access to specific data in variable position in object php

Time:04-09

I am trying to return the value of an object with array in php laravel, what I am doing to access the information is the following, which works correctly, but the problem comes when the information I need changes position for example from 21 to 22 I can not access because obviously in the node in which I try to find the information does not exist

my object :

See example

in my blade.php:

   @foreach ($xml_objeto as $xml_objeto)
                    <tr>   
                    <td> {{ $xml_objeto[1]['Rfc'] }} </td> // works
                    <td> {{ current($xml_objeto)->UUID }} </td> // I tried but it does not work
                    <td>  {{ $xml_objeto[21]['UUID'] }} </td> // it works but when the data changes position from 21 to 22 it is no longer localized
                    <td>  {{ $xml_objeto[1]['Nombre'] }} </td>
                    </tr>
                    @endforeach 

it should be noted that it is the only data with that name UUID="" in my object. thank you very much for all the help you can give me.

CodePudding user response:

What does $xml_objeto[21]['UUID'] return when it is no longer localized?

If it comes back as an empty string, or anything else other than what you want - you could check the other index.

For example:

{{ $xml_objeto[21]['UUID'] == '' ? $xml_objeto[22]['UUID'] : $xml_objeto[21]['UUID']; }}

CodePudding user response:

You can try using null coalescing, it will print null or whatever you want if it doesn't find that index in the array

<td>  {{ $xml_objeto[$key]['UUID'] ?? null}} </td> 

or

<td>  {{ $xml_objeto[21]['UUID'] ?? $xml_objeto[22]['UUID']}} </td> 

  • Related