PHP compound data type array usage analysis _ PHP Tutorial

Source: Internet
Author: User
Tags array definition
Analysis of PHP composite data type array usage. For example, data type is a hard-to-understand knowledge point. in this article, we will explain how to use PHP composite data type array, data type is a hard-to-understand knowledge point. in this article, we will explain how to use PHP composite data type array, and hope to help you.

(1) array: arrays in PHP use an optimized ordered graph type as an array. Always put quotation marks on the array index represented by a string (except a constant or variable.

PHP composite data type array definition array ():

Array ([key =>] // key can be integer or string
Value // value can be any value
,...
)

 
 
  1. <? Php
  2. $ Arr = array ("foo" => "bar", 12 => true); // if the key is a floating point, it is rounded to integer.
  3. Echo $ arr ["foo"]; // bar
  4. Echo $ arr [12]; // 1
  5. ?>


The default key name of the PHP composite data type array takes the current maximum integer index value, and the new key name index adds one to the current maximum index. If the specified key name already has a value, the value will be overwritten. Since PHP 4.3.0, the above index generation method has changed. If you add a new value to an array whose current maximum key name is a negative value, the newly generated index will be zero (0 ). Previously, the newly generated index is the current maximum index plus one, which is the same as the positive index.

Add/modify array elements:

 
 
  1. < ?php
  2. $arr = array(5 => 1, 12 => 2);
  3. $arr[] = 56; // This is the same as $arr[13] = 56;
  4. // at this point of the script
  5. $arr["x"] = 42; // This adds a new element to
  6. // the array with key "x"
  7. unset($arr[5]); // This removes the element from the array
  8. unset($arr); // This deletes the whole array
  9. ?>

The introduction of PHP composite data type array is here. I hope you can master this knowledge point.


...

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.