I asked this question before, Here however I think I presented the problem poorly, and got quite a few replies that may have been useful to someone but did not address the actual question and so I pose the question again.

之前我问过这个问题,但是我认为我提出的问题很糟糕,并得到了一些可能对某些人有用但却没有解决实际问题的回复,所以我再次提出问题。

Is there a single-line native method in php that would allow me to do the following. Please, please, I understand there are other ways to do this simple thing, but the question I present is does something exist natively in PHP that will grant me access to the array values directly without having to create a temporary array.

在php中是否有单行本机方法可以让我执行以下操作。请,我知道还有其他方法可以做这个简单的事情,但我提出的问题是在PHP本身存在的东西,它将授予我直接访问数组值而无需创建临时数组。

$rand_place = explode(",",loadFile("csvOf20000places.txt")){rand(0,1000)};

This is a syntax error, however ideally it would be great if this worked!

这是一个语法错误,但理想情况下如果这有效会很好!

Currently, it seems unavoidable that one must create a temporary array, ie

目前,似乎不可避免地必须创建一个临时数组,即

The following is what I want to avoid:

以下是我想要避免的:

$temporary_places_array = explode(",",loadFile("csvOf20000places.txt"));
$rand_place = $temporary_places_array[rand(0,1000)];

Also, i must note that my actual intentions are not to parse strings, or pull randomly from an array. I simply want access into the string without a temporary variable. This is just an example which i hope is easy to understand. There are many times service calls or things you do not have control over returns an array (such as the explode() function) and you just want access into it without having to create a temporary variable.

另外,我必须注意,我的实际意图不是解析字符串,也不是从数组中随机拉取。我只是想要访问没有临时变量的字符串。这只是一个我希望易于理解的例子。有很多次服务调用或你无法控制的东西返回一个数组(例如explode()函数),你只需要访问它而不必创建一个临时变量。

NATIVELY NATIVELY NATIVELY, i know i can create a function that does it.

NATIVELY NATIVELY NATIVELY,我知道我可以创建一个功能。

7 个解决方案

#1


3

No, there is no way to do that natively.

不,本机没有办法做到这一点。

You can, however:

但是你可以:

1.- Store the unavoidable array instead of the string. Given PHP's limitation this is what makes most sense in my opinion.

1.-存储不可避免的数组而不是字符串。鉴于PHP的限制,这是我认为最有意义的。

Also, don't forget you can unset()

另外,不要忘记你可以取消设置()

2.- Use strpos() and friends to parse the string into what you need, as shown in other answers I won't paste here.

2.-使用strpos()和朋友将字符串解析为您需要的内容,如其他答案中所示,我不会在此处粘贴。

3.- Create a function.

3.-创建一个功能。

更多相关文章

  1. 快速使用数组的最近元素来确定新元素是否唯一
  2. 如何在php数组中获取php键值
  3. 将SQL查询的SELECT子句解析为PHP数组
  4. PHP不区分大小写数组搜索函数[duplicate]
  5. 如何找到数组的大小
  6. PHP:将simpleXML对象转换为二维数组
  7. PHP是否以不同方式(内部)处理数字索引数组?
  8. 从JSON字符串/数组中提取第一个图像
  9. 测试一个字符串是否包含PHP中的单词?

随机推荐

  1. android剪切合并MP3音乐
  2. 【Android开发环境安装】
  3. 【066】Android & Java 难点解析
  4. Android Camera调用流程
  5. Android修改gen下包名
  6. Android: Android Wifi System
  7. android WebView loadData不能解析
  8. 本博博文预报
  9. 如何去写 Android(安卓)init.rc (Android
  10. 开启 Android 反射调用