What are the differences between the two php arrays?

Source: Internet
Author: User
I: {code ...} ii. {code ...} the while LOOP retrieves a row of data from $ param-& amp; gt; field. In the second write method $ param_array [& #039; some_id & #039;] [& #039; values & #039;] The array eventually has only one element. I don't know what's going on? I:

while(!$param->end)            {                $param_array[$param->field['param_group_id']]['param_group_name'] = $param->field['param_group_name'];                $param_array[$param->field['param_group_id']]['values'][$param->field['param_id']]                    = $param->field['param_name'];                $param->move_next();            }

II:

while(!$param->end)            {                 $param_array[$param->field['param_group_id']] = array(                    'param_group_name' => $param->field['param_group_name'],                    'values' => array(                        $param->field['param_id'] =>  $param->field['param_name']                    )                );                $param->move_next();            }

The while LOOP retrieves a row of data from $ param-> field each time. In the second write method, $ param_array ['some _ id'] ['values'] has only one element in the array, I don't know what's going on?

Reply content:

I:

while(!$param->end)            {                $param_array[$param->field['param_group_id']]['param_group_name'] = $param->field['param_group_name'];                $param_array[$param->field['param_group_id']]['values'][$param->field['param_id']]                    = $param->field['param_name'];                $param->move_next();            }

II:

while(!$param->end)            {                 $param_array[$param->field['param_group_id']] = array(                    'param_group_name' => $param->field['param_group_name'],                    'values' => array(                        $param->field['param_id'] =>  $param->field['param_name']                    )                );                $param->move_next();            }

The while LOOP retrieves a row of data from $ param-> field each time. In the second write method, $ param_array ['some _ id'] ['values'] has only one element in the array, I don't know what's going on?

The second method is to redefine each time.$param_aray[ $param->field['param_group_id'] ]This element, when$param->field['param_group_id']Duplicate$param_aray[ $param->field['param_group_id'] ]For more information, see the DEMO:

$ Data = array ("id" => 1, "name" => "a", "group" => 2, "value" => 10 ), array ("id" => 2, "name" => "B", "group" => 1, "value" => 11 ), array ("id" => 3, "name" => "c", "group" => 2, "value" => 12 ), array ("id" => 4, "name" => "d", "group" => 1, "value" => 13 ), array ("id" => 5, "name" => "e", "group" => 2, "value" => 14 ),); /* First */$ result = array (); foreach ($ data as $ item) {$ result [$ item ['group'] ['value'] [$ item ['id'] = $ item ['value']} var_dump ($ result); /* type 2 */$ result = array (); foreach ($ data as $ item) {$ result [$ item ['group'] = array ("value" => array ($ item ['id'] => $ item ['value']) ;} var_dump ($ result );

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.