我可以收集爆炸然后再次爆炸吗?
我正在尝试使用文本文件创建数据保存变量。我读到 Laravel 中的收集函数是管理数据的正确选择。
我想要做的是用“|”再次爆炸每个数组,所以我的代码是这样的,但它不能。
$temps=explode(PHP_EOL,$note);
$isi=collect([
explode('|',$temps)
]);
此代码仅在伴随如下手动数组时才能应用:
$isi=collect([
explode('|',$temps[0]),
explode('|',$temps[2])
]);
I'm trying to create data save variable using a text file. I read that the collect function in laravel is the right choice for managing the data.
Here is the data saved after I exploded with PHP_EOL :
What I want to do is explode once more per array with "|", so my code is like this but it can't.
$temps=explode(PHP_EOL,$note);
$isi=collect([
explode('|',$temps)
]);
This code can only be applied if it is accompanied by a manual array like this :
$isi=collect([
explode('|',$temps[0]),
explode('|',$temps[2])
]);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您已经解决了, explode 返回一个数组,但是接受字符串,因此您无法将
explode
的结果直接传递给另一个explode
调用。相反,您需要获取第一个
explode
生成的数组中的每个单独字符串,并分别explode
每个字符串。显然,尝试对数组的每个项目进行硬编码(根据您的尝试)是不切实际的,因此您可以使用循环来获取每个项目,或者稍微简洁的方法是使用 array_map,如以下示例所示:
演示:https://3v4l.org/EM6Ct
这将从第一个数组中获取每个字符串,并将其传递给
pipelineExplode
函数从中获取一个新数组,然后将该数组添加回由array_map
函数返回的外部数组中。As I think you've worked out, explode returns an array but accepts a string, so you can't pass the result of
explode
directly to anotherexplode
call.Instead you need to get each individual string in the array produced by the first
explode
, andexplode
each one separately.Clearly it's not practical to try and hard-code a reference to each item of the array (as per your attempt), so instead you can use a loop to fetch each item, or a slightly neater way is to use array_map, like in the following example:
Demo: https://3v4l.org/EM6Ct
This will take each string from the first array, pass it through the
pipeExplode
function to get a new array back from that, and then add that array back into the outer array which is returned by thearray_map
function.