5

I have a array like below (array 1) and I need to remove stdClass from it like in below array no. 2. Currently i'm doing it using a foreach loop, is there are better way to do that wthout looping?

Array no.1

array(3) {
  [0] => object(stdClass)#169 (4) {
    ["id"] => string(2) "59"
    ["name"] => string(13) "test"
    ["email"] => string(21) "[email protected]"
    ["telephone"] => string(20) "898998989"
  }
  [1] => object(stdClass)#190 (4) {
    ["id"] => string(2) "58"
    ["name"] => string(13) "test"
    ["email"] => string(21) "[email protected]"
    ["telephone"] => string(8) "71877858"
  }
  [2] => object(stdClass)#193 (4) {
    ["id"] => string(2) "34"
    ["name"] => string(9) "test"
    ["email"] => string(22) "[email protected]"
    ["telephone"] => string(13) "3189028092139"
  }
}

Array no.2

array(3) {
  [0] => array(4) {
    ["id"] => string(2) "62"
    ["name"] => string(5) "test"
    ["email"] => string(22) "[email protected]"
    ["telephone"] => string(10) "898998989"
  }
  [1] => array(4) {
    ["id"] => string(2) "59"
    ["name"] => string(13) "test"
    ["email"] => string(21) "[email protected]"
    ["telephone"] => string(20) "71877858"
  }
  [2] => array(4) {
    ["id"] => string(2) "58"
    ["name"] => string(13) "test"
    ["email"] => string(21) "[email protected]"
    ["telephone"] => string(8) "3189028092139"
  }
}

This is what I do (casting)

foreach($moderationContacts as $contact)
{
    $contacts[] = (array)$contact;
}
7
  • Why do you want to remove it? Commented May 7, 2013 at 13:34
  • You cannot manipulate arrays without looping. Any solution will need some form of implicit looping. Commented May 7, 2013 at 13:34
  • @Bananam00n - I need to create a JSON object from it using json_encode Commented May 7, 2013 at 13:34
  • @MDeSilva You can still create a JSON object from it, no need to convert the objects into arrays. Commented May 7, 2013 at 13:37
  • @Diego is right, exactly why I was asking Commented May 7, 2013 at 13:41

2 Answers 2

12

try

$array = json_decode( json_encode($array), true);

EDIT: I've tested this case, and it works:

$stdClass= new stdClass();
$stdClass->test = "foo";
$array = Array(
    "a" => Array("b","c"),
    "d" => $stdClass
);

$array = json_decode( json_encode($array), true);

var_dump($array);

OUTPUT

array
  'a' => 
    array
      0 => string 'b' (length=1)
      1 => string 'c' (length=1)
  'd' => 
    array
      'test' => string 'foo' (length=3)
Sign up to request clarification or add additional context in comments.

Comments

3

You can try

$array = array_map(function ($v) {
    return (array) $v ; // convert to array 
}, $array);

Or if this data is from json use

$array = json_decode($data,true);

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.