php appending string to stdClass object - php

I'm pulling records from database, and I have a filed called content_fr
the _fr is being dynamicaly created based on a site language.
Getting all records from the table gives me of course content_fr field. What I need to do is manually assign the suffix in this case _fr if I try to concatenate $row->content.'_fr' I get the error Notice: Undefined property: stdClass::$content, which makes sense since $content does not exist, but $content_fr does. Using standard arrays I was able to do it like $row['content_fr'], and worked fine, but now using stdClass I'm getting an error.
How can I convert $row->content.'_fr' into one string, so that php sees it as $row->content_fr ?

Try it like this :
$row -> {'content_' . $language}; // assuming $language = 'fr'

You can do it as follows:
$row->{'content_fr'};
EDIT: By the way, this question has been asked here many times. See previous threads such as this one: Accessing object's properties like if the objects were arrays.

Try something like this:
$lang = 'fr';
$property = 'content_' . $lang; // content_fr
$row->$property = 'something';

Related

Unable to get dynamic PHP's Object value using PHP variable

PHP is unable to get the value for dynamic object prepared as:
$abc->{$dynamic_object_pattern}
Where the value of the variable $dynamic_object_pattern is, json->{'data_1'}->{'value'}
For me, PHP 7.1 is understanding the statically defined pattern like below, and fetching the value as it should:
$abc->json->{'data_1'}->{'value'}
But not when I put the whole portion into a variable and then try to get its value. I Tried,
$abc->{$dynamic_object_pattern} and $abc->$dynamic_object_pattern
both ways, but no solution yet.
The error comes is Notice: Undefined property: stdClass::$json->{'data_1'}->{'value'}
I'm attempting an answer without seeing your JSON data
Here you say :
But not when I put the whole portion into a variable and then try to
get its value
From that line alone it sounds like you are trying to get value from a string rather than array. If you put the whole portion into a variable, PHP will interpret it as string. Make sure you add array() before newly created variable.
Natural array :
$array = array();
Now a string
$variable = $array;
Convert string to array
$new_array = array($variable);
Also, have you tried decoding?
// decode
$response = json_decode($new_array, true);
//print out
var_export(array_unique(array($response)));

How i can writte this in php for strings and no give me error

$array_names=array("phone","car","house");
I have this string: $phone_[$jr]
But the string with string called phone it's a name for value, i want use other names for get values, by this it´s necessary write using as this :
${$phone}
The problem it´s i need use other thing more in this string :
${$phone}_[$jr]
And this give me error
I see the problem it´s with "_" but i need use, my question it´s about how i need writte right for works
${$phone}_[$jr]
In other cases i need use :
${$house}_[$jr]
Etc, .....
Thank´s
I assume that you have a scenario like this:
// this is the array you are trying to access dynamically
$bar_ = [];
// this will hold the first part of the variable name
$phone = 'bar';
// and this is how you would build the string which is the full name of the array
// You can then use it to access one of its elements.
${$phone.'_'}[1] = 'f';

Merge two PHP tags

I want to convert two different PHP tags into a single PHP tag.This may sound a little weird but recently I found a question like this and answer with correct marked.
I can't find I'm currently lost address.
My question:
for example;
$gates = array('t1','t2','t3',t4','t5');
$openGates-> and $gates merge.
Result:
$openGates->t1; or t2,t3.
If I remember correctly, the answer to the problem I found before was;
$openGates->{$gates}; like this. I not sure
How can I do that?
You seem to be confusing objects with arrays. Arrays simply contain data, unless you create them with keys also. Such as:
$gates = array('t1'=>true,'t2'=>false,'t3'=>"maybe",'t4'=>0,'t5'=>50);
Matthew Page is correct in that you should look up PHP OOP for a solution.
That being said, you can cast your array to an object providing it has both keys and values:
$gates = (object) array('t1'=>true,'t2'=>false,'t3'=>"maybe",'t4'=>0,'t5'=>50);
or
$openGates = (object) $gates;
This will allow you to access 'properties' of the object in the way you have demonstrated:
echo $openGates->t1;, for example. The -> operator only applies to objects, which are instances of classes, not arrays.
If you do cast your array to the type of object, be sure that you have both keys and values.
It's not simple for newbie programmer...
At first:
$gates = array('t1','t2','t3','t4','t5');
It's Array
$openGates->
This is class instance. Btw. You can retrieve class instance var like $className->varName
You can't simple merge array and class instance. But you can create new class instance variables by loop.
foreach($gates as $gateKey=>$gateVal) {
$openGates->$gatesVal = NULL;
}
But I think it's, in result should be like that:
$gates = array('t1'=>'opened','t2'=>'closed','t3'=>'closed','t4'=>'opened','t5'=>'opened');
foreach($gates as $gateKey=>$gateVal) {
$openGates->$gateKey = $gateVal;
}
echo $openGates->t1;
// or
foreach($gates as $key=>$val) {
echo $openGates->$key.PHP_EOL;
}
Btw you can simple set $openGates->gates = $gates; and call it like echo $openGates->gates['t1'];

php: accessing array dynamically using a string variable

Its like this
I have a variable that has an array index in it, for e.g.
$var = 'testVar["abc"][0]';
or
$var = 'testVar["xyz"][0]["abc"]';
or it could be anything at run time.
Now when I try to access this by using this php code:
echo $$var;
or
echo ${$var};
I get a warning saying Illegal offset at line ...
but if I use this code, it works
eval('echo $'.$var);
I do not want to use eval(). Is there any other way?
EDIT:
The variable $testVar is an array build up on runtime and it could have multi-dimensional array built dynamically. Its format is not fixed and only the script knows by the use of certain variables that what the array could be.
for e.g. at any point, the array might have an index $["xyz"][0]["abc"] which I want to access dynamically.
My php version is 5.1
According to the documentation, what you are trying to accomplish is not possible:
In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write $$a[1] then the parser needs to know if you meant to use $a[1] as a variable, or if you wanted $$a as the variable and then the [1] index from that variable. The syntax for resolving this ambiguity is: ${$a[1]} for the first case and ${$a}[1] for the second.
In your case, $$var tries to read a variable with the name testVar["xyz"][0]["abc"], and not indexing an array. You could dereference that array like this:
$a = "testVar";
echo ${$a}["xyz"][0]["abc"];

Navigating array and objects data

I have an object that has a property link. I get it with getLink(). This property is actually an array so I get the 1st element $link[0] which in turn is an object so I get the property getHref(). My total code looks like this.
$link = $obj->getLink();
$link0 = $link[0];
$href = $link0->getHref();
Is there a better way to write this code, like in 1 line? something like $obj->getLink()->get(0)->getHref() I'm using PHP 5.3.4
current($obj->getLink())->getHref()
That's not very elegant though.
Your code should probably look like this:
$links = $obj->getLink();
$href = $links[0]->getHref();
If getLink always returns an array it should be called getLinks (plural) and I would expect to have to iterate over the return value; working only with the first result seems weird. Alternatively, getLink could accept a parameter to return a specific link, e.g.:
$href = $obj->getLink(0)->getHref();
PHP doesn't support direct return value dereferencing, so either the object interface is not very well suited for the language it's written in or you're not using the object as expected.

Categories