$ids = "(" . implode(",", $object->ids) . ")";
convert std object class to comma seperated string
문제
I have an std class object from twitter and i would like to take the ids array values and put them in a php variable $ids where $ids = (15761916,30144785,382747195,19399719)
.
I imagine using a for loop and using phps implode but i'm not sure how to go about it.
stdClass Object
(
[ids] => Array
(
[0] => 15761916
[1] => 30144785
[2] => 382747195
[3] => 19399719
)
[next_cursor] => 0
[next_cursor_str] => 0
[previous_cursor] => 0
[previous_cursor_str] => 0
)
해결책
다른 팁
Easiest way, I don't know if this will work (sorry!) as I don't tend to use a lot of stdClass Objects like this... would be to use an array map...
$ids = function (stdClass Object $object) {
return implode (",", $object->ids);
};
Maybe something like that, not entirely sure if this is the correct syntax... on my phone at the moment. Give that a shot!
I made it a lambda function by habbit you could just as easily do:
$ids = implode (",", $object->ids);
use this function to convert into array
$vars = get_object_vars ( $Obj );
print_r ( $vars );
$ids = implode (",", $vars[ids]);
As an extended answer u have 2 options :)
1st is implementing the Serializable Interface by doing so u can use the serialize function to get the desired output:
public function serialize() {
return array_implode(',', $this->ids);
}
OR
public function __toString() {
return array_implode(',', $this->ids);
}
and the usage becomes :
Serializable:
serialize($object_instance);
__toString:
print $object_instance;
both will give u the desired output, so u dont need to add the extra overhead to use a function or use array_implode()
in inline scripts.