Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch object data from string stored in variable

Tags:

object

php

class

When object name ( category ) put directly it returns correct value Like:

 $primary = $this->dbmapper->category['primary'] ;  // ( Correct Output )

But when , put object name in a variable called $dataname, it returns blank, like:

 $dataname = 'category';
 $primary = $this->dbmapper->$dataname['primary'] ;   ( Blank Output )

my constructor variable is:

  $this->dbmapper = $this->mapper();

My function is:

  function mapper($module='')
    {
        $mapper =  array();
        $mapper['category']['table'] = 'allcategory';
        $mapper['category']['primary'] = 'categoryID';
        $mapper['page']['table'] = 'allpages';
        $mapper['page']['primary'] = 'pageID';
        return (object) $mapper;
    }
like image 700
Alok Jha Avatar asked Dec 18 '16 17:12

Alok Jha


People also ask

How do you store fetch data in a variable?

Approach: First make the necessary JavaScript file, HTML file and CSS file. Then store the API URL in a variable (here api_url). Define a async function (here getapi()) and pass api_url in that function. Define a constant response and store the fetched data by await fetch() method.

How do I return a fetch response?

How do I return a response on Fetch? Use the fetch() method to return a promise that resolves into a Response object. To get the actual data, you call one of the methods of the Response object e.g., text() or json() . These methods resolve into the actual data.


1 Answers

To access an object property(especially if it's an array) via variable enclose it with braces:

...
$dataname = 'category';
$primary = $this->dbmapper->{$dataname}['primary'];
like image 166
RomanPerekhrest Avatar answered Sep 30 '22 17:09

RomanPerekhrest