简体   繁体   English

将对象插入数组

[英]Insert object into array

i have an array in php full of "Eventos Calendario" objects, at some point in my script i need to introduce a new object of the same type at position x of this array. 我在php中有一个充满“Eventos Calendario”对象的数组,在我的脚本中的某些时候我需要在这个数组的位置x引入一个相同类型的新对象。 this is the code i am using 这是我正在使用的代码

$EventoLimite = new EventosCalendario(null,$Timestamp, $TipoEvento);                
var_dump($EventoLimite);
array_splice($this->EventosEntrada, $i, 0, $EventoLimite); //
var_dump($this->EventosEntrada[$i]);

And the "Var_Dumps" i am getting are: 我得到的“Var_Dumps”是:

object(EventosCalendario)[15]
  public 'FechaHora' => int 1376334000
  public 'FechaHoraOriginal' => null
  public 'Tipo' => string 'Entrada' (length=7)
  public 'Origen' => string 'Insertado' (length=9)
  public 'Subtipo' => null
  public 'ID' => null

int 1376334000

Why is the new slot in the array only getting the value of "FechaHora" property? 为什么数组中的新插槽只获得“FechaHora”属性的值? i need to get the whole object in $this->EventosEntrada[$i]. 我需要在$ this-> EventosEntrada [$ i]中获取整个对象。 how can i do that?? 我怎样才能做到这一点??

“replacement”参数必须是一个数组本身,所以你应该写

array_splice($this->EventosEntrada, $i, 0, [$EventoLimite]); // note []s

maybe its because of that when you introduce a new object, just public variables and functions are available in the specific file you work on. 也许是因为当你引入一个新对象时,只需要在你工作的特定文件中使用公共变量和函数。 I mean it is cause by access of clsasses. 我的意思是它是由访问clsasses。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM