2

If i knew the correct terms to search these would be easy to google this but im not sure on the terminology.

I have an API that returns a big object. There is one particular one i access via:

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

There is numerous values and i can access them by changing it from 0 to 1 and so on

But i want to loop through the array (maybe thats not the correct term) and get all the emails in there and add it to a string like this:

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

Would have thought i just do something like: echo implode(',', $bug->fields->customfield_10205->name);

Also tried echo implode(',', $bug->fields->customfield_10205);

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

The output im looking for is: '[email protected],[email protected],[email protected]'

Where am i going wrong and i apologize in advance for the silly question, this is probably so newbie

2
  • Is this a SimpleXML object? Is it possible to perhaps var_dump() the value of $bug->fields->customfield_10205? Commented Jan 23, 2013 at 12:41
  • And what output do you get instead? If this is SimpleXML, these are not really arrays, so you can access them with [] and foreach but not use array functions. Commented Jan 23, 2013 at 12:42

4 Answers 4

2

You need an iteration, such as

# 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: You should look for attribute email instead of name, however, I just follow your code

Sign up to request clarification or add additional context in comments.

Comments

0

use this code ,and loop through the array.

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

Comments

0

This is not possible in PHP without using an additional loop and a temporary list:

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

Comments

0

You can use array_map function like this

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.