when i print "print_r($node)" in the node.tpl.php. i get this.
taxonomy] => Array (
[1] => stdClass object (
[tid] =>1
[vid]=>1
[name]=>cms
............)
)
so from the above, I know the taxonomy is an array. the array's value is an object. That the question comes. I looked up the php manual again and again, didn't find there is a saying "the array's value can be a object" why,I can't follow the above's code well. Hope someone can explain it to me. Thank you.
What Pekka write:
echo $node["taxonomy"][1]->tid;
Is not wrong, in the sense that it works for the above example. However, since you are doing this in the node.tpl.php you probably want something more robust, than this, since it only works for nodes with the term that has id 1.
Since the array of taxonomy terms is of the format:
array(tid => term_object)
You need to know the tid to access the term object. If you however want the tid, you can just get the array key:
$tids = array_keys($node["taxonomy"]);
Now, you don't know if or how many terms there is associated to your node, as it can be changed via settings, if you did:
if (!empty($node["taxonomy"])) {
$tids = array_keys($node["taxonomy"]);
$tid = tids[0];
}
You would get the tid of the first term (the one with the lowest tid). If you know from your setup that the node can only have 1 term and since the theme you're doing this in is site specific, this will be good enough for you. Else $tids will be an array of all the tids for the node that you use for your wishes.
in other words, in php, the array's value can be anything. am i right.
Yes, an array can hold values of any data type.
how the code i should write
The "path" to access the variable you show above would be:
echo $node["taxonomy"][1]->tid;
Related
I am facing a strange problem. It may just be a silly mistake, and I am just missing some basics.
I'm running php 5.6.1 on MAMP.
I have a simple array which I get from a mysql query. Using a foreach loop, I can print_r() each value, which gives me: stdClass Object ( [srno] => 6 [link] => this-is-link )
Now I can echo $obj->srno, and that prints fine. But I can't use echo $obj['srno'] which I was previously using, on an older version of PHP, but- It shows nothing.
Any help really appreciated. Thanks!
If you have a stdClass object and need to address it as an array, you can cast it to array quite easily:
$someObj = new stdClass();
$someObj->foo = "bar";
$someArray = (array)$someObj; // Cast the object to an array
echo $someArray['foo']; // Will give you "bar"
Working example: http://3v4l.org/nni1Y
Of course as comments already pointed out, you may want to look at retrieving your mysql results as an array in the first place.
As you said your results return as an object so you can use it by using
$obj->your_field_name to display field value. But your can not use by $obj['field_name'];
I have a quick question about something I imagine must be pretty easy - I've done a little research and found some links that seem promising, especially this, but it doesn't work for me for some reason.
Anyway, I made a stored procedure in MySQL and tested it with MySQL Workbench, and it works - it just adds num1 and num2 and returns the result. Now I'm trying to get it to work in PHP, but the result, instead of being an integer, is an array of one stdClass Object which contains that integer. That makes sense from the point of view of procedures that return a lot of data, but I'm having some trouble getting down to just the integer.
I run this:
CALL database.routine(2,7)
And I save the results into $var. When I run print_r($var), I get:
Array
(
[0] => stdClass Object
(
[num1+num2] => 9
)
)
So, to get past the Array part, I specifically asked for the first element in it, by running print_r($var[0]), which gets me:
stdClass Object
(
[num1+num2] => 9
)
And now I need to go one level deeper...I tried what the page I linked to above said and attempted to get to $var[0]->[num1+num2], as the field appears to be named, but that doesn't work. I've also tried a few combinations of single quotes and double quotes, but no luck. How do I get the number 9 out of this object?
Try...
$var[0]->{'num1+num2'}
Try this
$var[0]->{"num1+num2"}
or
$prop = 'num1+num2';
$var[0]->$prop;
Did you try this?
$var[0]->{'num1+num2'}
Try the following $var[0]->{'num1+num2'}
This is crazy but it might just work!
$var[0]->{'num1+num2'}
Im trying to list out here how to match strings that looks like array printr.
variable_data[0][var_name]
I would like to get from above example 3 strings, variable_data, 0 and var_name.
That above example is saved in DB so i same structure of array could be recreated but im stuck. Also a if case should look up IF the string (as above) is in that structure, otherwise no preg_match is needed.
Note: i dont want to serialize that array since the array 'may' contain some characters that might break it when unserializing and i also need that value in the array to be fully visible.
Any one with regexp skills who might know the approach ?
Solution:
(\b([\w]*[\w]).\b([\w]*[\w]).+(\b[\w]*[\w]))
Thos 2 first indexes should be skipped... but i still get what i want :)
Not for nothing but couldn't you just do..
$result = explode('[', someString);
foreach ($result as $i => $v) {
$temp = str_replace(']'. ''. $result[$i]);
//Do something with temp
}
Obviously you need to edit the above a little bit depending on what you are doing but it is very simple and even gives you the same flexibility and you don't need to invoke the matching engine...
I don't think we build regex's here for people... instead please see http://regexpal.com/ for a Regex tester / builder with visual aid.
Furthermore people usually don't know how to use them properly which is then fostered by others creating the expressions for them.
Please remember complex expressions can have terrible performance overheads although there is nothing seemingly complex about your request...
Then after it is compelte post your completed RegEx and answer your own question for maximum 1337ne$$ :)
But since I am nice here is your reward:
\[.+\]\[\d+\]
or
[a-z]+_[a-z]+\[.+\]\[\d+\]
Depending on what you want to match out of the string (which you didn't specify) so I assumed all
Both perform as follows:
arr_var[name][0]; //Matched
arr_var[name]; //Not matched
arr_var[name][0][1];//Matched
arr_var[name][2220][11];//Matched
Again, test them and understand with visual aid at the above link.
Solution:
(\b([\w]*[\w]).\b([\w]*[\w]).+(\b[\w]*[\w]))
Those 2 first indexes should be skipped... but i still get what i want :)
Edit
Here is improved one:
$str = "variable[group1][parent][child][grandchild]";
preg_match_all('/(\b([\w]*[\w]))/', $str,$matches);
echo '<pre>';
print_r($matches);
echo '</pre>';
// Output
Array
(
[0] => variable
[1] => group1
[2] => parent
[3] => child
[4] => grandchild
)
Ok so I am stuck with this xml in PHP stuff. I have gotten pretty far considering its my first 3 hours into XML all together ever in my entire life.
I am having trouble pulling data from a XML thing that has # in the name. See below (obviously im not going to post the whole XML thing but u can see how i got there below that.
SimpleXMLElement Object
(
[#attributes] => Array
(
[date] => 2010-09
[reserved] => 6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30
)
)
How i got there:
echo $this->General_functions->naked($xml->property[0]->availability->month[0]);
General_functions->naked is just a fast function to wrap and print_r around the given attribute.
My question is, HOW do i get the values inside #attributes cause no matter what i try i cant figure it out. Ive searched the web for a good 45 mins with no real answer.
Thanks in advance.
David
You need to use the attributes() method to get the results as another class. So, for example, to get the date attribute:
$myElement->attributes()->date
Also note that it's not a string, it's a SimpleXML attribute. If you want to get its actual value, you need to cast it to string explicitly:
(string)$myElement->attributes()->date
Access attributes of an element just as you would elements of an array:
(string) $xml->property[0]->availability->month[0]['date']
Edited to add the cast.
let's assume we have an array like this
$arr=array(array('a'=>1,'b'=>2),array('c'=>3,'d'=>4));
and a reference to one of its elements
$element=&$arr[1]['c'];
My question is is it possible to get back to the original array using the reference alone?
That is to get back to the parent array in some way without knowing it by name... This would be useful to me in a more complex scenario.
No, it's certainly not possible. Being a "reference" (as PHP calls it; it's actually a copy inhibitor) doesn't help at all in that matter. You'll have to store the original array together with the element.
$elArrPair = array(
"container" => $arr,
"element" => &$arr[1]['c'],
);
This way you can change the element with $elArrPair["element"] = $newValue and still be able to access the container.
You cannot get from $element to $arr. You can use in_array() of course, but nothing about $element contains a reference to $arr.
You copy the contect from one variable to another, nothing else, there is no connection between the two variables.