When PHP array keys are printed in descending order, only one?

Source: Internet
Author: User
Tags array definition
{Code ...}
php
  "36","Bill"=>"35","Peter"=>"43");krsort($age);foreach($age as $x=>$x_value)    {    echo "Key=" . $x . ", Value=" . $x_value;    echo "
"; }?>

Reply content:
php
  "36","Bill"=>"35","Peter"=>"43");krsort($age);foreach($age as $x=>$x_value)    {    echo "Key=" . $x . ", Value=" . $x_value;    echo "
"; }?>

I changed the sorting by value just now. No problem,
Must all keys (including databases) be unique?

I thought it was a sorting function problem. It turned out to be an array definition problem. The subject prints the size of your array and there are only two elements:

php$age=array("Bill"=>"35","Bill"=>"32","Peter"=>"43");echo count($age);# > 2

If there are two identical key values in the array, then you want $ age ["Bill"] (assuming the values of these two keys are different) Which one should output?

So this is not a problem with sorting functions. Therefore, the keys in the array must be unique. Otherwise, confusion may occur.

PHP defines an array. The key is unique. If two identical indexes are displayed. Add and overwrite the previous one.
For details, see:
Http://php.net/manual/zh/function.array.php

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.