我正在尝试从以下 json 中提取图像 URL - let $img_json =
[{"multi_images":"","multi_images-data-zoom-image":"/Download/GetImage?encryptedURL=rA8HfbBpHU%2FJmnYa9AR4eIXySIKvdCBw%2BU0V0Y5e1tvw7%2BmfoZu8UaDNSsGcj5j0sBpr5sFzCccdMJAtF4s%2F8kDl3l7cB%2Fmg6Iwe5WT24hk%3D"},{"multi_images":"","multi_images-data-zoom-image":"/Download/GetImage?encryptedURL=rA8HfbBpHU%2FJmnYa9AR4eIXySIKvdCBwNSJDgIu25iaZFDdDOJGvqPHPuQLDTNDcqsaQPmMVW0RRaU9J%2FZuIdrEgh%2FX8Lgcr6Iwe5WT24hk%3D"},{"multi_images":"","multi_images-data-zoom-image":"/Download/GetImage?encryptedURL=rA8HfbBpHU%2FJmnYa9AR4eIXySIKvdCBwg1cMGFDnzjB9VGn8rucwG%2Fs8Snz%2BcSYyPaEAAfuwOikyZXPowLE6pZGHf%2FfvE4Hi6Iwe5WT24hk%3D"},{"multi_images":"","multi_images-data-zoom-image":"/Download/GetImage?encryptedURL=rA8HfbBpHU%2FJmnYa9AR4eIXySIKvdCBwKGGA7PJ5ECXgJeuxqzZBuF%2BOd2h%2FsLUgxaKeof%2BN8rl6Y03aBgKZNSzeKHO6FcK26Iwe5WT24hk%3D"},{"multi_images":"","multi_images-data-zoom-image":"/Download/GetImage?
从这些我会生成一个数组。
从结果数组中,我将在所有数组值前添加前缀,前缀为“ https://www.dickerdata.com.au ”。
因此,我设计了以下功能:
function my_get_images_dd( $img_json ) {
$img_array = json_decode( $img_json, 1 );
$all_imgs = array();
for ( $i = 0; $i <= count( $img_array ); $i++ ) {
$all_imgs[] = $img_array[ $i ]['multi_images-data-zoom-image'];
}
/*Prepend URL domain*/
/* https://stackoverflow.com/a/28115783 */
$prefixed_array = preg_filter('/^/', 'https://www.dickerdata.com.au', $all_imgs);
return implode( ",", $prefixed_array );
}
就像最后数组中有一个额外的空值?为什么会这样?
编辑:
或者,我array_walk改用了,结果相同
function my_get_images_dd( $img_json ) {
$img_array = json_decode( $img_json, 1 );
$all_imgs = array();
for ( $i = 0; $i <= count( $img_array ); $i++ ) {
$all_imgs[] = $img_array[ $i ]['multi_images-data-zoom-image'];
}
/*Prepend URL domain*/
/* */
array_walk($all_imgs, function(&$item) { $item = 'https://www.dickerdata.com.au'.$item; });
return implode( ",", $all_imgs );
}