如何在数组输出中的下划线之前抓取一个单词
这是我当前的数组,它保留值中的第一个字符
Array ( [n] => Array ( [0] => name_john ) [a] => Array ( [0] => age_30 ) )
foreach ($queryArray as $key) {
$inArray[$key[0]][] = $key;
}
问题:如何在下划线之前抓取一个单词并将数组更改为类似
Array
(
[name] => Array
(
[0] => name_john
)
[age] => Array
(
[0] => age_30
)
)
This is my current array which keep a first character from the value
Array ( [n] => Array ( [0] => name_john ) [a] => Array ( [0] => age_30 ) )
foreach ($queryArray as $key) {
$inArray[$key[0]][] = $key;
}
Question : How do I grab a word before underscore and change the array to be like
Array
(
[name] => Array
(
[0] => name_john
)
[age] => Array
(
[0] => age_30
)
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
要解析下划线之前的单词,请使用explode:
To parse out the word before the underscore, use explode:
explode('_', $value)
并获取返回数组的第一项。工作示例: ( <一href="http://codepad.org/tmSJEUmH" rel="nofollow noreferrer">此处为键盘)
输出:
explode('_', $value)
and get the first item of the returned array.Working example: (codepad here)
Output:
但是如果你使用“key”作为关联来创建数组。索引,就像
然后你可以
并且你应该得到一个像
这样的数组虽然它不是“所需的”输出,但对于你正在做的事情来说可能更干净,更容易使用。
But if you make the array using the "key" as the assoc. index, like
Then you can
and you should get an array like
Tho its not the "desired" output, might be cleaner and easier to use for what it seems like you're doing.
尝试将其放入您的 foreach 语句中:)
Try put this inside your foreach statement :)