遍历数组对象_遍历数组

遍历数组对象

遍历数组对象

The certification guide demonstrates what is probably the fastest (performance-wise) way to go through the elements of an array - using the array_walk() function. The example in the guide is using a callback function to use the elements of the array. But there's more than that.

该认证指南演示了使用array_walk()函数可能是最快的(性能方面)遍历数组元素的方法。 本指南中的示例使用回调函数来使用数组的元素。 不仅如此。

Using the array_walk() function you can also make use of the keys of that array. And you can also pass to the walking callback function some values you may find appropriate - these are known as "user-defined" parameters.

使用array_walk()函数,您还可以使用该数组的键。 您还可以将一些您可能认为合适的值传递给walking回调函数,这些值称为“用户定义”参数。

An example that uses keys and values: < ?php // here's an array $french_numbers = array('one' => 'une', 'two' => 'deux', 'three' => 'trois');

使用键和值的示例:<?php //这是一个数组$ french_numbers = array('one'=>'une','two'=>'deux','three'=>'trois');

// the function we're going to call back function dumpEm($value, $key) { echo $key . ' - ' . $value . ''; }

//我们要回调的函数dumpEm($ value,$ key){echo $ key。 '-'。 $ value。 ''; }

// call array_walk() array_walk($french_numbers, 'dumpEm'); ?>This example will produce: one - une two - deux three - trois

//调用array_walk()array_walk($ french_numbers,'dumpEm'); ?>此示例将产生: one - une two - deux three - trois

You can see how the keys were also used. Now for the user-defined data you can pass to your callback function. This data can be of any type. Let's take an array as an example. < ?php // here's an array $french_numbers = array('one' => 'une', 'two' => 'deux', 'three' => 'trois');

您可以查看如何使用这些键。 现在,对于用户定义的数据,您可以传递给回调函数。 该数据可以是任何类型。 让我们以一个数组为例。 <?php //这是一个数组$ french_numbers = array('one'=>'une','two'=>'deux','three'=>'trois');

// the function we're going to call back function dumpEm($value, $key, $legend) { echo $key . '('. $legend['en'] .') - ' . $value . '('. $legend['fr'] .')'; }

//我们要回调的函数dumpEm($ value,$ key,$ legend){echo $ key。 '('。$ legend ['en']。')-'。 $ value。 '('。$ legend ['fr']。')'; }

// call array_walk() array_walk($french_numbers, 'dumpEm', array('en' => 'English', 'fr' => 'Fran?�ais')); ?> This will produce: one(English) - une(Fran?�ais) two(English) - deux(Fran?�ais) three(English) - trois(Fran?�ais)

//调用array_walk()array_walk($ french_numbers,'dumpEm',array('en'=>'English','fr'=>'Fran?ais')); ?>这将产生: one(English) - une(Fran? ais) two(English) - deux(Fran? ais) three(English) - trois(Fran? ais)

Finally, you can change the array values you pass to the callback function. To do so, you need to pass those elements by reference. Note that you cannot do the same on the array keys though. < ?php // here's an array $french_numbers = array('one' => 'une', 'two' => 'deux', 'three' => 'trois');

最后,您可以更改传递给回调函数的数组值。 为此,您需要通过引用传递这些元素。 请注意,尽管如此,您不能对阵列键执行相同的操作。 <?php //这是一个数组$ french_numbers = array('one'=>'une','two'=>'deux','three'=>'trois');

// the function we're going to call back function dumpEm(&$value, &$key) { $value = ucfirst($value); $key = ucfirst($key); echo $key . ' - ' . $value . ''; }

//我们要回调的函数dumpEm(&$ value,&$ key){$ value = ucfirst($ value); $ key = ucfirst($ key); 回声$ key。 '-'。 $ value。 ''; }

// call array_walk() array_walk($french_numbers, 'dumpEm');

//调用array_walk()array_walk($ french_numbers,'dumpEm');

print_r($french_numbers); ?>This will give you: One - Une Two - Deux Three - Trois Array ( [one] => Une [two] => Deux [three] => Trois )

print_r($ french_numbers); ?>这会给您: One - Une Two - Deux Three - Trois Array ( [one] => Une [two] => Deux [three] => Trois )

Tell your friends about this post on Facebook and Twitter

FacebookTwitter上告诉您的朋友有关此帖子的信息

翻译自: https://www.phpied.com/walking-the-array/

遍历数组对象

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值