When reading the WordPress taxonomy.php source code, encountered the creation of the array problem, summed up is:
What is the difference between $arrayname [Item]=value and $arrayname [Item][]=value? In this case, the number/value Exchange is the next position, right? Please advise the talent.
function _get_term_hierarchy ($taxonomy):
$children = Array ();
$terms = Get_terms ($taxonomy, Array (' get ' = = ' all ', ' by ' + ' = ' id ', ' fields ' = ' id=>parent '));
foreach ($terms as $term _id = $parent) {
if ($parent > 0)
$children [$parent] = $term _id;
}
In function get_terms ($taxonomies, $args = "):
$_terms = Array ();
if (' id=>parent ' = = $_fields) {
foreach ($terms as $term) {
$_terms[$term->term_id] = $term->parent;
}
}
Reply to discussion (solution)
First you [] inside is not the variable need to enclose in quotation marks, otherwise ... $arrayname [Item]=value This is a one-dimensional array that $arrayname an array of elements named ' item ' to value and $arrayname [' Item ' [] = value is a two-dimensional array that sets the value of the current position of the sub-array of the array $arrayname[' item '] to value
$children [$parent] []
$children [$parent]= may be an array (the child may be a lot of ah, can be an array)
$_terms[$term->term_id] = $term->parent;
$_terms[$term->term_id]= can only be a value (the parent can only be one AH)
Yes No
$_terms[$term->term_id] = $term->parent;
Used to collect the parent node for each term_id (term_id is unique)
$children [$parent] = $term _id;
Used to cluster sub-nodes
The former is directly assigned to the value.
The latter is the creation of an array, followed by the addition of the last element.
The former is directly assigned to the value.
The latter is the creation of an array, followed by the addition of the last element.
What does it mean to ' create an array and then go after the last element '? Do not understand.
$_terms[$term->term_id] = $term->parent;
Used to collect the parent node for each term_id (term_id is unique)
$children [$parent] = $term _id;
Used to cluster sub-nodes
----can understand.
$arrayname [Item]=value and $arrayname [Item][]=value] The array that is created by this, right?
$AAA = Array (
3=>7,
6=>9)
$BBB = Array (
3=>array (
0=>8,
1=>4,
)
)
The latter is the creation of an array, followed by the addition of the last element.
What does it mean to ' create an array and then go after the last element '? Do not understand.
For
foreach ($terms as $term) {
$_terms[$term->term_id] = $term->parent;
}
Because the assignment is performed in a loop
If $term->term_id is not unique, that is $term->term_id repeated
So, $_terms[$term->term_id] is the last $term of the same $term->term_id->parent