PHP/Drupal - Pound sign on array - php

Hi
When I print out the variable, I got pound sign on the array as below:
...
[#weight] => 0
[#theme] => text_formatter_default
[#field_name] => field_product_item_no
[#type_name] => product
...
What is the meaning of Pound sign?
Thanks

This is just the way the Drupal Forms API expects its data.

In Drupal, [#key]'s value is metadata, whereas [key]'s value is data.
According to the answer given to my question about "#" properties on the Drupal Stackexchange,
Put simply, array keys in a render array without a # in front of the name are seen as children of the render array, and are subsequently rendered themselves (recursively).
Those with a # in front of theme are seen as meta data/variables for the render array to use as necessary, and are not themselves rendered.

The pound sign is just a valid character as array key and has no special meaning in PHP besides a convention defined by the application.

Related

how can I use mongodb array(simple and embedded array) elements as php indexed array [duplicate]

I googled, installed Devel, Drupal for Firebug, but I can't find it.
I found what I want, I know where it is; I just don't know how to get it.
I'll put this in code brackets, but Devel tells me the file name (which I want to stick into the .tpl.php file) is here:
field_image (Object) stdClass
handler (Object) views_handler_field_field
view (Object) view
result (Array, 2 elements)
0 (Object) stdClass
_field_data (Array, 1 element)
nid (Array, 2 elements)
entity (Object) stdClass
field_image (Array, 1 element)
und (Array, 1 element)
0 (Array, 11 elements)
filename (String, 23 characters ) FILENAME.jpg
So, how do I get that FILENAME.jpg to be output using PHP?
<?php print $something->other; ?>
Whenever you need to read a value out of a variable, you need to know which expression you need to formulate to access that value.
For a simple variable value this is simple, you just take the variable name and access it as a variable by prefixing it with the $ sign:
var_dump($variable);
This is documented here.
However this does only work for simple datatypes like string or integer. There are as well compound datatypes, namely array and object. They can contain further datatypes, be it simple or compound. You can learn in the PHP manual how to access the values of an array and how you can access them from an object. I think you already know of that a bit, so just for having it linked here.
When you have learned about that, you can then combine this. E.g. if there is an array within an object and therein is a string you would like to get, you need to combine the $ sign and the variable name with the needed accessors, property names and array keys. Then you get your value. The data you have posted shows that you have an object that has some other objects and arrays and in the end you find the variable name.
Some combination example:
var_dump($variable->handler->view[0]->_field_data);
This is based on the data you've provided above. $variable is where you start, -> is used to access object members which need to be named then (like a name for a variable) : handler. As you've seen in your debug output that handler is an object, you need to use again the -> to access the view member of it.
Now view is different because it's an array. You access values of an array by using [] and putting the key in there. The key in my example is a number, 0. And as the value of that array entry is an object again, in the next step you need to use -> again.
You can continue this game until you reach the element that you're interested in. The debug output you already have helps you to write the expression that returns the value. Possibly it is:
$field_image->handler->view->result[0]->_field_data['nid']['entity']->field_image['und'][0]['filename']
But I can not validate that here on my system in full.
However when finding things out, it's helpful to make use of var_dump as you could step by step extend the expression until you find the element. If you make an error you will immediately see. Sometimes it helps to place a die(); after the var_dump statement so not to end the response before it contains to much other data that will hide the information from you. The devel plugin offers additional debug routines to dump values prominent.
If this is your object:
field_image (Object) stdClass
handler (Object) views_handler_field_field
view (Object) view
result (Array, 2 elements)
0 (Object) stdClass
_field_data (Array, 1 element)
nid (Array, 2 elements)
entity (Object) stdClass
field_image (Array, 1 element)
und (Array, 1 element)
0 (Array, 11 elements)
filename (String, 23 characters ) FILENAME.jpg
I'd guess you can find it using:
field_image->handler->view->result[0]->_field_data['nid'][entity]->field_image['und'][0]['filename]
Could be a mistake in there, but the general Idea is: if you have an object, get the variable using ->, and if you have an array, use [key].
Let's say you have a node object in $node. You can print it's values very nice with:
dpm($node); // remember this function is declared in devel module
Then you can see the information from $node and expand the internal fields with a click. And with a double click on the field you can see it's php path.
You'll get this result:
Hope that helps!
PD: I guess this functionality isn't available on D6's dpm.
Try:
$field_image->handler->view->result[0]->_field_data['nid']['entity']->field_image['und'][0]['filename']
If you have devel installed and try
krumo ($variable);
Just bear in mind as default only admin users have rights to use the krumo command, but this can be sorted out by looking at the DEVEL role permissions. (don't forget to remove these permissions once your done though)
<? print_r($something["other"]); ?>
(where other is this)
so result is 'this'
Let me summarize up
print_r($data); => Traditional view of printing array.
var_dump($data); => Not so much cleaned view , gives you everything but in very suffocated manner
print "<pre>"; print_r($data); => A cleaned view but will not get data types information.
dpm($data); => It gives you everything, but you need to have installed devel.
You should use field_view_field($entity_type, $entity, $field_name, $display = array(), $langcode = NULL) , that will return renderable array . You can check api document https://api.drupal.org/api/drupal/modules%21field%21field.module/function/field_view_field/7.x
If you can't use devel module for some reason, another useful "debug" functions could be var_export() and a Drupal wrapper drupal_var_export(). These functions gives the output as PHP code.

PHP find needle in array stack [duplicate]

I googled, installed Devel, Drupal for Firebug, but I can't find it.
I found what I want, I know where it is; I just don't know how to get it.
I'll put this in code brackets, but Devel tells me the file name (which I want to stick into the .tpl.php file) is here:
field_image (Object) stdClass
handler (Object) views_handler_field_field
view (Object) view
result (Array, 2 elements)
0 (Object) stdClass
_field_data (Array, 1 element)
nid (Array, 2 elements)
entity (Object) stdClass
field_image (Array, 1 element)
und (Array, 1 element)
0 (Array, 11 elements)
filename (String, 23 characters ) FILENAME.jpg
So, how do I get that FILENAME.jpg to be output using PHP?
<?php print $something->other; ?>
Whenever you need to read a value out of a variable, you need to know which expression you need to formulate to access that value.
For a simple variable value this is simple, you just take the variable name and access it as a variable by prefixing it with the $ sign:
var_dump($variable);
This is documented here.
However this does only work for simple datatypes like string or integer. There are as well compound datatypes, namely array and object. They can contain further datatypes, be it simple or compound. You can learn in the PHP manual how to access the values of an array and how you can access them from an object. I think you already know of that a bit, so just for having it linked here.
When you have learned about that, you can then combine this. E.g. if there is an array within an object and therein is a string you would like to get, you need to combine the $ sign and the variable name with the needed accessors, property names and array keys. Then you get your value. The data you have posted shows that you have an object that has some other objects and arrays and in the end you find the variable name.
Some combination example:
var_dump($variable->handler->view[0]->_field_data);
This is based on the data you've provided above. $variable is where you start, -> is used to access object members which need to be named then (like a name for a variable) : handler. As you've seen in your debug output that handler is an object, you need to use again the -> to access the view member of it.
Now view is different because it's an array. You access values of an array by using [] and putting the key in there. The key in my example is a number, 0. And as the value of that array entry is an object again, in the next step you need to use -> again.
You can continue this game until you reach the element that you're interested in. The debug output you already have helps you to write the expression that returns the value. Possibly it is:
$field_image->handler->view->result[0]->_field_data['nid']['entity']->field_image['und'][0]['filename']
But I can not validate that here on my system in full.
However when finding things out, it's helpful to make use of var_dump as you could step by step extend the expression until you find the element. If you make an error you will immediately see. Sometimes it helps to place a die(); after the var_dump statement so not to end the response before it contains to much other data that will hide the information from you. The devel plugin offers additional debug routines to dump values prominent.
If this is your object:
field_image (Object) stdClass
handler (Object) views_handler_field_field
view (Object) view
result (Array, 2 elements)
0 (Object) stdClass
_field_data (Array, 1 element)
nid (Array, 2 elements)
entity (Object) stdClass
field_image (Array, 1 element)
und (Array, 1 element)
0 (Array, 11 elements)
filename (String, 23 characters ) FILENAME.jpg
I'd guess you can find it using:
field_image->handler->view->result[0]->_field_data['nid'][entity]->field_image['und'][0]['filename]
Could be a mistake in there, but the general Idea is: if you have an object, get the variable using ->, and if you have an array, use [key].
Let's say you have a node object in $node. You can print it's values very nice with:
dpm($node); // remember this function is declared in devel module
Then you can see the information from $node and expand the internal fields with a click. And with a double click on the field you can see it's php path.
You'll get this result:
Hope that helps!
PD: I guess this functionality isn't available on D6's dpm.
Try:
$field_image->handler->view->result[0]->_field_data['nid']['entity']->field_image['und'][0]['filename']
If you have devel installed and try
krumo ($variable);
Just bear in mind as default only admin users have rights to use the krumo command, but this can be sorted out by looking at the DEVEL role permissions. (don't forget to remove these permissions once your done though)
<? print_r($something["other"]); ?>
(where other is this)
so result is 'this'
Let me summarize up
print_r($data); => Traditional view of printing array.
var_dump($data); => Not so much cleaned view , gives you everything but in very suffocated manner
print "<pre>"; print_r($data); => A cleaned view but will not get data types information.
dpm($data); => It gives you everything, but you need to have installed devel.
You should use field_view_field($entity_type, $entity, $field_name, $display = array(), $langcode = NULL) , that will return renderable array . You can check api document https://api.drupal.org/api/drupal/modules%21field%21field.module/function/field_view_field/7.x
If you can't use devel module for some reason, another useful "debug" functions could be var_export() and a Drupal wrapper drupal_var_export(). These functions gives the output as PHP code.

Accessing specified Element of Array with Twig

I have the following array and i want to access the value of a specified element with twig.
numbers => Array ([01234567] => Array ( [0] => 9876543210 [1] => 8765432109 [2] => 0000000000))
I know there is only one entry in numbers, so I want to access the array with the key 01234567 directly.
Even tough numbers|keys[0] does return the correct key, I can't use it like numbers[numbers|keys[0]] to get the array. I also tried the attribute(array, item) function, but i didn't got it to work.
Is it possible to access it directly or do I need to use loops?
You have found a probably undocumented "feature" of Twig. If you check the source code, twig tries to determine if the given key is numeric, or not. It does this check with the ctype_digit function, which checks if a variable contains only numeric characters.
The example in your question contains an array key, which meets this conditions: it contains only numbers. Unfortunately, it also starts with a zero, which is removed when the string is converted into an integer.
I'm not exactly sure that this is intended behavior, so you may try to report this example as a bug.
For the current twig implementation, because everything except the loop construct uses the getAttribute function, you have no other choice but to use a for loop.

Able to see a variable in print_r()'s output, but not sure how to access it in code

I googled, installed Devel, Drupal for Firebug, but I can't find it.
I found what I want, I know where it is; I just don't know how to get it.
I'll put this in code brackets, but Devel tells me the file name (which I want to stick into the .tpl.php file) is here:
field_image (Object) stdClass
handler (Object) views_handler_field_field
view (Object) view
result (Array, 2 elements)
0 (Object) stdClass
_field_data (Array, 1 element)
nid (Array, 2 elements)
entity (Object) stdClass
field_image (Array, 1 element)
und (Array, 1 element)
0 (Array, 11 elements)
filename (String, 23 characters ) FILENAME.jpg
So, how do I get that FILENAME.jpg to be output using PHP?
<?php print $something->other; ?>
Whenever you need to read a value out of a variable, you need to know which expression you need to formulate to access that value.
For a simple variable value this is simple, you just take the variable name and access it as a variable by prefixing it with the $ sign:
var_dump($variable);
This is documented here.
However this does only work for simple datatypes like string or integer. There are as well compound datatypes, namely array and object. They can contain further datatypes, be it simple or compound. You can learn in the PHP manual how to access the values of an array and how you can access them from an object. I think you already know of that a bit, so just for having it linked here.
When you have learned about that, you can then combine this. E.g. if there is an array within an object and therein is a string you would like to get, you need to combine the $ sign and the variable name with the needed accessors, property names and array keys. Then you get your value. The data you have posted shows that you have an object that has some other objects and arrays and in the end you find the variable name.
Some combination example:
var_dump($variable->handler->view[0]->_field_data);
This is based on the data you've provided above. $variable is where you start, -> is used to access object members which need to be named then (like a name for a variable) : handler. As you've seen in your debug output that handler is an object, you need to use again the -> to access the view member of it.
Now view is different because it's an array. You access values of an array by using [] and putting the key in there. The key in my example is a number, 0. And as the value of that array entry is an object again, in the next step you need to use -> again.
You can continue this game until you reach the element that you're interested in. The debug output you already have helps you to write the expression that returns the value. Possibly it is:
$field_image->handler->view->result[0]->_field_data['nid']['entity']->field_image['und'][0]['filename']
But I can not validate that here on my system in full.
However when finding things out, it's helpful to make use of var_dump as you could step by step extend the expression until you find the element. If you make an error you will immediately see. Sometimes it helps to place a die(); after the var_dump statement so not to end the response before it contains to much other data that will hide the information from you. The devel plugin offers additional debug routines to dump values prominent.
If this is your object:
field_image (Object) stdClass
handler (Object) views_handler_field_field
view (Object) view
result (Array, 2 elements)
0 (Object) stdClass
_field_data (Array, 1 element)
nid (Array, 2 elements)
entity (Object) stdClass
field_image (Array, 1 element)
und (Array, 1 element)
0 (Array, 11 elements)
filename (String, 23 characters ) FILENAME.jpg
I'd guess you can find it using:
field_image->handler->view->result[0]->_field_data['nid'][entity]->field_image['und'][0]['filename]
Could be a mistake in there, but the general Idea is: if you have an object, get the variable using ->, and if you have an array, use [key].
Let's say you have a node object in $node. You can print it's values very nice with:
dpm($node); // remember this function is declared in devel module
Then you can see the information from $node and expand the internal fields with a click. And with a double click on the field you can see it's php path.
You'll get this result:
Hope that helps!
PD: I guess this functionality isn't available on D6's dpm.
Try:
$field_image->handler->view->result[0]->_field_data['nid']['entity']->field_image['und'][0]['filename']
If you have devel installed and try
krumo ($variable);
Just bear in mind as default only admin users have rights to use the krumo command, but this can be sorted out by looking at the DEVEL role permissions. (don't forget to remove these permissions once your done though)
<? print_r($something["other"]); ?>
(where other is this)
so result is 'this'
Let me summarize up
print_r($data); => Traditional view of printing array.
var_dump($data); => Not so much cleaned view , gives you everything but in very suffocated manner
print "<pre>"; print_r($data); => A cleaned view but will not get data types information.
dpm($data); => It gives you everything, but you need to have installed devel.
You should use field_view_field($entity_type, $entity, $field_name, $display = array(), $langcode = NULL) , that will return renderable array . You can check api document https://api.drupal.org/api/drupal/modules%21field%21field.module/function/field_view_field/7.x
If you can't use devel module for some reason, another useful "debug" functions could be var_export() and a Drupal wrapper drupal_var_export(). These functions gives the output as PHP code.

UTF-8 not showing in multi-dimensional arrays with a NUMERIC key

I have a script that puts some cyrillic text into session variables like this: $_SESSION['cyrillic'][$y] where $y is an iterator, so in the end the array will look like this:
[cyrillic] => Array
(
[0] => ������
[1] => ��������
[2] => ������
[3] => ������
However, after struggling with UTF-8 headers in both PHP, HTML and files save charsets (with and without BOM), I've found out there is nothing wrong with the charset settings (apparently), as setting any other session variable with the cyrillic text in WILL WORK.
This means:$_SESSION['cyr']['txt'] = $cyrillic_string_here; will work just fine, as shown here:
[cyr] => Array
(
[txt] => десять
)
So, what I fail to understand then, is WHY can't I insert cyrillic into an array using a numeric key, but an alphabetical one? And how can I work around this when I -need- to use an iterating loop?
EDIT: It's not that. I can insert the same text into another variable and echo it, or a session variable without numeric key values and it will work fine.
EDIT: I also found this:
*The hard-to-find documentation sentence I'm about is: "The keys in the $_SESSION associative array are subject to the same limitations as regular variable names in PHP."*
But I thought I could make an array with a numeric value like this: $array[$i]? Or does it mean since the key is ASSOC that $array['text'][$i] wouldn't work?
Found the solution. I had a trim(strtolower($text)); wrapped around my cyrillic.
strtolower:
[These functions] only work when translation is between common characters in ISO 8859-1 and UTF-8, that means they will work well with western languages but not cyrillic or asian.
Thanks for your help.

Categories