lundi 7 janvier 2019

Drilling down into an array in PHP

I have a Laravel install with Moloquent (Mongo). Mongo ins't necessarily the problem, when the model loads the "JSON" record, it becomes a PHP associative array. I need to be able to create a function in a model that returns a value of an array element by a string without knowing if the element exists.

for example:

$search1 = 'folder1/folder2/folder3/item';
//would look like: $array['folder1'][folder2'][folder3']['item']
$search2 = 'folder1/picture1/picture'; 
//would look like: $array['folder1'][picture1']['picture']

echo recRecord($search1);
echo recRecord($search2);    

function getRecord($a='') {
  //would use explode here?
  return $result;
}

I guess I could use the ?? operator, but I am looking for a better way.

Thank you.



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2FaXFzf
via IFTTT

Aucun commentaire:

Enregistrer un commentaire