PHP------定义数组,取值数组和遍历数组

PHP数组

特点:可以存储任意类型的数据,可以不连续,可以是索引的也可以是关联的

什么是索引?

就是常见数组的样式,索引从开始,0,1,2,3,定义数组是直接往里面放值,只个索引自动生成,所以一般从0开始的,这样的数组是索引数组,索引是连续的。

什么是关联?

就是我们的哈希表集合,在定义的时候,必须给它一个key,一个values,这两个是关联的,通过key对应的values值是关联的。

1.定义数组

定义数组的第一种方式:

定义简单地索引数组

$a = array(1,2,3);

定义数组的第二种方式:

赋值定义

$a[] =1;

$a[] =2;

$a[] =3;

定义数组的第三种方式:

定义关联数组

$a = array(

"one"=>"hello",

"two"=>100,

"three"=>9.9

);

2.数组取值

根据索引数组取值:

$a = array(1,2,3);

echo $a[0];

根据key取value值取值:

$a = array(

"one"=>"hello",

"two"=>100,

"three"=>9.9

);

echo $a["three"];

3.遍历数组

(1).for循环

只适用于索引数组

$a = array(1,2,3);

for($i=0;$i<count($a);$i++)

{

  echo $a[$i]."<br>";

}

(2).foreach遍历

适用于所有数组

foreach第一种形式:

$a = array(

"one"=>"hello",

"two"=>100,

"three"=>9.9);

foreach($a as $b)

{

  echo $b."<br>";

}

foreach第二种形式:

$a = array(

"one"=>"hello",

"two"=>100,

"three"=>9.9);

foreach($a as $b=>$c)

{

  //echo $b."<>".$c."<br>";

  //也可以这样写:  

  echo "{$b}<>{$c}<br>";

}

(3).适用each()和list()结合遍历数组

返回数组里面的当前元素的详细内容

$a = array(

"one"=>"hello",

"two"=>100,

"three"=>9.9);

var_dump(each($a));

var_dump(each($a));

var_dump(each($a));

(4).将右侧数组里面的每个元素分别赋值给list()的参数列表

注意:右侧数组必须包含索引

$a = array(1,2,3);

list($b,$c,$d) = $a;

echo $b;

echo $c;

echo $d;

while遍历

(5).适用于each()和list()结合数组遍历

$a = array(

"one"=>"hello",

"two"=>100,

"three"=>9.9);

while(list($b,$c) = each($a))

{

  echo "{$b}--{$c}<br>";

}

时间: 2024-08-06 12:54:06

PHP------定义数组,取值数组和遍历数组的相关文章

php 数组定义、取值和遍历

<?php //常用函数 //生成随机数 //echo rand(1,10); //两个参数来确定随机数的范围 //日期时间函数 //var_dump(time()); //取当前时间的UNIX时间戳 //date_default_timezone_set('PRC'); //echo date("Y-m-d H:i:s",time());//格式化日期时间戳 //echo date("Y-m-d H:i:s");//省掉第2个参数获取当前时间 //数组 //

[黑马PHP教程]A. 数组的定义,取值,分类

一,数组基础 二,数组定义(赋值) 三,数组取值  四,数组的分类 按键值关系来分:        按数组的维度(复杂程度)来分:

抓取二维数组某值出来,到一维数组

/*** * '抓取二维数组某值出来,到一维数组' * @param $arr * @param $item * @return array */ function get_arr_item_val($arr,$item){ if (!$arr){ return []; } $res = []; foreach ($arr as $v){ $res[] = $v[$item]; } return $res; } 原文地址:https://www.cnblogs.com/pansidong/p/1

不定长数组取值交叉遍历组合生成算法

代码例如以下: #include <stdio.h> int factor[3][4] = { {0, 1, 2, 3}, {0, 1}, {0, 1, 2}, }; int lengths[3] = {4, 2, 3}; void recurisionAccess(int factor[3][4], int lengths[3], int colum, int row) { int i = 0; int j = 0; int k = 0; int len = 0; int len_num =

二叉树 根据二叉树的前序数组和中序序遍历数组生成二叉树

题目:给定二叉树的前序遍历和中序遍历,生成二叉树. Example: 前序遍历数组:preArr[]:{1,2,4,5,3,6,7} 中序遍历数组:inArr[]:{4,2,5,1,6,3,7} 生成的二叉树如下图: 解题思路: 由二叉树的前序变量性质可知:preArr[0] 是数组的根节点,有根据二叉树的中序遍历的性质可知,{4,2,5}是二叉树的左子树,{6,3,7}在右子树上,重复执行该操作就构造出了二叉树 public class Solution { public TreeNode r

把一个数组遍历倒序放到另一个数组中,数组取值是c:out value

1 var year = "<c:out value="${year}" />"; 2 3 var years = []; 4 5 var yearDataArray = []; 6 //遍历year,放进yearDataArray中 7 $("#year option[value!='']").each(function(){ 8 yearDataArray.push($(this).text()); 9 }); 10 //year

python 字典,元组,对象,数组取值方法

def create(self,cr,uid,vals,context=None): if context is None: context ={} if vals.get('name','/')=='/': vals['name']=self.pool.get('ir.sequence').get(cr,uid,'sale.origin') or '/' ctx=dict(context or {},mail_create_nolog=True) #如果存在这个字段 则不处理 反之 删除本元素

Python 2.7.9 Demo - 015.元组的定义、取值、遍历

#coding=utf-8 #!/usr/bin/python final_list = ('a', 1, 'b', 2, 'c', 3); print final_list[0]; print final_list[1:3]; print final_list * 2; print final_list + final_list + final_list; # 原组不能被重新赋值 # TypeError: 'tuple' object does not support item assignm

Python 2.7.9 - 014.列表List的定义、取值、遍历

#coding=utf-8 #!/usr/bin/python list = ['a', 1, 'b', 2, 'c', 3]; print list[0]; print list[1:3]; print list * 2; print list + list + list; # 遍历 print('for each method 1 : '); for data in list: print data; print('for each method 2 : '); for i in range

Javascript 数组之判断取值和数组取值

题目:var arr = [ '100px', 'abc'-6, [], -98765, 34, -2, 0, '300', , function(){alert(1);}, null, document, [], true, '200px'-30,'23.45元', 5, Number('abc'), function(){ alert(3); }, 'xyz'-90 ]; 1.找到arr里所有的数字:-98765, 34, -2, 0, 5 2.找到可以转成数字的:'100px', -987