1

I have the following array. I am just showing number 0 to reduce the post lenght.

Array ( [0] => stdClass Object ( [type] => vector [id] => somecoolid [name] => aa_filename [center] => Array ( [0] => 139.34831290409 [1] => 38.282884929937 [2] => 2 ) [created] => 2022-12-04T01:42:56.051Z [modified] => 2022-12-04T01:44:47.634Z [visibility] => private [description] => some description [filesize] => 260139 [status] => available [tileset_precisions] => stdClass Object ( [10m] => 1608318 ) [created_by_client] => CI ) [1] => stdClass Object ...snip... 

The entire array is assigned to $getBodyTilesets. To access most of the data, I use the following loop;

<?php foreach ($getBodyTilesets as $getBodyTileset) : ?> <?= $getBodyTileset->id ?> <?= $getBodyTileset->type ?></td> <?= $getBodyTileset->name ?></td> <?= $getBodyTileset->center[0] ?> <?= $getBodyTileset->center[1] ?> <?= $getBodyTileset->center[2] ?></td> <?= $getBodyTileset->created ?></td> <?= $getBodyTileset->modified ?></td> <?= $getBodyTileset->visibility ?></td> <?= $getBodyTileset->description ?></td> <?= $getBodyTileset->filesize ?></td> <?= $getBodyTileset->status ?></td> <?php endforeach; ?> 

I cannot understand how to access tileset_precisions. The above says [10m] but this data will always change to something different like [5m] or [3cm] or [71.m] etc. How would one best access that data pls?

1
  • this data will always change to something different like [5m] or [3cm] or [71.m] etc...yuk. Can you ask whoever created this data to make a more sensible structure? P.S. If you decoded this from JSON, you could ask json_decode to decode it as an associative array, that would make this easier. Failing that, you probably need to look here, or you can try casting the object to an array on-demand Commented Dec 21, 2022 at 14:05

1 Answer 1

2

When tileset_precisions always has one element, then

<?= array_values((array)$getBodyTileset->tileset_precisions)[0] ?></td> 

or

<?= current((array)$getBodyTileset->tileset_precisions) ?></td> 

If you also want the key e.g 10m:

<?= key((array)$getBodyTileset->tileset_precisions) ?></td> 
Sign up to request clarification or add additional context in comments.

2 Comments

@ADyson I thought data is refered to the value of the element.
Sorry I misread, already deleted that comment!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.