문제

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
) 
도움이 되었습니까?

해결책

$ids = "(" . implode(",", $object->ids) . ")";

다른 팁

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 :

  1. Serializable:

    serialize($object_instance);

  2. __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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top