简体   繁体   English

通过索引访问PHP字符串数组

[英]Access PHP array of strings by index

I have a PHP array like 我有一个类似的PHP数组

$arr = array("c_m_email" => "a@b.z");

and I can access the email by 我可以通过以下方式访问电子邮件

$arr['c_m_email'] 

But is there another way to just write $arr[0] ? 但是还有另一种方式可以只写$arr[0]吗?

Use array_values() 使用array_values()

array_values() returns all the values from the array and indexes the array numerically. array_values()返回数组中的所有值,并对数组进行数字索引。

Manual: http://php.net/manual/en/function.array-values.php 手册: http//php.net/manual/en/function.array-values.php

If you want the first entry in the array, you could also do this: 如果要数组中的第一个条目,也可以这样做:

reset($h);
echo current($h)
    $i=0;
    $data=array();

   foreach($arr as $value){

       $data[$i] = $value;

       $i++;

    }

    print_r($data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM