0

我有一个关联数组,标题$data如下:

Array
(
[0] => Array
        (
            [item_id] => 170
            [item_title] => Master of Engineering
            [item_time_stamp] => 1421152531
            [item_user_id] => 244
            [item_type_id] => pages
            [item_photo] => 169
[profile_image] => <img src="http://54.174.50.242/theme/frontend/foxplus/style/default/image/pages/category_169.jpg"  alt=""  width="50"  height="50" />
)
[1] => Array
        (
            [item_id] => 169
            [item_title] => SF
            [item_time_stamp] => 1421063963
            [item_user_id] => 901
            [item_type_id] => pages
[profile_image] => <img src="http://54.174.50.242/theme/frontend/foxplus/style/default/image/pages/category_169.jpg"  alt=""  width="50"  height="50" />
        )
)

现在我只想获取 key 中图像的 URL [profile_image]。最终生成的数组应如下所示:

Array
(
[0] => Array
        (
            [item_id] => 170
            [item_title] => Master of Engineering
            [item_time_stamp] => 1421152531
            [item_user_id] => 244
            [item_type_id] => pages
            [item_photo] => 169
[profile_image] => http://54.174.50.242/theme/frontend/foxplus/style/default/image/pages/category_169.jpg
)
[1] => Array
        (
            [item_id] => 169
            [item_title] => SF
            [item_time_stamp] => 1421063963
            [item_user_id] => 901
            [item_type_id] => pages
[profile_image] => http://54.174.50.242/theme/frontend/foxplus/style/default/image/pages/category_169.jpg
        )
)

我应该怎么得到这个?请帮我。

4

4 回答 4

1

试试这个,它有点糟糕,但它可以正常工作

for ($i = 0; isset($data[$i]); $i++){
$temp = explode("\"", $data[$i]['profile_image']);
$data[$i]['profile_image'] = $temp[1];
}
于 2015-02-05T14:15:53.897 回答
1

尝试这样的事情:

//to test
$data = array(
    array(
        'profile_image'=>'<img src="http://54.174.50.242/theme/frontend/foxplus/style/default/image/pages/category_169.jpg"  alt=""  width="50"  height="50" />'
    ),
    array(
        'profile_image'=>'<img src="http://54.174.50.242/theme/frontend/foxplus/style/default/image/pages/category_169.jpg"  alt=""  width="50"  height="50" />'
    )
);

function fixdata($data){
    $fix = function($url){
        $patt = "/h\S*.jpg|.gif|.png/";
        preg_match($patt,$url,$match); 
        return (empty($match[0]))?$url:$match[0];
    };
    foreach($data as $key=>$value){
        $data[$key]['profile_image'] = $fix($value['profile_image']);
    }
    return $data;
}
var_dump($data);
$newdata = fixdata($data);
var_dump($newdata);
于 2015-02-05T14:35:20.700 回答
0

下面的代码将构建一个新数组,并允许您从嵌套数组之一中抓取任何值的内容。我所写的内容适用于您的 img 标签始终采用相同格式的情况,但是如果您遇到格式不同的情况,您可以修改代码正在抓取标签的区域以重建 url最小的努力。

$mainArray = array(

    array(
        'item_id'           => 170,
        'item_title'        => 'Master of Engineering',
        'item_time_stamp'   => 1421152531,
        'item_user_id'      => 244,
        'item_type_id'      => 'pages',
        'item_photo'        => 169,
        'profile_image'     => '<img src="http://54.174.50.242/theme/frontend/foxplus/style/default/image/pages/category_169.jpg"  alt=""  width="50"  height="50" />'
    ),
    array(
        'item_id'           => 169,
        'item_title'        => 'SF',
        'item_time_stamp'   => 1421063963,
        'item_user_id'      => 901,
        'item_type_id'      => 'pages',
        'item_photo'        => 169,
        'profile_image'     => '<img src="http://54.174.50.242/theme/frontend/foxplus/style/default/image/pages/category_169.jpg"  alt=""  width="50"  height="50" />'
    )

);


$newArray = array();
foreach($mainArray as $innerArray){


    $newInnerArray = array();
    foreach($innerArray as $key => $val){

        if($key !== 'profile_image'){

            $newInnerArray[$key] = $val; 

        } else {

            //Scrape the url here
            $link = strstr($val, '"');
            $link = strstr($link, ' alt', TRUE);
            $link = str_replace('"', '', $link);
            $link = str_replace(' ', '', $link);

            $newInnerArray[$key] = $link;

        }        

    }

    $newArray[] = $newInnerArray;

}

echo '<pre>';

echo var_export($newArray, TRUE);
于 2015-02-05T14:39:27.897 回答
0

使用 DOCDocument 类:

foreach ($data as &$item) {
    $doc = new DOMDocument();
    $doc->loadXML($item["profile_image"]);
    $item["profile_image"] = $doc->documentElement->getAttribute("src");
}

因此,您不依赖于 IMG 字符串的详细格式,例如您的 src 属性所在的位置。

于 2015-02-05T15:00:34.567 回答