PHP笔记(8)数组定义与遍历

<?php
//define a array
$smilies = array(
	':)' => 'smile.png',
	'=)' => 'smile.png',
	':|' => 'neutral.png',
	'=|' => 'neutral.png',
	':(' => 'sad.png',
	'=(' => 'sad.png',
	':D' => 'big_smile.png',
	'=D' => 'big_smile.png',
	':o' => 'yikes.png',
	':O' => 'yikes.png',
	';)' => 'wink.png',
	':/' => 'hmm.png',
	':P' => 'tongue.png',
	':p' => 'tongue.png',
	':lol:' => 'lol.png',
	':mad:' => 'mad.png',
	':rolleyes:' => 'roll.png',
	':cool:' => 'cool.png');
	//show each of the value and key
	foreach ($smilies as $smiley_text => $smiley_img)
	{
		echo "key is $smiley_text value is $smiley_img <br />";
	}
?>

其中数组来自某php的源码,这里将其简化,让大家明白如何使用这样的东西

<?php
//define a array
$smilies = array(
	':)' => 'smile.png',
	'=)' => 'smile.png',
	':|' => 'neutral.png'
	);
	//show each of the value and key
	foreach ($smilies as $smiley_text => $smiley_img)
	{
		echo "key is $smiley_text value is $smiley_img <br />";
	}
	$nCount = count($smilies);
	echo "the count of array is $nCount <br />";
	echo array_pop($smilies).'<br />';
	echo "the count of array is $nCount <br />";
	// if we use array_pop ,then the array is delete the
	// end of the value ,so that we can't find the value
	echo 'bad We can\'t find value of :| '.$smilies[':|'].'<br />';
	echo 'good find it '.$smilies[':)'].'<bar />';

?>

在来看一些操作。。大家可以自己运行一下!

这里给出显示.

key is :) value is smile.png
key is =) value is smile.png
key is :| value is neutral.png
the count of array is 3
neutral.png
the count of array is 3

Notice: Undefined index: :| in C:\PHPnow-1.5.6\htdocs\arry.php on line 19
bad We can't find value of :|
good find it smile.png


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值