Use Blade Array/Objects like Twig - arrays

Is there a way to treat objects and arrays the same in Blade (like twig).
So that no matter what, you access the object and array with dot-notation, or even "->" notation would be fine. I would like to not have to differentiate between an object and an array. Twig allows this, but blade does not. I want to know if there is an extension/change I can make to blade to allow the same feature as twig.
For example an object containing an array:
DATA:
$object = [ (object)
part_1 => [
part_2 => [ (array)
array_1 => "test"
]
]
]
BLADE:
$object->part_1->part_2['array_1']
//returns "test"
$object->part_1->part_2->array_1
//fails "Trying to get property of non-object"
TWIG:
object.part_1.part_2.array_1
//returns "test"
Also explained as so:
Twig makes it easy to access variables using the dot notation.
This can be used on either a object or array.
In Blade, it’s the same as plain PHP.
BLADE ----------------- TWIG
$user->name -------> user.name
$user['name'] -------> user.name

To use "->" -notation, the variable has to be of PHP's type StdClass. An array (or associative array) is not. Although, you can easily cast it :)
$stdObj = (object) ["prop" => "value"];
As #Bryce said,
this will not work for multidimensional array
. However, you can find your answer of converting it here https://www.if-not-true-then-false.com/2009/php-tip-convert-stdclass-object-to-multidimensional-array-and-convert-multidimensional-array-to-stdclass-object/

Related

how to get matrix itens by a count variable <? #entity[$count].literal ?> in Watson assistant (conversation)

I need to get all user input literals from a entity array. For example,
The user input:
I want to see dolphins, an elephant and dogs
The entity:
{
"type": "synonyms",
"value": "animalcheck",
"synonyms": [
"dolphins",
"elephant",
"dogs",
"dog",
"dolphin",
"girafe"
]
}
So.. need the user inputs literal: ["dolphins","elephant","dogs"]
I'm trying with this:
<? #entity[$count].literal ?>
where $count=1 and will be incremented until reach #entity.values.size() but this $count inside brackets return error, it's not works.
Any suggestion?
I do say you seem to be making hard work for yourself. Is there any reason you cannot have an entity group that's its own list of animal types, i.e. animals, which itself contains the values ["dolphins","elephant","dogs"]. In this way, if a users question contains any of these values, they will be in the entity array animals:["dolphins","dogs"] etc.
Which in turn is easier to handle within assistant. There is nothing to stop you having both entity groups "animals" and "animalcheck".
(Although animalcheck seems to be your value, not sure what your entity is actually called. You example uses #entity - but sure that would be allowed as an entity name. )
Also to access your entity, say if it was called "entitylist", you would need to use;
See Docs; https://cloud.ibm.com/docs/services/assistant?topic=assistant-expression-language
The issue might be with the $count variable not being treated as an integer and only integer can be used to access an array in WA.
This expression will assure that the content of $count variable will be treated as an integer and hence be ok to access arrays in WA:
<? #entity[$count.toInt()].literal ?>.

Accessing array element from twig function? (Symfony)

I want to do the equivalent of accessing:
{{ user.getRoles()[0]['name'] }}
So user.getRoles() is an array, and I want to access the name element of the first item in the array.
Obviously the above doesn't work though (error Impossible to access a key "name") I assume because combining the function call and accessing an array element isn't allowed.
What should I do?
Are you sure 'name' even exists? Tried doing a {{ dump(user.getRoles()) }}? What you're doing looks fine to me.
I constructed a similar method in my user object:
public function getTest() {
return [['name' => 'name1'],['name' => 'name2']];
}
And calling that in twig:
{{ user.getTest()[0]['name'] }}
Prints out 'name1' - so it looks like all twig is doing is telling you the key doesn't exist.

Get each JSON array value using PHP

I've been trying to read this JSON with PHP:
{
"total_found":"49",
"1":{
"title":"Maze Runner-The Scorch Trials 2015 HD-TS x264-Garmin",
"category":"video",
"seeds":2141,
"leechs":1176,
"torrent_size":1122230176,
"torrent_hash":"29d3e7062825a62abdd877ee96dc3fea41183836"
},
"2":{
"title":"Maze.Runner-The.Scorch.Trials.2015.720P.HD-TS.x264.AC3.HQ.Hive-CM8",
"category":"hdrip",
"seeds":395,
"leechs":1856,
"torrent_size":3999751475,
"torrent_hash":"e1e14a5ccf540a739907db2e1e0d810ecdb8bebc"
}
}
How can I get each title and torrent_size?
The easiest thing to do is to decode the JSON to an array. PHP has a built-in function for this:
$results = json_decode($json_data);
http://php.net/manual/en/function.json-decode.php
To clearly see the array structure, try dumping the variable $results using print_r();, i.e.:
print_r($results);
Then you simply need to access the data using the array.
It will be something like $results[1]['torrent_size'];.

In Puppet, can I realize a defined type for each element in an array?

After removing my variable names and everything extraneous for readability, my code comes down to this. I already set up a custom fact that returns an array of users that I need to set up some configuration files for. I'm trying to use a defined resource type and realize it with an array argument to do the configs for each user because Puppet lacks a basic for loop, so the code I have simplifies to this:
define modulename::pushconfigs{
user {"$name":
ensure => present
parameter => value
parameter => value
parameter => value
}
}
modulename::setconfigs{$::userlist: }
# $::userlist is an array of users, in the form [user1 user2 user3...]
for a couple parameters. However, when I try to run it, it says couldn't[do configs]for user user1 user2 user3. In other words, it's realizing the defined type only once, and it's trying to do so for a user whose name is the concatenated array.
How can I instead realize the defined type for each one in the array?
This should work...
define createuser{
user { $name :
ensure => present,
group => "gp",
home => "/home/$name",
shell => "/bin/bash",
}
}
$allusers = [ "user1", "user2", "user3", "user4" ]
createuser{$allusers:}
$::userlist is a string.
Its a fact, and facts are strings.
You need to turn it into an array before you can iterate over it.
If $::userlist = "user1,user2,user3"
$a_userlist=split($::userlist,',')
modulename::setconfigs{$a_userlist: }

Pass variable from one WordPress hook to another

I have functions for two WordPress hooks: admin_menu and save_post
First I declare an associative array, which is called as global in both functions.
In the admin_menu function, I'm adding an additional key & value to the end of the array and I want the updated array to be available to the save_post function.
$my_array = array(
"key1" => "value1",
"key2" => "value2"
);
my_admin_function() {
global $my_array;
$my_array["key3"] => "value3";
}
my_save_function() {
global $my_array;
}
add_action('admin_menu', 'my_admin_function');
add_action('save_post', 'my_save_function');
In the above example, $my_array in my_save_function still only has 2 key/value pairs.
I can't figure out how to get my third key into my_save_function so it will get saved!
(Note: In my full code I'm using my_admin_function to add a meta box which cycles through an array of form fields, and then adds another field to the array. I'm then trying to save all of the fields in `my_save_function'.)
Well, you cannot do this because save_post action occurs before admin_menu action.
You should use another hook, e.g. init.

Resources