我有一个名为$allFeeds
的关联数组(在执行print_r($allFeeds);
之后),如下所示:
注意:实际的关联数组$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>
)
)
)
首先,我想检查关键数组$ allFeeds的内部数组中是否存在键['feed_image']
。如果它存在,则通过更改每个数组元素的图像路径来转换数组$allFeeds[$key]['feed_image']
,如下所示:
注意:我想根据每个标记的'src'属性值创建新数组。在这种情况下你也可以观察到。
Array
(
[0] => Array
(
[feed_image] => Array
(
[0] => 2015/04/9bd387c6442135834298d6a17b3f9555%s.jpg
[1] => 2015/04/2015/04/6eb60ee0e258223ef72a9a632d0ce429%s.png
)
)
)
我尝试了以下代码,但对我来说没有用。我不明白我在做什么错。如果有人帮助我纠正错误并获得所需的输出,那就太好了。
$cnt = 0;
foreach($allFeeds as $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++;
}
有人可以通过为我所面临的问题提供可靠有效的解决方案来帮助我吗?
提前致谢。
答案 0 :(得分:1)
您的代码中有一些小错误,请参阅以下调整:
$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 be removed',
)
)
);
$cnt = 0;
foreach($allFeeds as $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);
输出:
Array
(
[0] => Array
(
[feed_image] => Array
(
[0] => 2015/04/9bd387c6442135834298d6a17b3f9555_240.jpg
[1] => 2015/04/6eb60ee0e258223ef72a9a632d0ce429_240.png
)
)
)