I am attempting to extract image URLs from the following 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"}]

From these I would generate an array.

From the resulting array, I would then prepend a prefix to all the array values, the prefix being "https://www.dickerdata.com.au".

Thus I have devised the following function:

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 );
}

Except that the resulting output has one superfluous instance of a prefix at the very end of the array. It seems to be prefixing blank:

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

It's like there's an extra empty value in the array at the end? Why is this happening?

Edit:

Alternatively, I have used array_walk instead, with the same result

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 Answers

1
tizi4n On Best Solutions

You could add the string prefix in the for loop:

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

you need to change your for loop to $i < count( $img_array ) (not <=)

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

edit: The reason for the empty value is exactly for for loop <= (it runs the loop when $i is the same as the value of count, but array starts at zero, so it does not possess a key at count($array) (count will be 100, array ist from 0 to 99)