$arrayname [the difference between Item]=value and $arrayname [Item]=value]

Source: Internet
Author: User
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

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.