简体   繁体   English

将节点存入(对象)到数组中

[英]store a node into (object) into an array

I've this piece of code, it retrieves a node from my node type 'Student'. 我有这段代码,它从我的节点类型'Student'中检索一个节点。 I try different things to, save all values in '$node' into an array. 我尝试不同的东西,将'$node'所有值保存到数组中。 But $node is a object. 但$ node是一个对象。 My question is how doe I store 'all' values in $node into a array. 我的问题是如何将$node 'all'值存储到数组中。 In Java an C# it's simpler to do that. 在Java中,C#更容易做到这一点。

$results = db_query(db_rewrite_sql("SELECT nid FROM {node} WHERE type =
 'student'"));

while($nid = db_result($results)) {

  $node = node_load($nid);

  // Do something with $node

}

in java or c# you can say in a for/foreach/while 'loop' 在java或c#中你可以说for / foreach / while'loop'

String item[] = null;
for(int i = 0; i<=myNode; i++) {
   item.add(myNode[i]); // the item and value UgentID, name student, location student are been stored in item array.
}

I don't know if PHP has that too. 我不知道PHP是否也有。 "yourObject.Add(otherObject)" “yourObject.Add(otherObject)”

The following let's your object behave (access-wise) as an array: 以下是让您的对象以数组的形式行为(按访问方式):

$result = new ArrayObject( $node );

If you truly want an array, simply cast it afterwards: 如果你真的想要一个数组,那么只需将其转换为:

$result = (array) $result;

Heck, come to think of it, you could even simply do: 哎呀,想想看,你甚至可以这样做:

$result = (array) $node;

:-) :-)

Both methods of casting to array will actually expose protected/private properties as well, I just found out. 我发现,这两种转换为数组的方法实际上也会暴露受保护/私有属性。 :-S Horrible. : - 太可怕了。

edit: 编辑:

// initiate array
$nodes = array();
while($nid = db_result($results)) {

  // either do one of the following, to push
  $nodes[] = node_load($nid);

  // or:
  array_push( $nodes, node_load($nid) );

  // Do something with $node

}

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

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