我有这段代码,它从我的节点类型'Student‘中检索一个节点。我尝试了不同的方法,将'$node'中的所有值保存到一个数组中。但是$node是一个对象。我的问题是如何将$node中的“所有”值存储到一个数组中。在Java an 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
}在java或c#中,您可以在for/foreach/while“循环”中这样说。
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.
}我不知道PHP是否也有这个功能。"yourObject.Add(otherObject)“
发布于 2011-03-30 20:31:48
下面的代码让你的对象像一个数组一样行为(访问方式):
$result = new ArrayObject( $node );如果你真的想要一个数组,只需在之后转换它:
$result = (array) $result;真见鬼,仔细想想,你甚至可以这么做:
$result = (array) $node;:-)
我刚刚发现,两种转换为数组的方法实际上也会公开受保护的/私有的属性。:-S可怕。
编辑:
// 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
}https://stackoverflow.com/questions/5486157
复制相似问题