自定义键值数组

<?php

//数组,自定义键(key)的数组  key=>value

//创建数组
$userName = array('lily','milly','amy');
print_r($userName);
echo '<br />';

$userName1 = array('li'=>'lily','milly','amy');
print_r($userName1);
echo '<br />';

$userName2 = array('sister'=>'lily','brother'=>'milly','friend'=>'amy');
print_r($userName2);
echo '<br />';


//访问数组
echo $userName2['sister'];
echo '<br />';

//追加元素
$userAge = array('lily'=>12);
print_r($userAge);
echo '<br />';

$userAge['mac']=23;
$userAge['pity']=33;
print_r($userAge);
echo '<br />';

//直接创建数组,不通过array
$userFrom['lily'] = 'wuhan';
$userFrom['mac'] = 'haungshi';
$userFrom['pit'] = 'shanghai';
print_r($userFrom);
echo '<br />';
if(is_array($userFrom)){
	echo 'userFrom is array';
}else{
	echo 'userFrom is not array';
}
echo '<br />';

//问题:不能通过for循环访问元素
//只能通过foreach()遍历数组元素

foreach($userFrom as $value){
	echo $value;
	echo '<br />';
}

foreach($userFrom as $key => $value){
	echo $key.'---comes from---'.$value;
	echo '<br />';
}

?>

你可能感兴趣的:(PHP,数组,自定义键值)