我如何使用php数组获取此值 [英] how do i get this values using php arrays
本文介绍了我如何使用php数组获取此值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
[
["STRG008c0",2,0,"orange","***STRG008*#*"],
["STRG009c0",3,0,"orange","***STRG009*#*"],
["STRG001c0",2,0,"green","***STRG001*#*"],
["STRG003c0",3,0,"green","***STRG003*#*"],
["STRG002c0",4,0,"green","***STRG002*#*"]
]
如何在PHP中循环获取以下内容?
How do I get the below in a loop in PHP?
***STRG008*#*
***STRG009*#*
***STRG001*#*
***STRG003*#*
***STRG002*#*
推荐答案
您似乎希望最后一列的值作为字符串。
It looks like you want the last column values as a string.
方法1:
在一个新变量中收集每个子数组的所有最后值,例如 $ result
和 implode()
之后。
Collect all last values of each subarray in a new variable, say $result
and implode()
them later.
<?php
$array = [
["STRG008c0",2,0,"orange","***STRG008*#*"],
["STRG009c0",3,0,"orange","***STRG009*#*"],
["STRG001c0",2,0,"green","***STRG001*#*"],
["STRG003c0",3,0,"green","***STRG003*#*"],
["STRG002c0",4,0,"green","***STRG002*#*"]
];
$result = [];
foreach ($array as $subarray) {
$result[] = end($subarray);
}
$result = implode(",",$result);
echo $result;
方法2:
您可以使用 array_column()
仅过滤最后一列的值,稍后过滤 implode()
。
You can use array_column()
to filter only last column values and implode()
them later. This would be a one-liner.
<?php
$array = [
["STRG008c0",2,0,"orange","***STRG008*#*"],
["STRG009c0",3,0,"orange","***STRG009*#*"],
["STRG001c0",2,0,"green","***STRG001*#*"],
["STRG003c0",3,0,"green","***STRG003*#*"],
["STRG002c0",4,0,"green","***STRG002*#*"]
];
$result = implode(",", array_column($array,4));
echo $result;
这篇关于我如何使用php数组获取此值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文