2013-01-23 29 views
2

如果我知道搜索這些詞的正確術語很容易google,但我不知道術語。訪問對象中的數組

我有一個API返回一個大對象。有一種特定的一個我通過訪問:

$bug->fields->customfield_10205[0]->name; 
//result is [email protected] 

有許多值的,我可以通過它改變從0到1等

訪問他們,但我通過陣列要循環(也許這就是不正確的說法),並得到所有的郵件在那裏,並把它添加到像這樣的字符串:

implode(',', $array); 
//This is private code so not worried too much about escaping 

本來以爲我只是這樣做: 回聲破滅(「」,$ bug->字段 - > customfield_10205->名);

也試過 echo implode(',',$ bug-> fields-> customfield_10205);

and echo implode(',',$ bug-> fields-> customfield_10205 [] - > name);

輸出即時尋找的是: '爲johndoe @ gmail.com,marydoe @ gmail.com,patdoe @ gmail.com'

我要去哪裏錯了,我提前爲愚蠢的問題道歉,這可能是新手

+0

這是一個SimpleXML對象?是否有可能'var_dump()''$ bug-> fields-> customfield_10205'的值? –

+0

你會得到什麼輸出?如果這是SimpleXML,那麼這些不是*真正的數組,所以你可以用'[]'和'foreach'訪問它們,但不能使用數組函數。 –

回答

2

你需要一個迭代,如

# an array to store all the name attribute 
$names = array(); 

foreach ($bug->fields->customfield_10205 as $idx=>$obj) 
{ 
    $names[] = $obj->name; 
} 

# then format it to whatever format your like 
$str_names = implode(',', $names); 

PS:你應該尋找屬性電子郵件而不是名稱,但是,我只是按照您的代碼

0

使用此代碼,並循環訪問數組。

$arr = array(); 
for($i = 0; $i < count($bug->fields->customfield_10205); $i++) 
{ 
    $arr[] = $bug->fields->customfield_10205[$i]->name; 
} 
$arr = implode(','$arr); 
0

這是不可能的PHP,而無需使用額外的循環和一個臨時列表:

$names = array(); 
foreach($bug->fields->customfield_10205 as $v) 
{ 
    $names[] = $v->name; 
} 
implode(',', $names); 
0

您可以使用array_map函數這

function map($item) 
{ 
    return $item->fields->customfield_10205[0]->name; 
} 

implode(',', array_map("map", $bugs)); // the $bugs is the original array