使用=>在PHP中 [英] Use of => in PHP

查看:75
本文介绍了使用=>在PHP中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这在PHP中意味着什么,什么时候使用它?

What does this mean in PHP and when is the time to use it?

 =>

另一个例子.

 foreach ($parent as $task_id => $todo)

推荐答案

详细说明已经说过的话.

To elaborate a bit on what has already been said.

假设您了解PHP中的数组.这实际上是一种在给定索引的情况下对同一变量下的项列表"进行分组的方法,通常是从0开始的数字整数索引.假设我们要为索引创建一个英文术语列表,即

Assuming that you know about arrays in PHP. Which is really a way of grouping a "list" of items under the same variable given a certain index - normally a numeric integer index starting from 0. Say we want to make a list of the indexes English term, that is,

Zero
One
Two
Three
Four
Five

可以使用数组在PHP中表示它,如下所示:

Representing this in PHP using an array could be done like so:

$numbers = array("Zero", "One", "Two", "Three", "Four", "Five");

现在,如果我们想要相反的情况怎么办?以零"作为键并以0作为值?在PHP中将非整数作为数组的键称为关联数组,其中每个元素都是使用键=>值"的语法定义的,因此在我们的示例中:

Now, what if we wanted the reverse situation? Having "Zero" as key and 0 as value? Having a non-integer as a key of an array in PHP is called an associative array where each element is defined using the syntax of "key => value", so in our example:

$numbers = array("Zero" => 0, "One" => 1, "Two" => 2, "Three" => 3, "Four" => 4, "Five" => 5);

现在的问题变成了:使用foreach语句时,如果您同时想要键和值怎么办?答:语法相同!

The question now becomes: What if you want both the key and the value when using a foreach statement? Answer: the same syntax!

$numbers = array("Zero" => 0, "One" => 1, "Two" => 2, "Three" => 3, "Four" => 4, "Five" => 5);

foreach($numbers as $key => $value){
    echo "$key has value: $value\n";
}

这将显示

Zero has value: 0
One has value: 1
Two has value: 2
Three has value: 3
Four has value: 4
Five has value: 5

这篇关于使用=>在PHP中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆