首頁 > 後端開發 > php教程 > PHP数组详解,数组详解_PHP教程

PHP数组详解,数组详解_PHP教程

WBOY
發布: 2016-07-12 08:49:29
原創
930 人瀏覽過

PHP数组详解,数组详解

作为一名C++程序员,在转做PHP开发的过程中,对PHP数组产生了一些混淆,与C++数组有相似的地方,也有一些不同,下面就全面地分析一下PHP的数组及其与C++中相应数据类型的区别和联系。

数组的分类:

1、数值数组:也叫索引数组,即以数字(从0 开始)作为数组下标。相当于C++中的vector。

2、关联数组:以字符串作为数组下标。相当于C++中的map。

3、多维数组:数组中每个元素也是一个数组。其子数组中的每个元素也可以是数组。

数组的声明:

1、数值数组

<span>a、如下例子中,会自动分配数字ID键。  
</span><span>$names</span> = <span>array</span>("Peter","Joe","Lily"<span>);  
b、如下例子中,我们人工分配数字ID键。  
</span><span>$names</span>[0] = "Peter"<span>;  
</span><span>$names</span>[1] = "Joe"<span>;  
</span><span>$names</span>[2] = "Lily"<span>;  
可以在脚本中使用这些ID键:  
</span><?<span>php  
    </span><span>$names</span>[0] = "Peter"<span>;  
    </span><span>$names</span>[1] = "Joe"<span>;  
    </span><span>$names</span>[2] = "Lily"<span>;  
    </span><span>echo</span> <span>$names</span>[0]." and ".<span>$names</span>[1]." are ".<span>$names</span>[2]."'s neighbors"<span>;  
</span><span>/*</span><span> 何问起 hovertree.com </span><span>*/</span>
?>  
登入後複製

2、关联数组:

<span>例子1  
</span><span>$ages</span> = <span>array</span>("Peter"=>32, "Joe"=>30, "Lily"=>28<span>);  
例子2  
本例与例子1相同,只是另一种创建数组的方法。  
</span><span>$ages</span>["Peter"] = "32"<span>;  
</span><span>$ages</span>["Joe"] = "30"<span>;  
</span><span>$ages</span>["Lily"] = "28"<span>;  
在脚本中使用关联数组:  
</span><?<span>php  
    </span><span>$ages</span>["Peter"] = "32"<span>;  
    </span><span>$ages</span>["Joe"] = "30"<span>;  
    </span><span>$ages</span>["Lily"] = "28"<span>;  
    </span><span>echo</span> "Peter is ".<span>$ages</span>["Peter"]." years old."<span>;  
</span><span>/*</span><span> 何问起 hovertree.com </span><span>*/</span>
?><span>  
以上脚本输出:  
Peter is </span>32 years old. 
登入後複製

3、多维数组:

<span>在本例中,我们创建了一个带有自动分配数字ID键的多维数组:  
</span><span>$families</span> = <span>array</span><span>  
{  
    </span>"Griffin"=><span>array</span><span>  
    {  
        </span>"Peter",  
        "Lois",  
        "Megan"<span>  
    }</span>,  
    "Quagmire"=><span>array</span><span>  
    {  
        </span>"Glenn"<span>  
    }</span>,  
    "Brown"=><span>array</span><span>  
    {  
        </span>"Cleveland",  
        "Loretta",  
        "Junior"<span>  
    }  
};  
</span><span>echo</span> "Is " . <span>$families</span>['Griffin'][2] . " a part of the Griffin family?"<span>;  
以上代码输出:  
Is Megan a part of the Griffin family</span>?  
登入後複製

数组遍历

1<span>、for循环遍历  
for循环只能遍历索引数组。  
</span><?<span>php  
    </span><span>$names</span> = <span>array</span>("Peter","Joe","Lily"<span>);  
    </span><span>for</span>(<span>$id</span>=0;<span>$id</span><<span>count</span>(<span>$names</span>);++<span>$id</span><span>)  
    {  
        </span><span>echo</span> <span>$names</span>[<span>$id</span><span>];  
    }  
</span>?>  
2<span>、foreach遍历  
即可以遍历索引数组,也可以遍历关联数组  
遍历索引数组  
</span><span>foreach</span>(array_expression <span>as</span> <span>$value</span><span>)  
{  
    循环体;  
}  
遍历关联数组  
</span><span>foreach</span>(array_expression <span>as</span> <span>$key</span>=><span>$value</span><span>)  
{  
    循环体;  
}  
  
A、一维数组遍历  
索引数组  
</span><?<span>php  
    </span><span>$contact</span> = <span>array</span>("李某","xx公司","abc@xx.com"<span>);  
    </span><span>foreach</span>(<span>$contact</span> <span>as</span> <span>$value</span><span>)  
    {  
        </span><span>echo</span> <span>$value</span><span>;  
    }  
</span>?>  
  
<?<span>php  
    </span><span>$contact</span> = <span>array</span>("姓名"=>"李某","公司"=>"xx公司","邮箱"=>"abc@xx.com"<span>);  
    </span><span>foreach</span>(<span>$contact</span> <span>as</span> <span>$key</span>=><span>$value</span><span>)  
    {  
        </span><span>echo</span> <span>$key</span>.":".<span>$value</span><span>;  
    }  
</span>?><span>  
  
B、多维数组遍历  
</span><?<span>php  
    </span><span>$wage</span> = <span>array</span><span>(  
        </span>"市场部"=<span>array</span><span>(  
            </span><span>array</span>(1,"李某","市场经理",8000),  
            <span>array</span>(2,"王某","市场专员",5000),  
            <span>array</span>(3,"刘某","市场专员",7000<span>)  
        )</span>,  
        "产品部"=<span>array</span><span>(  
            </span><span>array</span>(1,"李某","产品经理",9000),  
            <span>array</span>(2,"王某","产品专员",6000),  
            <span>array</span>(3,"刘某","产品专员",5000<span>)  
        )</span>,  
        "账务部"=<span>array</span><span>(  
            </span><span>array</span>(1,"李某","账务经理",7000),  
            <span>array</span>(2,"王某","账务专员",6000),  
            <span>array</span>(3,"刘某","账务专员",5000<span>)  
        )  
    );  
      
    </span><span>foreach</span>(<span>$wage</span> <span>as</span> <span>$section</span>=><span>$table</span><span>)  
    {  
        </span><span>echo</span> <span>$section</span>."部门人员如下"<span>;  
        </span><span>foreach</span>(<span>$table</span> <span>as</span> <span>$row</span><span>)  
        {  
            </span><span>foreach</span>(<span>$row</span> <span>as</span> <span>$value</span><span>)  
            {  
                </span><span>echo</span> <span>$value</span><span>;  
            }  
        }  
    }  </span><span>/*</span><span> 何问起 hovertree.com </span><span>*/</span>
?>  
登入後複製

推荐:http://www.cnblogs.com/roucheng/p/phpdongtai.html

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1137446.htmlTechArticlePHP数组详解,数组详解 作为一名C++程序员,在转做PHP开发的过程中,对PHP数组产生了一些混淆,与C++数组有相似的地方,也有一些不同,下...
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板