preg_filter-implode函数导致其他空数组值

时间:2019-05-10 05:19:46

标签: php arrays implode

我正在尝试从以下json中提取图片网址-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?encryptedURL=rA8HfbBpHU%2FJmnYa9AR4eIXySIKvdCBwMcRNYUwBVM2vikrtBskkx4DcVDc%2BovLdZmEhvdbv8FLux%2FeubdmTbYiAqC5QnV9v6Iwe5WT24hk%3D"},{"multi_images":"","multi_images-data-zoom-image":"/Download/GetImage?encryptedURL=rA8HfbBpHU%2FJmnYa9AR4eIXySIKvdCBwUKSjnM%2F5nWK7mCv2Z6pVopKCHhtzQeUci%2FAmjauDBn7H%2B6rL7rmQCWU1G6qrWlNw6Iwe5WT24hk%3D"},{"multi_images":"","multi_images-data-zoom-image":"/Download/GetImage?encryptedURL=rA8HfbBpHU%2FJmnYa9AR4eIXySIKvdCBwJiL2Snf2V3JXH4QaHPSMvCgPHPbC68zHS%2BBnOfLkYDn8f9jvUcYgmsYJEKqlIr%2FK6Iwe5WT24hk%3D"},{"multi_images":"","multi_images-data-zoom-image":"/Download/GetImage?encryptedURL=rA8HfbBpHU%2FJmnYa9AR4eIXySIKvdCBwQybUaXaW0RsKk4MVropXFa2q28e8OYUSoyHMstGjpDLuqAN%2BudRWfSxlwgVRGvHo6Iwe5WT24hk%3D"},{"multi_images":"","multi_images-data-zoom-image":"/Download/GetImage?encryptedURL=rA8HfbBpHU%2FJmnYa9AR4eIXySIKvdCBwfa%2BpAsLmVxryvGCXz93nePEFwwnxdYO77XVy5fiXo%2Bf1B9l9C0CegZiiw4mofoa76Iwe5WT24hk%3D"},{"multi_images":"","multi_images-data-zoom-image":"/Download/GetImage?encryptedURL=rA8HfbBpHU%2FJmnYa9AR4eIXySIKvdCBwG41ol3y4AoDGIE1TCXznXSIWpbN9ANuIDtAmwZegq83opEW%2FN01wBJ86IW0CIYWI6Iwe5WT24hk%3D"}]

从这些我将生成一个数组。

然后从结果数组中为所有数组值添加一个前缀,该前缀为“ 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 );
}

除了结果输出在数组的最末端有一个多余的前缀实例。似乎是空白前缀:

https://www.dickerdata.com.au/Download/GetImage?encryptedURL=rA8HfbBpHU%2FJmnYa9AR4eIXySIKvdCBw%2BU0V0Y5e1tvw7%2BmfoZu8UaDNSsGcj5j0sBpr5sFzCccdMJAtF4s%2F8kDl3l7cB%2Fmg6Iwe5WT24hk%3D,https://www.dickerdata.com.au/Download/GetImage?encryptedURL=rA8HfbBpHU%2FJmnYa9AR4eIXySIKvdCBwNSJDgIu25iaZFDdDOJGvqPHPuQLDTNDcqsaQPmMVW0RRaU9J%2FZuIdrEgh%2FX8Lgcr6Iwe5WT24hk%3D,https://www.dickerdata.com.au/Download/GetImage?encryptedURL=rA8HfbBpHU%2FJmnYa9AR4eIXySIKvdCBwg1cMGFDnzjB9VGn8rucwG%2Fs8Snz%2BcSYyPaEAAfuwOikyZXPowLE6pZGHf%2FfvE4Hi6Iwe5WT24hk%3D,https://www.dickerdata.com.au/Download/GetImage?encryptedURL=rA8HfbBpHU%2FJmnYa9AR4eIXySIKvdCBwKGGA7PJ5ECXgJeuxqzZBuF%2BOd2h%2FsLUgxaKeof%2BN8rl6Y03aBgKZNSzeKHO6FcK26Iwe5WT24hk%3D,https://www.dickerdata.com.au/Download/GetImage?encryptedURL=rA8HfbBpHU%2FJmnYa9AR4eIXySIKvdCBwMcRNYUwBVM2vikrtBskkx4DcVDc%2BovLdZmEhvdbv8FLux%2FeubdmTbYiAqC5QnV9v6Iwe5WT24hk%3D,https://www.dickerdata.com.au/Download/GetImage?encryptedURL=rA8HfbBpHU%2FJmnYa9AR4eIXySIKvdCBwUKSjnM%2F5nWK7mCv2Z6pVopKCHhtzQeUci%2FAmjauDBn7H%2B6rL7rmQCWU1G6qrWlNw6Iwe5WT24hk%3D,https://www.dickerdata.com.au/Download/GetImage?encryptedURL=rA8HfbBpHU%2FJmnYa9AR4eIXySIKvdCBwJiL2Snf2V3JXH4QaHPSMvCgPHPbC68zHS%2BBnOfLkYDn8f9jvUcYgmsYJEKqlIr%2FK6Iwe5WT24hk%3D,https://www.dickerdata.com.au/Download/GetImage?encryptedURL=rA8HfbBpHU%2FJmnYa9AR4eIXySIKvdCBwQybUaXaW0RsKk4MVropXFa2q28e8OYUSoyHMstGjpDLuqAN%2BudRWfSxlwgVRGvHo6Iwe5WT24hk%3D,https://www.dickerdata.com.au/Download/GetImage?encryptedURL=rA8HfbBpHU%2FJmnYa9AR4eIXySIKvdCBwfa%2BpAsLmVxryvGCXz93nePEFwwnxdYO77XVy5fiXo%2Bf1B9l9C0CegZiiw4mofoa76Iwe5WT24hk%3D,https://www.dickerdata.com.au/Download/GetImage?encryptedURL=rA8HfbBpHU%2FJmnYa9AR4eIXySIKvdCBwG41ol3y4AoDGIE1TCXznXSIWpbN9ANuIDtAmwZegq83opEW%2FN01wBJ86IW0CIYWI6Iwe5WT24hk%3D,https://www.dickerdata.com.au

这就像数组末尾有一个额外的空值吗?为什么会这样?

编辑:

或者,我改用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 );
}

1 个答案:

答案 0 :(得分:1)

您可以在for循环中添加字符串前缀:

$all_imgs[] = 'https://www.dickerdata.com.au'.$img_array[ $i ]['multi_images-data-zoom-image'];

您需要将for循环更改为$ i << / strong> count($ img_array)(不是<=)

for ( $i = 0; $i < count( $img_array ); $i++ ) {
              $all_imgs[] = $img_array[ $i ]['multi_images-data-zoom-image'];
   }

编辑: 空值的原因正好是for循环<=((当$ i与count的值相同时,它将运行循环,但是array从零开始,因此它在count($ array)处没有键( count为100,数组ist从0到99)