开发者

store a node into (object) into an array

开发者 https://www.devze.com 2023-02-20 14:40 出处:网络
I\'ve this piece of code, it retrieves a node from my node type \'Student\'. I try different things to, save all values in \'$node\' into an array. But $node is a object.

I've this piece of code, it retrieves a node from my node type 'Student'. I try different things to, save all values in '$node' into an array. But $node is a object. My question is how doe I store 'all' values in $node into a array. In Java an C# it's simpler to do that.

$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# yo开发者_JAVA百科u can say in a 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. "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

}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号