DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Thing to Know About PHP Arrays

Thing to Know About PHP Arrays

Stoimen Popov user avatar by
Stoimen Popov
·
Dec. 14, 11 · Web Dev Zone · Interview
Like (0)
Save
Tweet
2.33K Views

Join the DZone community and get the full member experience.

Join For Free

Consider the following case. We have an array with identical keys.

$arr = array(1 => 10, 1 => 11);

What happens when the interpreter reaches this line of code? This is not a syntax error and it is completely valid. Very similar, but more interesting case is when we have an array of identical keys, where those identical keys are represented once as an integer and then as a string.

Keys in PHP arrays are not type sensitive, so pay attention when using them!

(Keys in PHP arrays are not type sensitive, so pay attention when using them!)

$arr = array(1 => 10, "1" => 11);

Now several questions arise. First of all, how many elements have this array? Two or one. This can be easily verified by checking what count() will return.

echo count($arr);

The correct answer is 1. This simply means, that there’s no difference between string keys and integer keys. What would happen if we had a “normal” array with different keys?

$arr = array(1 => 10, "2" => 11);
echo count($arr);

As expected this returns 2.

Next thing to check is what’s in the array after this initialization line.

$arr = array(1 => 10, "1" => 11);

Is there something in the first element $arr[0], or there’s something in the second element $arr[1]? What is the value of the single value?

As it appears the second element replaces the first one. We’ve seen that the array has only one value, but where’s that value? The only way to check this is to dump both elements:

var_dump($arr);

Here we can see that $arr[1] contains “11″ and it is the only value, and $arr[0] is not set.

Related posts:

  1. PHP: What is More Powerful Than list() – Perhaps extract()
  2. PHP: What is More Powerful Than list()
  3. PHP: The Array Element Doesn’t Exist – Suppress the Warnings

 

Source: http://www.stoimen.com/blog/2011/10/19/thing-to-know-about-php-arrays/

 

 

PHP

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Generate Fake Test Data
  • DZone's Article Submission Guidelines
  • Autowiring in Spring
  • Ultra-Fast Microservices: When Microstream Meets Payara

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo