Home > Backend Development > PHP Tutorial > Detailed explanation of PHP arrays, detailed explanation of arrays_PHP tutorial

Detailed explanation of PHP arrays, detailed explanation of arrays_PHP tutorial

WBOY
Release: 2016-07-12 08:49:29
Original
930 people have browsed it

Detailed explanation of PHP arrays, detailed explanation of arrays

As a C programmer, in the process of switching to PHP development, I had some confusion about PHP arrays, which are similar to C arrays There are also some differences. Let’s comprehensively analyze the differences and connections between PHP arrays and the corresponding data types in C.

Classification of arrays:

1. Numeric array: also called index array, that is, numbers (starting from 0) are used as array subscripts. Equivalent to vector in C.

2. Associative array: Use string as array subscript. Equivalent to map in C.

3. Multidimensional array: Each element in the array is also an array. Each element in its subarray can also be an array.

Array declaration:

1. Numeric array

<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>
?>  
Copy after login

2. Associative array:

<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. 
Copy after login

3. Multidimensional array:

<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>?  
Copy after login

Array traversal

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>
?>  
Copy after login

Recommended: http://www.cnblogs.com/roucheng/p/phpdongtai.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1137446.htmlTechArticleDetailed explanation of PHP arrays, detailed explanation of arrays As a C programmer, in the process of switching to PHP development, I have a lot of experience with PHP Arrays have caused some confusion. They are similar to C arrays, but also have some differences. Next...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template