Php query string to object

Convert a string to object in PHP (CodeIgniter)

Solution 1: You can store classnames or method names as strings and later use them, or even store variable names, like here (variable variables), but you cannot store whole logic. Try this: Additionally, I would recommend defining the model’s property as either static or constant, as it doesn’t change at run-time or for specific instances of the class: Furthermore, hard-coding array references to specific column names is somewhat counter-productive, unless you know that [0] will always be the ID, and [1] will always be the name (in which case, you might as well set a static prefix for the column names and use that instead of the array).

Convert a string to object in PHP (CodeIgniter)

Class Account extends CI_Model < private $tbl_rest = array(); private $tbl_fields = array('bs_id', 'bs_name', 'bs_type', 'bs_sub'); function get_data($dataid)< $this->db->select( '*' ); $this->db->from( $this->tbl_name ); $this->db->where( $this->tbl_key, $id ); $query = $this->db->get(); if( $query->num_rows() > 0 ) < foreach ($query->result() as $row) < $this->tbl_rest[] .= ' echo( json_encode( array( 'tdata' => $this->tbl_rest ) ) ); > else < echo( false ); >> > 

When I change $query->result() like this

' I starting getting error "object of class could not be converted to string" My Question is:
  1. Is possible to convert an array string to object?
  2. And how to make $row->$this->tbl_fields[0] happen, so I do not always have to write the name of the field.
Читайте также:  Количество вхождений символа строке java

This is a basic order of operations issue. PHP doesn’t know if you want some dynamic $this property of the row, or the $this->tbl_fields[0] property of the row.

tbl_rest[] .= ' Additionally, I would recommend defining the model's $tbl_fields property as either static or constant, as it doesn't change at run-time or for specific instances of the class:

Furthermore, hard-coding array references to specific column names is somewhat counter-productive, unless you know that [0] will always be the ID, and [1] will always be the name (in which case, you might as well set a static prefix for the column names and use that instead of the $tbl_fields array).

Better yet, because all you’re doing here is a key/value pair (I assume you want to re-use this pattern for other API methods. ), you could simply select the fields the way you want to use them. Here’s what I would recommend:

db->select(array( self::$col_prefix.'id AS key', self::$col_prefix.'name AS value' )); $this->db->from( $this->tbl_name ); $this->db->where( $this->tbl_key, $id ); $query = $this->db->get(); if( $query->num_rows() > 0 ) < foreach ($query->result() as $row) < $this->tbl_rest[] .= ' echo( json_encode( array( 'tdata' => $this->tbl_rest ) ) ); > else < echo( false ); >> > 

You should use search before asking

You could create an standard class variable (very basic object):

and loop through your array and re-assign the values

foreach ($array as $key => $value) < $object->$key = $value; > 

Php comma separated string to object?, Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question.Provide details and share your research! But avoid …. Asking for help, clarification, or responding to other answers.

Convert String To Object-Variable-Name?

i want to turn a simple string like that «response->dict->words» into a variable name that i can actually work with. I will give an example now. Lets assume the value of $response->dict->words is 67 .

$var = "response->dict->words" echo $$var; /* PRINT THE VALUE 67 FROM $response->dict->words*/ 

As you may notice i put an extra dollar sign before the $var because this should work, but it doesn’t.

Can anyone help me with this?

class ClassOne < public function test() < return 'test'; >> class ClassTwo < public function test2() < return 'test2'; >> $one = new ClassOne(); $two = new ClassTwo(); $objects = array('one', 'two'); $methods = array('test', 'test2'); for ($i = 0; $i < count($objects); $i++) < echo $->$methods[$i](); > 

You can store classnames or method names as strings and later use them, or even store variable names, like here $ (variable variables), but you cannot store whole logic.

To evaluate whole logic, you have to use eval() , which is most probably bad idea

$var = "response->dict->words" eval("?>  

You can split your string and make the call as below:

class Response < public $dict; public function __construct() < $this->dict = new stdClass(); $this->dict->words = 'words test'; > > $response = new Response(); $var = 'response->dict->words'; $elements = explode('->', $var); echo $->$elements[1]->$elements[2]; 

Or, if you don't know the level of nesting the object call, you can perform the call in a foreach loop. When the loop exits, the last call will be available after it:

class Response < public $dict; public function __construct() < $this->dict = new stdClass(); $this->dict->words = new stdClass(); $this->dict->words->final = 'test chained string'; > > $response = new Response(); $var = 'response->dict->words->final'; $elements = explode('->', $var); foreach ($elements as $key => $element) < if ($key == 0) < $call = $; continue; > $call = $call->$element; > echo $call; 

Results into: test chained string

There is a better way, why don't you cache the variable like

Convert XML to Object in PHP and again convert that, You actually don't need to load the SimpleXML object into that json_encode/decode. You can use that object already and parse whatever values you needed. Having to encode/decode as an array and to have to access values and then converting it into SimpleXML is too much.

Convert string back to ObjectID

I have converted a ObjectID to string which acts as a dropdown list value. When posted I need to take this string and convert it back to a ObjectID in the cleanest fashion.

What would be the best way of doing this?

It seems that MongoID class accepts a string in the constructor:

public MongoId::__construct ([ string $id = NULL ] ) 
id

A string to use as the id. Must be 24 hexidecimal characters. If an invalid string is passed to this constructor, the constructor will ignore it and create a new id value.

How to convert an array to object in PHP?, How to convert an array to object in PHP? Ask Question Asked 12 years, 8 months ago. Modified 1 year, 4 months ago. Viewed 967k times 453 98. How can I convert an array like this to an object? [128] => Array ( [status] => "Figure A. Facebook's horizontal scrollbars showing up on a 1024x768 screen resolution." ) [129] => …

Convert serialized string to object or array [duplicate]

I have this following serialized string received by a Laravel Controller from an ajax call:

"address=&city=&cp=&phone_domicile=" (it can be longer)

How can I convert it to a php Object or an Array using php or a Laravel helper function? to have this following:

address => . city => . phone_domicile => . 

Maybe it's an easy question but I can't figure out how to do it.

$query = 'address=&city=&cp=&phone_domicile='; parse_str($query,$queryStrToArray); print_r($queryStrToArray); 

Working example :- https://3v4l.org/Cg3qt

Php - Convert serialized string to object or array, Parse query string into an array (12 answers) Closed 2 years ago . I have this following serialized string received by a …

Источник

Extract query string into an associative array with PHP

A little while back I posted how to extract the domain, path, etc from a url with PHP and in this follow up post show how to extract the query string into an associative array using the parse_str function.

Extract the query string with parse_url

In this example we’ll look at the URL from querying [chris hope] at Google (I don’t show up until the second page, by the way) which looks like this:

http://www.google.com/search?hl=en&source=hp&q=chris+hope&btnG=Google+Search&meta=&aq=f&oq=

Using parse_url we can easily extract the query string like so:

$parts = parse_url($url); echo $parts['query'];

The output from the above will be this:

hl=en&source=hp&q=chris+hope&btnG=Google+Search&meta=&aq=f&oq=

As an aside, before continuing with using parse_str to extract the individual parts of the query string, doing print_r($parts) would show this:

Array ( [scheme] => http [host] => www.google.com [path] => /search [query] => hl=en&source=hp&q=chris+hope&btnG=Google+Search&meta=&aq=f&oq= )

Extract the query string parts with parse_str

The parse_str function takes one or two parameters (the second from PHP 4.0.3) and does not return any values. If the second parameter is present, the values from the query string are returned in that parameter as an associative array. If it is not present, they are instead set as variables in the current scope, which is not really ideal.

So, without the first parameter:

You could now echo the "q" value like this:

In my opionion, it’s better to have the values returned as an array like so:

parse_str($parts['query'], $query);

Now doing print_r($query) would output this:

Array ( [hl] => en [source] => hp [q] => chris hope [btnG] => Google Search [meta] => [aq] => f [oq] => )

The "q" value could now be echoed like this:

Follow up posts

Have a read of my post titled "PHP: get keywords from search engine referer url" to find out how to use the parse_url function in conjunction with the parse_str function to see what query string visitors have entered into a search engine.

Источник

Оцените статью