$ Arrayname [item] = difference between value and $ arrayname [item] = value

Source: Internet
Author: User
The difference between $ arrayname [item] value and $ arrayname [item] value when reading the wordpress taxonomy. php source code, an array creation problem occurs. in summary:
$ Arrayname [item] = What is the difference between value and $ arrayname [item] [] = value? In this example, the number/value is switched to the next position, right? Please kindly advise.
In function _ get_term_hierarchy ($ taxonomy:

$ Children = array ();

$ Terms = get_terms ($ taxonomy, array ('get' => 'all', 'orderby' => '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 [] do not use quotation marks to enclose Variables. otherwise ..... $ arrayname [item] = value this is a one-dimensional array. The value of the element whose $ arrayname is set to 'item' is value and $ arrayname ['item'] [] = value is two-dimensional. array sets the value of the current position of the lower-level array of array $ arrayname ['item'] to value

$ Children [$ parent] []
$ Children [$ parent] = it may be an array (there may be many sub-items, but it may be an array)

$ _ Terms [$ term-> term_id] = $ term-> parent;
$ _ Terms [$ term-> term_id] = can only be one value (only one parent item can be)
Yes

$ _ Terms [$ term-> term_id] = $ term-> parent;
The parent node used to collect each term_id (term_id is unique)

$ Children [$ parent] [] = $ term_id;
Used to cluster subnodes

The former is a direct value assignment.
The latter creates an array and then adds the last element.

The former is a direct value assignment.
The latter creates an array and then adds the last element.
-- 'Create an array and add the last element? I don't understand.
$ _ Terms [$ term-> term_id] = $ term-> parent;
The parent node used to collect each term_id (term_id is unique)

$ Children [$ parent] [] = $ term_id;
Used to cluster subnodes
---- Understandable

$ Arrayname [item] = value and $ arrayname [item] [] = value are constructed into an array like this, right?
$ Aaa = array (
3 => 7,
6 => 9)

$ Bbb = array (
3 => array (
0 => 8,
1 => 4,
)
)

The latter creates an array and then adds the last element.
-- 'Create an array and add the last element? I don't understand.

For
Foreach ($ terms as $ term ){
$ _ Terms [$ term-> term_id] = $ term-> parent;
}
The value assignment is executed in a loop.
If $ term-> term_id is not unique, that is, $ term-> term_id appears repeatedly.
Then, $ _ terms [$ term-> term_id] is the last $ term-> parent of the same $ term-> term_id.

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.