Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从数组中删除stdClass对象_Php - Fatal编程技术网

Php 从数组中删除stdClass对象

Php 从数组中删除stdClass对象,php,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"

我有一个如下所示的数组(数组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"
  }
}
这就是我所做的(铸造)

你可以试试

$array = array_map(function ($v) {
    return (array) $v ; // convert to array 
}, $array);
或者,如果此数据来自
json
请使用

$array = json_decode($data,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)

为什么要删除它?如果不循环,就无法操作数组。任何解决方案都需要某种形式的隐式循环。@Bananam00n-我需要使用JSON从中创建JSON对象_encode@MDeSilva您仍然可以从中创建JSON对象,无需将对象转换为数组。@Diego是对的,这正是我问的原因
$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)