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.
Related
I am testing my code with PHP and using Hamcrest matchers and I am wanting to assert that my returned array contains a particular key/value pair, however I am struggling.
For example, my array returns ['wanted-value' => 'some-value', 'arbitrary-value' => 'who-cares']
I want to assert that the returned array contains 'wanted-value' => 'some-value' but I don't care what else is in there.
I have seen arrayContaining used, but from what I can tell this only looks for a value or key, but not both?
Who would have thought the official docs would have the answer?
To be honest, I didn't know PHP docs for Hamcrest actually existed, I'd always just seen it as JAVA-specific.
The solution: hasKeyValuePair
assertThat($result, hasKeyValuePair('wanted-value', 'some-value'));
I have a very simple array where I want to be able to add settings as simple as possible. In my case, if I don't need a second value, I don't need to add it.
Code
$settings = [
'my_string',
'my_array' => 'hello'
];
print_r($settings);
Result
The problem with my approach is that my_string is seen as a value and the key 0 is added.
Array
(
[0] => my_string
[my_array] => hello
)
I could check if the key is a number and in that case figure out that I don't use a second value.
Is there a better approach?
I'm aware of that I can use multiple nested arrays but then simplicity is gone.
The way you've written it, you wouldn't be able to access the value 'my_string' without some kind of key. There is always a key for any value, and if you don't specify values, PHP generates them as though they were indices in an actual array (PHP 'arrays' are actually dictionaries/maps/etc. that have default behavior in the absence of a specified key).
$settings = [
'name' => 'my_string',
'my_array' => 'hello'
];
If you don't want to use $settings[0] -- and I wouldn't want to either -- you should probably decide on a name for whatever 'my_string' is supposed to be and use that as its key.
I found what appears to be an abusable bug in the PHP language. When creating variable variables a seemingly illegal variable can be declared and then handled as long as you keep accessing it as a variable variable.
Example:
${'0'} = 1; //I am an illegal variable called $0
${'0'}++; //I can be accessed and manipulated.
var_dump(${'0'}); //Output: int(2)
This behavior appears rather odd. It is briefly mentioned in the official documentation for SimpleXml as a way to create variable names that contain hyphens but my example shows it can be abused pretty badly nonetheless.
I would like to know how come this behavior is possible and is tolerated when the code is parsed?
Internally PHP stores variables (zend_array* symbol_table) in a same data structure that is used for arrays. This allows you to have variable names with the same constraints as array keys.
Eg. Zend API function zend_set_local_var sets a value to symbol table using zend_hash_update, which is a function used to manipulate PHP array types as well.
We can't however allow every character in variable names in PHP source code. That's because lexical analysis must distinguish labels from other tokens. Variable variables offer a workaround for this.
It's not a bug nor a way to abuse anything. That being said, the documentation states that all labels, including variables, must have a regex form [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*, so i wouldn't rely on having arbitrary characters in variable names.
What ${'0'} = 1; actually does, it sets value 1 to the current scope's symbol table at key 0.
You can get that particular table with get_defined_vars function.
Looking at the source code, we'll see that the function just copies the current symbol table and returns it to caller.
PHP extract function (src) actually checks that keys have a valid label format (by calling php_valid_var_name), so you can't generate variables with funky names using that.
Anyway, even though it's possible to create variables of any name using variable variable syntax (even a variable with no name ${''}), i think it's bad practice. Even worse if a library expects or enforces you to do so. Saying it's a workaround might be a bit overstatement actually. Maybe it should be considered as an implementation detail and an undocumented feature.
#Nadav I don't have full idea about that but have a little bit idea,
Remember that curly braces({}) literally mean "evaluate what's inside the curly braces" so, you can squeeze the variable creation like you did ${'0'} = 1
reffered it from this http://php.net/manual/en/language.variables.php#73373
PHP stores variable in the array form, if you want to check then use $GLOBALS(an array) is the super global variable which has all the reference of the variables present in the script, it stores variable name as key and value as variable value, suppose below case:
<?php
${'0'} = 1; // here php stores it as 0th index in the array
${'1'} = 2; // here php stores it as 1th index in the array
$b = "I am b"; // here php stores it as bth index in the array
echo "<pre>";
print_r($GLOBALS);
output:
Array
(
[_GET] => Array
(
)
[_POST] => Array
(
)
[_COOKIE] => Array
(
)
[_FILES] => Array
(
)
[GLOBALS] => Array
*RECURSION*
[0] => 1
[1] => 2
[b] => I am b
)
I have PHP $_SESSION arrays that have an undefined amount of elements. Is it possible to initialise all the values to 0, or do I have to check whether the value is NULL and then set to 0 every time I check a value?
Edit: I'm sorry for the vagueness of my question.
I have an undefined amount of levels, and I'd like to store all the scores of each level in that array. Currently my amount of levels is fixed, so I am currently just writing:
$_SESSION['totals'] = array(0,0,0,0,0);
And then when adding manipulating the data, I simply increment/add a certain amount to that element.
Now I'd prefer to have the same ease of directly incrementing/adding values to certain elements without needing to check whether a value is NULL or something like that...
Edit 2: edited my code as follows:
$_SESSION['totals'] = array();
if(array_key_exists($row['level']-1,$_SESSION['totals'])){
$_SESSION['totals'][$row['level']-1]++;
}else{
$_SESSION['totals'][$row['level']-1] = 1;
}
And it seems to work. Thanks fellas!
You can use array_fill_keys function to fill an array with specified value for defined keys:
$keys = array('foo', 'bar', 'baz');
$output = array_fill_keys($keys, 0);
Defining an array with initial values is defining an array with a length. This does not prevent you from adding or removing elements from the array:
// initial array
$myArray = [0, 0, 0, 0];
print_r($myArray); // would output Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0)
$myArray[] = 1;
print_r($myArray); // would output Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 1 )
$_SESSION contains only what you put into it. Just make sure you add 0 instead of null the first time you add something.
If you need to do this later, I think your design might be bad. But anyway, $_SESSION is like a normal array, so you can just use PHP's array function to traverse the array and check or change each value.
PHP arrays aren't really arrays you might know from other languages. They really are more like linked lists with hash access.
What this means is: You either know all the string array indices you want to use, and either make sure they exist, or you check their existence every time you access them which might fail.
For numeric indices, the same thing applies. You might have an array with indices 1, 2 and 4 present, but if you run over it with only a for loop, you will trigger a notice when accessing the nonexistant element 3.
Use foreach loops whenever you want to iterate arrays. Check with isset() whenever you want to document that the array value might not be present. Don't do it if you know or assume that the array element MUST be present - if not, you get the notice as a reminder that your code is working on a data structure that is NOT what you thought it is. Which actually is a good thing. Then fix it. :)
Your best bet would be to abstract your session stuff by creating facade methods, or getters and setters around the session variables rather than access them directly. This way you can return a default value if the one you're after doesn't exist.
I've used array_key_exists() to check if the index is set. If it is not, I display 0 or add store a certain value in that field, else I show the value of that index or add certain value to that field.
Credit to Sven for bringing that up.
to check what u have in your sessions, loop tru it. not sure if this is what u are asking.
foreach($_SESSION as $x=>$y)
{
if(empty($x)) { //do something }
}
I'm learning PHP and Drupal. I need to reference a variable contained in an array called $contexts.
So print_r($contexts) gives me this:
Array (
[context_view_1] => ctools_context Object (
[type] => view
[view] => view Object (
[db_table] => views_view
[result] => Array (
[0] => stdClass Object (
[nid] => 28
[node_data_field_display_field_display_value] => slideshow
)
)
eek confusing. I want to work with the node_data_field_display_field_display_value variable. I think my code needs to be like this, but I know this isn't right:
if ($contexts['context_view_1']['view']['result'][0]
['node_data_field_display_field_display_value'] == 'slideshow') then do whatever...
Thanks!
You suggested the following array reference to get to the variable you want:
$contexts['context_view_1']['view']['result'][0]['node_data_field_display_field_display_value']
The reason this doesn't work is because some of the structures in the chain are actually objects rather than arrays, so you need a different syntax for them to get at their properties.
So the first layer is correct, because $contexts is an array, so context_view_1 is an array element, so you'd get to it with $contexts['context_view_1'] as you did.
But the next level is an object, so to get to view, you need to reference it as an object property with -> syntax, like so: $contexts['context_view_1']->view
For each level down the tree, you need to determine whether it's an object or an array element, and use the correct syntax.
In this case, you'll end up with something that looks like this:
$context['context_view_1']->view->result[0]->node_data_field_display_field_display_value
That's a mess of a variable. The issue you're having is that you're using the bracketed notation, e.g. "['view']", for each "step" in the navigation through your variable. That would be fine if each child of the variable were an array, but not every one is.
You'll note, for example, that $contexts['context_view_1'] is actually an object, not an array (take note that it says "[context_view_1] => ctools_context Object"). Whereas you would use that bracketed notation to address the elements of an array, you use the arrow operator to address the properties of an object.
Thus, you would address the field you are trying to reach with the following expression:
$contexts['context_view_1']->view->result[0]->node_data_field_display_field_display_value
For properties listed as "Object", you need to use -> to get into it, and "Array", you need to use []. So:
$contexts['context_view_1']->view->result[0]->node_data_field_display_field_display_value
$contexts['context_view_1']->view->result[0]->node_data_field_display_field_display_value
echo $context['context_view_1']->view->result[0]->node_data_field_display_field_display_value;
Do not mistake objects with arrays. A memeber of an array can be accesed by $array['member'], but fields of an object can be accessed as $object->fieldname.