Say I have this array

说我有这个数组

$array = array('pen' => 'blue', 'paper' => 'red', 'ink' => 'white');

When I loop through it

当我循环它

$string = '';
foreach ($array AS $key=>$value) {
    $string .= $key . ' = ' . $value;
}

I want to get the "line number" of the element the loop is currently on.

我想得到循环当前所在元素的“行号”。

If the loop is on "pen" I would get 1. If the loop is on "paper" I would get 2. If the loop is on "ink" I would get 3.

如果循环是“笔”,我会得到1.如果循环是“纸”我会得到2.如果循环是“墨水”,我会得到3。

Is there an array command for this?

这有一个数组命令吗?

3 个解决方案

#1


6

No. You will have to increment an index counter manually:

不可以。您必须手动递增索引计数器:

$string = '';
$index = 0;
foreach ($array as $key=>$value) {
    $string .= ++$index . ") ". $key . ' = ' . $value;
}

更多相关文章

  1. PHP用空格分割文本为数组的方法
  2. 如何使用ChromePhp Logger记录数组
  3. 在数组中计数,其中值为0
  4. 如何将PHP数组的关联数组转移到javascript?
  5. PHP - 将字符串转换为键值数组
  6. 如何在不更改其键值的情况下删除数组的第一个元素?
  7. 以完整二叉树,数组格式获取所有节点
  8. php 写一个算法,查寻数组里第一个大于某数的值。
  9. 插入PHP数组作为MYSQL列

随机推荐

  1. Python MySQLdb连接数据库的应用
  2. Effective MySQL之深入解析复制技术
  3. 在arcpy中删除或删除表的代码是什么?
  4. oracle11g 创建id自增长监听器的步骤
  5. 在SQL SELECT语句中重用别名字段
  6. 用javabean连接sql server 2000数据库报
  7. CentOS下MySQL主从同步配置 ​Slave_IO_R
  8. 使用IN语句缓慢mysql删除查询
  9. mysql的replace的使用
  10. 无需安装ORACLE远程连接SQLPLUS以及PL/SQ