foreach学习遇到的有关问题(细说php 兄弟连)
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++; } ?>
输出结果如下:
在数组$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>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

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

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

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

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

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

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