在本文将数组的各种创建方式用PHP实例代码的方式分享给大家,感兴趣的朋友可以了解下
看这篇文章之前相信大家都已经看过PHP中文手册关于数组这一节的讲解了,怎么样呢,看懂了多少?至少我第一次阅读文档时是一头雾水,也许是因为在翻译的不够通俗易懂吧^_^!!这里UncleToo根据自己的经验,将数组的各种创建方式用PHP实例代码的方式分享给大家,希望对大家有些帮助(当然,,PHP文档还是要多看的)
复制代码 代码如下:
$number = array(1,3,5,7,9);
$color =array("red","blue","green");
$student = array("name",17)
?>
复制代码 代码如下:
$language = array(1=>"PHP",3=>"JAVA",4=>"C");
$student = array("name"=>"张三","age"=>17)
?>
复制代码 代码如下:
$result = array();
?>
复制代码 代码如下:
$firstname = "Peter";
$lastname = "Griffin";
$age = "38";
$result = compact("firstname", "lastname", "age");
print_r($result);
?>
复制代码 代码如下:
Array
(
[firstname] => Peter
[lastname] => Griffin
[age] => 38
)
复制代码 代码如下:
$firstname = "Peter";
$lastname = "Griffin";
$age = "38";
$name = array("firstname", "lastname");
$result = compact($name, "location", "age");
print_r($result);
?>
复制代码 代码如下:
Array
(
[firstname] => Peter
[lastname] => Griffin
[age] => 38
)
复制代码 代码如下:
$a1=array("a","b","c","d");
$a2=array("Cat","Dog","Horse","Cow");
print_r(array_combine($a1,$a2));
?>
复制代码 代码如下:
$number = range(0,5);
print_r ($number);
?>
复制代码 代码如下:
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
)
复制代码 代码如下:
$number = range(0,50,10);
print_r ($number);
?>
复制代码 代码如下:
Array
(
[0] => 0
[1] => 10
[2] => 20
[3] => 30
[4] => 40
[5] => 50
)
复制代码 代码如下:
$letter = range("a","d");
print_r ($letter);
?>
复制代码 代码如下:
Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
复制代码 代码如下:
$a=array_fill(2,3,"Dog");
print_r($a);
?>