php - 从数组中删除 stdClass 对象

标签 php

我有一个如下所示的数组(数组 1),我需要从中删除 stdClass,就像下面的数组编号一样。 2. 目前我正在使用 foreach 循环来做这件事,有没有更好的方法来做到这一点而无需循环?

1号数组

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

2号数组

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

这就是我所做的(选角)

foreach($moderationContacts as $contact)
{
    $contacts[] = (array)$contact;
}

最佳答案

尝试

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

编辑: 我已经测试了这个案例,它有效:

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

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

var_dump($array);

输出

array
  'a' => 
    array
      0 => string 'b' (length=1)
      1 => string 'c' (length=1)
  'd' => 
    array
      'test' => string 'foo' (length=3)

关于php - 从数组中删除 stdClass 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16420384/

相关文章:

php - AWS EC2 vsftpd 将文件/目录所有者显示为数字 (48)

php - 使用 Laravel Nova 显示指标趋势的问题 - 参数太少

php - 不要在 Mysql 中添加完全相同的 3 列值

java - 将 Spring MVC 与内联 PHP 混合

php - 短信到数据库

javascript - 使用 test 和 location.href 在 URL 中查找字符串

php - .htaccess 和 mod_rewrite 到使用不同标准的平面 URL

php - 在 PHP 中有效地使用 foreach

php - 将值插入两个相关表中

php - cakephp 模型验证错误消息未显示在 hasOne 关联中