Json 数组访问 PHP 中的某些元素

丹尼尔

从文件上传脚本中,我收到了一个字符串,我认为它是一个 json 格式的多维数组。

$string =

'[{"file":"filename_xyz1.png","index":0},{"file":"filename_xyz2.png","index":1},{"file":"filename_xyz3.png","index":2}]'

在 PHP 中,我只想将元素“文件”的值放入 PHP 数组中,结果是:

array("filename_xyz1.png", "filename_xyz2.png", "filename_xyz3.png");

先回声也会有帮助,所以我从解码 json 开始:

$array_files = json_decode($string);

foreach ($array_files as $key => $filename){
    echo $filename['file'];
}

虽然不幸的是最后一部分没有成功。

B001ᛦ

我只想将元素“文件”的值放入 PHP 数组

当您期待一个数组时,您可能会在以下位置使用assoc参数json_decode()

$array_files = json_decode($string, true); 

这将返回一个数组作为结果

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章