ホームページ >php教程 >php手册 >玩转PHP关联数组的10个技巧

玩转PHP关联数组的10个技巧

WBOY
WBOYオリジナル
2016-06-06 19:36:121517ブラウズ

关联 数组是描述一组键及其 关联 值时经常使用的一种数据类型,可以说它是所有编程语言的支柱,当然,PHP也不例外,实际上, 关联 数组对Web开发任务来说是非常重要的,PHP支持多种操作 关联 数组的方法,对开发人员来说,要从中找出最有效,最合适自己所开

  关联数组是描述一组键及其关联值时经常使用的一种数据类型,可以说它是所有编程语言的支柱,当然,PHP也不例外,实际上,关联数组对Web开发任务来说是非常重要的,PHP支持多种操作关联数组的方法,对开发人员来说,要从中找出最有效,最合适自己所开发程序的方法来操纵这些数组。

  这篇文章将介绍10个操作PHP关联数组的技巧,熟练运用能帮助你提高开发效率。

  1、添加数组元素

  PHP是一种弱类型语言,这意味着你不需要显示声明一个数组及其大小,相反,你可以同时声明并填充数组。

<p><span>1</span><span>.</span><span>$capitals</span><span>=</span><span>array</span><span>(  <br>   </span><span>2</span><span>.</span><span>'</span><span>Alabama</span><span>'</span><span>=></span><span>'</span><span>Montgomery</span><span>'</span><span>,</span><span>  <br>   </span><span>3</span><span>.</span><span>'</span><span>Alaska</span><span>'</span><span>=></span><span>'</span><span>Juneau</span><span>'</span><span>,</span><span>  <br>   </span><span>4</span><span>.</span><span>'</span><span>Arizona</span><span>'</span><span>=></span><span>'</span><span>Phoenix</span><span>'</span><span>  <br>   </span><span>5</span><span>.</span><span> ); </span></p>

  额外的数组元素可以象下面这样追加:

<p><span>$capitals</span><span>[</span><span>'</span><span>Arkansas</span><span>'</span><span>] </span><span>=</span><span>'</span><span>Little Rock</span><span>'</span><span>; </span></p>

  如果你正在处理数字索引数组,你可能想使用显示命名的函数前置和追加元素,如array_push()和array_unshift()函数,但这些函数不能操作关联数组。

  2、删除数组元素

  如果要从数组中删除一个元素,请使用unset()函数,如:

<p><span>unset</span><span>(</span><span>$capitals</span><span>[</span><span>'</span><span>California</span><span>'</span><span>]); </span></p>

  使用数字索引数组时,删除数组元素的办法更多,更灵活,可以使用array_shift()和array_pop()函数分别从数组的开头和末尾删除一个元素。

  3、交换键和值

  假设你想创建一个名叫$states的新数组,使用州府作为索引,使用州名作为关联值,使用array_flip()函数很容易完成这个任务。

<p><span>1</span><span>.</span><span>$capitals</span><span>=</span><span>array</span><span>(  <br>   </span><span>2</span><span>.</span><span>'</span><span>Alabama</span><span>'</span><span>=></span><span>'</span><span>Montgomery</span><span>'</span><span>,</span><span>  <br>   </span><span>3</span><span>.</span><span>'</span><span>Alaska</span><span>'</span><span>=></span><span>'</span><span>Juneau</span><span>'</span><span>,</span><span>  <br>   </span><span>4</span><span>.</span><span>'</span><span>Arizona</span><span>'</span><span>=></span><span>'</span><span>Phoenix</span><span>'</span><span>  <br>   </span><span>5</span><span>.</span><span> );  <br>   </span><span>6</span><span>.</span><span>$states</span><span>=</span><span>array_flip</span><span>(</span><span>$capitals</span><span>);  <br>   </span><span>7</span><span>.</span><span>//</span><span> $states = array(  </span><span><br></span><span>8</span><span>.</span><span>//</span><span>  'Montgomery' => string 'Alabama',  </span><span><br></span><span>9</span><span>.</span><span>//</span><span>  'Juneau'     => string 'Alaska',  </span><span><br></span><span>10</span><span>.</span><span>//</span><span>  'Phoenix'    => string 'Arizona'  </span><span><br></span><span>11</span><span>.</span><span>//</span><span> ); </span></p>

  4、合并数组

  假设前面的数组由一个基于Web的“FlashCard”服务使用,你想提供一种方法测试学生对美国各州首府的掌握情况,你可以使用array_merge()函数合并包含州和首府的数组。

<p><span>1</span><span>.</span><span>$stateCapitals</span><span>=</span><span>array</span><span>(  <br>   </span><span>2</span><span>.</span><span>'</span><span>Alabama</span><span>'</span><span>=></span><span>'</span><span>Montgomery</span><span>'</span><span>,</span><span>  <br>   </span><span>3</span><span>.</span><span>'</span><span>Alaska</span><span>'</span><span>=></span><span>'</span><span>Juneau</span><span>'</span><span>,</span><span>  <br>   </span><span>4</span><span>.</span><span>'</span><span>Arizona</span><span>'</span><span>=></span><span>'</span><span>Phoenix</span><span>'</span><span>  <br>   </span><span>5</span><span>.</span><span> );  <br>   </span><span>6</span><span>.</span><span>$countryCapitals</span><span>=</span><span>array</span><span> (  <br>   </span><span>7</span><span>.</span><span>'</span><span>Australia</span><span>'</span><span>=></span><span>'</span><span>Canberra</span><span>'</span><span>,</span><span>  <br>   </span><span>8</span><span>.</span><span>'</span><span>Austria</span><span>'</span><span>=></span><span>'</span><span>Vienna</span><span>'</span><span>,</span><span>  <br>   </span><span>9</span><span>.</span><span>'</span><span>Algeria</span><span>'</span><span>=></span><span>'</span><span>Algiers</span><span>'</span><span>  <br>  </span><span>10</span><span>.</span><span> );  <br>  </span><span>11</span><span>.</span><span>$capitals</span><span>=</span><span>array_merge</span><span>(</span><span>$stateCapitals</span><span>,</span><span>$countryCapitals</span><span>); </span></p>

  5、编辑数组值

  假设在数组中的数据包含大小写错误,在插入到数据库之前,你想纠正这些错误,你可以使用array_map()函数给每个数组元素应用一个回调。

<p><span>function</span><span> capitalize(</span><span>$element</span><span>)  <br> {  <br>   </span><span>$element</span><span>=</span><span>strtolower</span><span>(</span><span>$element</span><span>);  <br>   </span><span>return</span><span>ucwords</span><span>(</span><span>$element</span><span>);  <br> }  <br> </span><span>$capitals</span><span>=</span><span>array</span><span>(  <br>   </span><span>'</span><span>Alabama</span><span>'</span><span>=></span><span>'</span><span>montGoMEry</span><span>'</span><span>,</span><span>  <br>   </span><span>'</span><span>Alaska</span><span>'</span><span>=></span><span>'</span><span>Juneau</span><span>'</span><span>,</span><span>  <br>   </span><span>'</span><span>Arizona</span><span>'</span><span>=></span><span>'</span><span>phoeniX</span><span>'</span><span>  <br> );  <br> </span><span>$capitals</span><span>=</span><span>array_map</span><span>(</span><span>"</span><span>capitalize</span><span>"</span><span>,</span><span>$capitals</span><span>); <br></span></p>

6、按键对数组排序

FlashCard程序常常使用各种排序,如按字母顺序排序,你可以使用ksort()函数按键对关联数组进行排序。

<p><span>1</span><span>.</span><span>$capitals</span><span>=</span><span>array</span><span>(  <br>   </span><span>2</span><span>.</span><span>'</span><span>Arizona</span><span>'</span><span>=></span><span>'</span><span>Phoenix</span><span>'</span><span>,</span><span>  <br>   </span><span>3</span><span>.</span><span>'</span><span>Alaska</span><span>'</span><span>=></span><span>'</span><span>Juneau</span><span>'</span><span>,</span><span>  <br>   </span><span>4</span><span>.</span><span>'</span><span>Alabama</span><span>'</span><span>=></span><span>'</span><span>Montgomery</span><span>'</span><span>  <br>   </span><span>5</span><span>.</span><span> );  <br>   </span><span>6</span><span>.</span><span>ksort</span><span>(</span><span>$capitals</span><span>); </span></p>

  因为数组是通过参数传递给ksort()函数的,意味着你不再需要将排序结果分配给另一个变量。

  7、随机数组排序

  在FlashCard程序中还涉及到另一种随机排序技术,这时你要使用shuffle()函数实现数组项目的随机排序。

<p><span>1</span><span>.</span><span>$capitals</span><span>=</span><span>array</span><span>(  <br>   </span><span>2</span><span>.</span><span>'</span><span>Arizona</span><span>'</span><span>=></span><span>'</span><span>Phoenix</span><span>'</span><span>,</span><span>  <br>   </span><span>3</span><span>.</span><span>'</span><span>Alaska</span><span>'</span><span>=></span><span>'</span><span>Juneau</span><span>'</span><span>,</span><span>  <br>   </span><span>4</span><span>.</span><span>'</span><span>Alabama</span><span>'</span><span>=></span><span>'</span><span>Montgomery</span><span>'</span><span>  <br>   </span><span>5</span><span>.</span><span> );  <br>   </span><span>6</span><span>.</span><span>shuffle</span><span>(</span><span>$capitals</span><span>); </span></p>

  如果不需要打乱数组顺序,你只是想随机选择一个值,那么使用array_rand()函数即可。

  8、确定键和值是否存在

  你可以使用in_array()函数确定一个数组元素是否存在。

<p><span>1</span><span>.</span><span>$capitals</span><span>=</span><span>array</span><span>(  <br>   </span><span>2</span><span>.</span><span>'</span><span>Arizona</span><span>'</span><span>=></span><span>'</span><span>Phoenix</span><span>'</span><span>,</span><span>  <br>   </span><span>3</span><span>.</span><span>'</span><span>Alaska</span><span>'</span><span>=></span><span>'</span><span>Juneau</span><span>'</span><span>,</span><span>  <br>   </span><span>4</span><span>.</span><span>'</span><span>Alabama</span><span>'</span><span>=></span><span>'</span><span>Montgomery</span><span>'</span><span>  <br>   </span><span>5</span><span>.</span><span> );  <br>   </span><span>6</span><span>.</span><span>if</span><span> (</span><span>in_array</span><span>(</span><span>"</span><span>Juneau</span><span>"</span><span>,</span><span>$capitals</span><span>))  <br>   </span><span>7</span><span>.</span><span> {  <br>   </span><span>8</span><span>.</span><span>echo</span><span>"</span><span>Exists!</span><span>"</span><span>;  <br>   </span><span>9</span><span>.</span><span> } </span><span>else</span><span> {  <br>  </span><span>10</span><span>.</span><span>echo</span><span>"</span><span>Does not exist!</span><span>"</span><span>;  <br>  </span><span>11</span><span>.</span><span> } </span></p>

  很少有人知道这个函数也可以确定一个数组键是否存在,在这一点上,它和array_key_exists()函数的功能一样。

<p><span>1</span><span>.</span><span>$capitals</span><span>=</span><span>array</span><span>(  <br>   </span><span>2</span><span>.</span><span>'</span><span>Arizona</span><span>'</span><span>=></span><span>'</span><span>Phoenix</span><span>'</span><span>,</span><span>  <br>   </span><span>3</span><span>.</span><span>'</span><span>Alaska</span><span>'</span><span>=></span><span>'</span><span>Juneau</span><span>'</span><span>,</span><span>  <br>   </span><span>4</span><span>.</span><span>'</span><span>Alabama</span><span>'</span><span>=></span><span>'</span><span>Montgomery</span><span>'</span><span>  <br>   </span><span>5</span><span>.</span><span> );  <br>   </span><span>6</span><span>.</span><span>if</span><span> (</span><span>array_key_exists</span><span>(</span><span>"</span><span>Alaska</span><span>"</span><span>,</span><span>$capitals</span><span>))  <br>   </span><span>7</span><span>.</span><span> {  <br>   </span><span>8</span><span>.</span><span>echo</span><span>"</span><span>Key exists!</span><span>"</span><span>;  <br>   </span><span>9</span><span>.</span><span> } </span><span>else</span><span> {  <br>  </span><span>10</span><span>.</span><span>echo</span><span>"</span><span>Key does not exist!</span><span>"</span><span>;  <br>  </span><span>11</span><span>.</span><span> } </span></p>

  9、搜索数组

  你可能想搜索数组资源,这样用户就可以方便地用一个特定的州府检索关联的州,可以通过array_search()函数实现数组搜索。

<p><span>1</span><span>.</span><span>$capitals</span><span>=</span><span>array</span><span>(  <br>   </span><span>2</span><span>.</span><span>'</span><span>Arizona</span><span>'</span><span>=></span><span>'</span><span>Phoenix</span><span>'</span><span>,</span><span>  <br>   </span><span>3</span><span>.</span><span>'</span><span>Alaska</span><span>'</span><span>=></span><span>'</span><span>Juneau</span><span>'</span><span>,</span><span>  <br>   </span><span>4</span><span>.</span><span>'</span><span>Alabama</span><span>'</span><span>=></span><span>'</span><span>Montgomery</span><span>'</span><span>  <br>   </span><span>5</span><span>.</span><span> );  <br>   </span><span>6</span><span>.</span><span>$state</span><span>=</span><span>array_search</span><span>(</span><span>'</span><span>Juneau</span><span>'</span><span>,</span><span>$capitals</span><span>);  <br>   </span><span>7</span><span>.</span><span>//</span><span> $state = 'Alaska' </span></p>

  10、标准PHP库

  标准PHP库(Standard PHP Library,SPL)为开发人员提供了许多数据结构,迭代器,接口,异常和其它以前PHP语言没有的功能,使用这些功能可以通过面向对象的语法遍历数组。

<p><span>1</span><span>.</span><span>$capitals</span><span>=</span><span>array</span><span>(  <br>   </span><span>2</span><span>.</span><span>'</span><span>Arizona</span><span>'</span><span>=></span><span>'</span><span>Phoenix</span><span>'</span><span>,</span><span>  <br>   </span><span>3</span><span>.</span><span>'</span><span>Alaska</span><span>'</span><span>=></span><span>'</span><span>Juneau</span><span>'</span><span>,</span><span>  <br>   </span><span>4</span><span>.</span><span>'</span><span>Alabama</span><span>'</span><span>=></span><span>'</span><span>Montgomery</span><span>'</span><span>  <br>   </span><span>5</span><span>.</span><span> );  <br>   </span><span>6</span><span>.</span><span>$arrayObject</span><span>=</span><span>new</span><span> ArrayObject(</span><span>$capitals</span><span>);  <br>   </span><span>7</span><span>.</span><span>foreach</span><span> (</span><span>$arrayObject</span><span>as</span><span>$state</span><span>=></span><span>$capital</span><span>)  <br>   </span><span>8</span><span>.</span><span> {  <br>   </span><span>9</span><span>.</span><span>printf</span><span>(</span><span>"</span><span>The capital of %s is %s<br></span><span>"</span><span>,</span><span>$state</span><span>,</span><span>$capital</span><span>);  <br>  </span><span>10</span><span>.</span><span> }  <br>  </span><span>11</span><span>.</span><span>//</span><span> The capital of Arizona is Phoenix  </span><span><br></span><span>12</span><span>.</span><span>//</span><span> The capital of Alaska is Juneau  </span><span><br></span><span>13</span><span>.</span><span>//</span><span> The capital of Alabama is Montgomery </span></p>

  这仅仅是SPL众多伟大功能中的一个,一定要阅读PHP文档了解更多信息。

http://www.phpbuilder.com/columns/associative-array-manipulation/Jason_Gilmore12092010.php3

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。