php - Why the logic for array conversion and HTML parsing is not working in following scenario? -


i've associative array titled $allfeeds (after executing print_r($allfeeds);) follows :

note : actual associative array $allfeeds large. understanding purpose i've put 1 element large array.

   array    (       [0] => array       (          [feed_image] => array          (            [0] => <a href="http://52.1.47.143/photo/928/2_onclick_ok/userid_244/"  class=" js_photo_item_928  photo_holder_image" rel="928" ><img src="http://52.1.47.143/file/pic/photo/2015/04/9bd387c6442135834298d6a17b3f9555_240.jpg"  alt=""  width="180"  height="160"  class="photo_holder" /></a><br />            [1] => <a href="http://52.1.47.143/photo/927/8/userid_244/" class=" js_photo_item_928  photo_holder_image" rel="927"><img src="http://52.1.47.143/file/pic/photo/2015/04/6eb60ee0e258223ef72a9a632d0ce429_240.png"  alt="" height="84" width="150"  class="photo_holder"  userid="244" /></a>          )       )     ) 

first of want check whether key ['feed_image'] present in inner array of associative array $allfeeds. if it's present convert array $allfeeds[$key]['feed_image'] changing image path of each array element follows :

note : want create new array 'src' attribute's value of each tag. can observe in case too.

   array    (       [0] => array       (          [feed_image] => array          (            [0] => 2015/04/9bd387c6442135834298d6a17b3f9555%s.jpg            [1] => 2015/04/2015/04/6eb60ee0e258223ef72a9a632d0ce429%s.png          )       )     ) 

i tried following code didn't work out me. don't understand mistake i'm making. great if me in correcting mistake , desired output.

$cnt = 0; foreach($allfeeds $key => $value) {   if(isset($allfeeds[$cnt]['feed_image']) && is_array($allfeeds[$cnt]['feed_image'])) {     $allfeeds[$cnt]['feed_image'][$key] = array_map(function($item) {       $dom = new domdocument;       $dom->loadhtml($item);       $src = simplexml_import_dom($dom)->xpath('//img/@src');       if(!isset($src[0])) {         return false;       }       $components = explode('photo/', $src[0]);                     return end($components);               }, $feed['feed_image']);     $allfeeds[$cnt]['feed_image'][$key] = array_filter($allfeeds[$cnt]['feed_image'][$key]);   }   $cnt++; } 

can please me providing reliable , efficient solution issue i'm facing?

thanks in advance.

you have small bugs in code, see adjustments below:

$allfeeds = array (     0 => array     (         'feed_image' => array         (             0 => '<a href="http://52.1.47.143/photo/928/2_onclick_ok/userid_244/"  class=" js_photo_item_928  photo_holder_image" rel="928" ><img src="http://52.1.47.143/file/pic/photo/2015/04/9bd387c6442135834298d6a17b3f9555_240.jpg"  alt=""  width="180"  height="160"  class="photo_holder" /></a><br />',             1 => '<a href="http://52.1.47.143/photo/927/8/userid_244/" class=" js_photo_item_928  photo_holder_image" rel="927"><img src="http://52.1.47.143/file/pic/photo/2015/04/6eb60ee0e258223ef72a9a632d0ce429_240.png"  alt="" height="84" width="150"  class="photo_holder"  userid="244" /></a>',             2 => 'no image, should removed',         )     ) );  $cnt = 0; foreach($allfeeds $key => $value) {   if(isset($allfeeds[$cnt]['feed_image']) && is_array($allfeeds[$cnt]['feed_image'])) {     // $allfeeds[$cnt]['feed_image'][$key] = array_map(function($item) {     $allfeeds[$cnt]['feed_image'] = array_map(function($item) {       $dom = new domdocument;       $dom->loadhtml($item);       $src = simplexml_import_dom($dom)->xpath('//img/@src');       if(!isset($src[0])) {         return false;       }       $components = explode('photo/', $src[0]);         return end($components);       // }, $feed['feed_image']);       }, $allfeeds[$cnt]['feed_image']);        // $allfeeds[$cnt]['feed_image'][$key] = array_filter($allfeeds[$cnt]['feed_image'][$key]);       $allfeeds[$cnt]['feed_image'] = array_filter($allfeeds[$cnt]['feed_image']);   }   $cnt++; }  print_r($allfeeds); 

output:

array (     [0] => array         (             [feed_image] => array                 (                     [0] => 2015/04/9bd387c6442135834298d6a17b3f9555_240.jpg                     [1] => 2015/04/6eb60ee0e258223ef72a9a632d0ce429_240.png                 )          )  ) 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -