2011-02-19 89 views
0

我正在使用SimpleXML从公共提要@ Flickr中提取图像。我希望把所有拉到图像到一个数组中,我已经做了:通过foreach创建数组

$images = array(); 
foreach($channel->item as $item){ 
    $url = $path->to->url; 
    $images[] = $url; 
} 

从这个话,我可以附和了所有的图像使用:

foreach($images as $image){ 
    //output image 
} 

然后我决定我想有图像标题,以及用户,所以我认为我会用:

$images = array(); 
foreach($channel->item as $item){ 
    $url = $path->to->url; 
    $title = $path->to->title; 
    $images[$title] = $url; 
} 

我想用我$image['name of title']可输出为标题的URL,这将意味着,但它提供了一个非法的偏移误差,当我る这个..只会有标题和网址,但不是用户。

谷歌上搜索了一下我念你不能在数组键​​使用_后,但我尝试使用:

$normal = 'dddd'; 
$illegal = ' de___eee'; 
$li[$normal] = 'Normal'; 
$li[$illegal] = 'Illegal'; 

而这种输出权,排除_是在数组中的键非法(..我想) 。

所以现在我真的很困惑为什么它不会运行,当我在玩时使用print_r()我注意到数组中的一些SimpleXML对象,这就是为什么我认为这是给出错误。

理想的输出将在格式的数组:

$image = array(0 => array('title'=>'title of image', 
          'user'=>'name of user', 
          'url' =>'url of image'), 
       1 => array(....) 
     ); 

但我真的难倒我怎么会从foreach环路构成本,链接引用以及其他问题(couldn” t找到任何),欢迎。

回答

3
$images = array(); 
foreach ($channel->item as $item){ 
    $images[] = array(
     'title' => $item->path->to->title, 
     'url' => $item->path->to->url 
    ); 
} 

foreach ($images as $image) { 
    echo "Title: $image[title], URL: $image[url]\n"; 
} 
+0

谢谢你,我觉得很愚蠢。 – Daniel 2011-02-19 04:22:14

0

下划线不禁止数组键,您可能会遇到的唯一问题符号,即标题一些时间重叠,你可能数组中失去一些项目。最正确的方法是deceze example

如果你喜欢关联数组,你可以使用图像路径作为数组键和标题作为值。