PHP obtains the location instance of an element in the array.

Source: Internet
Author: User
To determine the position of an array element, we can use the array_keys function to compare with foreach to implement the syntax of array_keys as follows: array_keys (array, value, [strict]) where strict settings

To determine the position of an array element, we can use the array_keys function to compare with foreach.

The syntax of array_keys is as follows:

Array_keys (array, value, [strict])

When strict is set to true, the strict matching mode of data type verification is triggered. the default value is false. the following code shows a simple application of array_keys. the code is as follows:

  1. $ Test = array (10, 20, 30, "10", "20", "30 ");
  2. Print_r (array_keys ($ test, "10", true); // exact match
  3. // Output:
  4. // Array ([0] => 3)
  5. ?>

Of course, we can also use foreach to judge, the code is as follows:

  1. $ One = array (1, 2, 3, 4 );
  2. $ Two = array ("test1" => array ('name' => "tank", "phone" => 12412433 ),
  3. "Test2" => array ('name' => "ying", "phone" => 252523523 ),
  4. "Test3" => array ('name' => "zhang", "phone" => 656452345 ),);
  5. Foreach ($ one as $ k => $ v ){
  6. Echo "key =". $ k. "& value =". $ v ."
    ";
  7. }
  8. $ I = 0;
  9. Foreach ($ two as $ k => $ v ){
  10. Foreach ($ v as $ value ){
  11. If ($ v = 'tank ')
  12. {
  13. Echo $ I;
  14. }
  15. $ I ++;
  16. }
  17. }

Next, we can use the traversal of the array and then compare the result by adding a value.

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.