Associative array (PHP) - php

I do not know what does it mean when it says: (it is from hook_block_view code for drupal)
$block['content'] = array(
'#theme' => 'node_recent_block',
'#nodes' => $nodes,
);
I know $block['content'] is an associative array, also I know that $node is Full node object, Contains data that may not be safe.But about #theme, #nodes and 'node_recent_block.
Can someone please tell me what do they mean.
I searched a lot but I did not find out what does it mean when there is a # before name of a key.
Thank you

$block is an associative array, in which the element "content" is also an associative array.
Another way to define this would be:
$block = array(
'content' => array(
'#theme' => 'node_recent_block',
'#nodes' => $nodes,
),
);
Inside an associative array => is an assignment.
'content' is an array where '#theme' = 'node_recent_block' and '#nodes' = $nodes
Edit:
You could also assign the values like this:
$block['content']['#theme'] = 'node_recent_block';
$block['content']['#nodes'] = $nodes;

Related

Defining array in array in array - php

I am trying to create the following array dynamically:
$aSettings = array( "text"=>
array( "icon_type" =>
array(
"name"=>"icon_type",
"method"=>"dropdown",
"option"=>"",
"default"=>""
),
"column" =>
array(
"name"=>"column_count",
"method"=>"dropdown",
"default"=>"1"
)
)
)
I am not sure how to declare the array into the array.
I have the following example code:
$aSettings=array();
$aSetting_type['text']=array();
$aSetting_name['icon_type']=array();
$aSetting_name['column']=array();
$aSetting_values1=array('name'=>'icon_type','method'=>'dropdown','option'=>'','default'=>'');
$aSetting_values2=array('name'=>'column_count','method'=>'dropdown','default'=>1);
I guess I am overlooking something very simple, but how do I put all these arrays into each other?
I want to be able to call a value from the array as:
$aSettings['text']['column']['name'];
Any ideas?
You could do:
$aSettings['text']['icon_type'] = $aSetting_values1;
$aSettings['text']['column'] = $aSetting_values2;
If you need it more dynamic, you could use variables like so:
$type = 'text';
$name1 = 'icon_type';
$aSettings[$type][$name1] = $aSetting_values1;
Collect the array is reverse lower to higher order, it would be easy to collect without confusions, for example
$icon_type = array(
"name"=>"icon_type",
"method"=>"dropdown",
"option"=>"",
"default"=>""
);
$column = array(
"name"=>"column_count",
"method"=>"dropdown",
"default"=>"1"
);
$text = array(
"icon_type" => $icon_type,
"column" => $column
);
$aSettings = array(
"text"=> $text
);
Once you collect array like this you can easily access any element in the array i.e. echo $aSettings['text']['column']['name'];

pushing and apending arrays into an associative array in php

How do I push arrays inside the "adjacencies" key value pair that should have an encapsulated array holding the "arrays" (ie. array(("nodeTo" => "$to"),("nodeTo" => "$to"))) without overwriting them and appending them similiar to "+=". also the push into the key "adjacencies" doesnt seem to pick up the value.
$node[] = array(
"adjacencies" => array(), //inside this array should go all the arrays seprated by commas.
"data" => array(
"color" => $color1,
"type" => $type1
);
// this push doesnt seem to detect the adjacencies value and doesnt really push the array inside of the container array. I also tried $node["adjacencies"][]=array("nodeTo" => "$to"); but it didnt work
$node["adjacencies"]=array("nodeTo" => "$to");
}
If you want multiple arrays within 'adjacencies', append them to the end of the array:
$node[0]['adjacencies'][] = array("nodeTo" => "$to");
Granted, you'll need to know which $node index to work with (if there are multiple nodes).
Edit:
After reading the comments, it looks like the OP's desired array structure is this:
$node = array(
'adjacencies' => array(),
'data' => array(
'color' => $color1,
'type' => $type1,
);
);
Thus, to append additional nodes to the adjacencies array, you can do this:
$node['adjacencies'][] = array('nodeTo' => "$to");
By the way you use $node in the second statement I think you meant:
$node = array(
not:
$node[] = array(
// ^^
Then you can push the array by doing:
$node['adjacencies'][] = array('nodeTo' => $to);

How to push hash into array of hash in php?

Like array_push() where we can push an element in to array. I want to push an hash [name,url] in to an array of hash.
If you're referring to associative arrays where the key is user-provided (rather than an auto-incrementing numeric field), just use direct syntax:
$a = Array();
$a['name'] = 'url';
Note that $a = Array(); array_push($a, 'lol'); is (almost) the same as $a = Array(); $a[] = 'lol';. array_push is just a (pointless) "shortcut" for the same syntax, which only works for automatic, numeric indexes.
I strongly recommend reading the PHP manual section on the topic. That's what it's there for.
I do not know, what do you need, but it you need to push pair of values into array, this may be your solution:
$hashes_array = array();
array_push($hashes_array, array(
'name' => 'something1',
'url' => 'http://www1',
));
array_push($hashes_array, array(
'name' => 'something2',
'url' => 'http://www2',
));
After that $hashes_array should look like that (each element of the bigger array is array itself - associative array with two keys and two values corresponding to them):
[
['name' => 'something1', 'url' => 'http://www1'],
['name' => 'something2', 'url' => 'http://www2']
]
<?php
$aArrayOfHash['example'] = 'http://example.com/';
?>
ifif i understand your problem, you want to retrieve hash value from a url then use parse_url with PHP_URL_FRAGMENT argument
$url = 'http://username:password#hostname/path?arg=value#anchor';
print_r(parse_url($url));
echo parse_url($url, PHP_URL_FRAGMENT);
will return
[fragment] => anchor
Reference

Can I store an array within a multidimensional array? (PHP)

It is possible to put an array into a multi dim array? I have a list of user settings that I want to return in a JSON array and also have another array stored in that JSON array...what is the best way to do that if it isn't possible?
A multi dimension is already an array inside an array. So there's nothing stopping you from putting another array in there. Sort of like dreams within dreams :P
Just use associative arrays if you want to give your array meaning
array(
'SETTINGS' => array(
'arr1' => array( 0, 1),
'arr2' => array( 0, 1)
),
'DATA' => array(
'arr1' => array( 0, 1),
'arr2' => array( 0, 1)
)
)
EDIT
To answer your comment, $output_files[$file_id]['shared_with'] = $shared_info; translates to (your comment had an extra ] which I removed)
$shared_info = array(1, 2, 3);
$file_id = 3;
$output_files = array(
'3' => array(
'shared_with' => array() //this is where $shared_info will get assigned
)
);
//you don't actually have to declare it an empty array. I just did it to demonstrate.
$output_files[$file_id]['shared_with'] = $shared_info; // now that empty array is replaced.
any array key can have an array value in php, as well as in json.
php:
'key' => array(...)
json:
"key" : [...]
note: php doesn't support multidimensional arrays as in C or C++. it's just an array element containing another array.

Need help about array

What do
$categories[$id] = array('name' => $name, 'children' => array());
and
$categories[$parentId]['children'][] = array('id' => $id, 'name' => $name);
mean?
Thanks a lot.
How should i format the output so i can learn the results that was returned?
You can format your code into tables by looping on the array using for or foreach. Read the docs for each if you don't have a grasp on looping.
2.What does
$categories[$id] = array('name' => $name, 'children' => array());
and
$categories[$parentId]['children'][] = array('id' => $id, 'name' => $name);
The first line assigns an associative array to another element of the $categories array. For instance if you wanted the name of the category with ID of 6 it would look like this:
$categories[6]['name']
The second line does something similar, except when you are working with an array in PHP, you can use the [] operator to automatically add another element to the array with the next available index.
What is the uses of .= ?
This is the concatenation assignment operator. The following two statements are equal:
$string1 .= $string2
$string1 = $string1 . $string2
These all have to do with nesting arrays.
first example:
$categories[$id] = array('name' => $name, 'children' => array());
$categories is an array, and you are setting the key id to contain another array, which contains name and another array. you could accomplish something similar with this:
$categories = array(
$id => array(
'name' => $name,
'children' => array()
)
)
The second one is setting the children array from the first example. when you have arrays inside of arrays, you can use multiple indexes. It is then setting an ID and Name in that array. here is a another way to look at example #2:
$categories = array(
$parentID => array(
'children' => array(
'id' = $id,
'name' => $name
)
)
)
note: my two ways of rewriting are functionally identical to what you posted, I'm just hoping this makes it easier to visualize what's going on.

Categories