Home Backend Development PHP Tutorial foreach学习遇到的有关问题(细说php 兄弟连)

foreach学习遇到的有关问题(细说php 兄弟连)

Jun 13, 2016 pm 01:45 PM
gt php quot

foreach学习遇到的问题(细说php 兄弟连)
书中代码如下

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
<?php $contact=array(1,14=>"高某","A公司","北京市",14=>"(010)98765432","gao@php.com");
 $num=0;
 foreach($contact as $value)
 {
     echo"在数组\$contact中第".$num."个元素是:$value<br>";
     $num++;
 }
?>

Copy after login

输出结果如下:
在数组$contact中第0个元素是:1
在数组$contact中第1个元素是:(010)98765432
在数组$contact中第2个元素是:A公司
在数组$contact中第3个元素是:北京市
在数组$contact中第4个元素是:gao@php.com

小弟对于数组array存在困惑 其键与值的问题
第0个元素是1 这个我明白 那么第1个元素为什么会跳到电话号码呢?
14=>"高某"怎么算。。 然后是公司 北京市 邮箱 为什么会跳跃 不懂 求指点 

ps : 此书输出语句居然是【在数组\$contact中第$num个元素是:$value
】 你没的 连接符都没有。

------解决方案--------------------
数组运行机制.
从左到右 按 键值生成 内容.无键值情况 则 按 数字顺序生成 键值。

以上
左到右的情况中 键值 为 14 的有2个

数组现排序 按插入情况排列
[0] = 1 (1)
[14] = 高某 (2) [15] = A公司 (3)
------解决方案--------------------
晕,你重复定义了
array(
14=>"高某",
14=>"(010)98765432"
);
自然就被(010)98765432,覆盖了
但是按照数组的排列14=>"高某",原来排在第二个,
所以14=>"(010)98765432"排在第二个
------解决方案--------------------
PHP code


$contact = array(
    1,
    14 => "高某", 
    "A公司", 
    "北京市",
    14 => "(010)98765432",
    "gao@php.com");
// 格式化打印出来,如下:
echo '<pre class="brush:php;toolbar:false">';print_r($contact);
/*
Array
(
    [0] => 1
    [14] => (010)98765432
    [15] => A公司
    [16] => 北京市
    [17] => gao@php.com
)
*/

// 由此可以看出后面的“(010)98765432”将前面的“高某”覆盖了,这是为什么呢?因为php中同维度数组中不允许存在相同的键,你可以将键看成人的身份证号码,值看成是人的名字,名字有相同而身份证号不会有相同。之后数组变成如下:
$contact=array(1,14=>"(010)98765432","A公司","北京市","gao@php.com");

//这个时候因为"A公司","北京市","gao@php.com"这三个在键14的后面,那么它们的键不会再从1开始了,而是接着14开始,也就是15
//咱们再看个例子(将"A公司"移动到1的后面):
$contact=array(1,"A公司",14=>"(010)98765432","北京市","gao@php.com");
echo '<pre class="brush:php;toolbar:false">';print_r($contact);

//结果输出如下:
/*
Array
(
    [0] => 1
    [1] => A公司
    [14] => (010)98765432
    [15] => 北京市
    [16] => gao@php.com
)
*/ <div class="clear">
                 
              
              
        
            </div>
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles