从对象中选择随机项

时间:2013-03-31 19:13:52

标签: php zend-framework

我有一个像这样的ini文件:

[hints]
menu.0 = "Hint: Dropping a element on parent element will move the selected element at the end of list!"
menu.1 = "Hint: Use Menu elements to beautify your navigator!"
menu.2 = "Hint: Pages in red means they are draft pages. Adding them to your menu means that your visitors can not see the page!"

使用Zend_Config_Ini读取ini文件:

self::$hints = new Zend_Config_Ini(APPLICATION_PATH . '/configs/hints.ini',
                                      'hints');

var_dump(self::$hints->menu);上的输出是:

object(Zend_Config)[63]
  protected '_allowModifications' => boolean false
  protected '_index' => int 0
  protected '_count' => int 3
  protected '_data' => 
    array (size=3)
      0 => string 'Hint: Dropping a element on parent element will move the selected element at the end of list!' (length=93)
      1 => string 'Hint: Use Menu elements to beautify your navigator!' (length=51)
      2 => string 'Hint: Pages in red means they are draft pages. Adding them to your menu means that your visitors can not see the page!' (length=118)
  protected '_skipNextIteration' => null
  protected '_loadedSection' => null
  protected '_extends' => 
    array (size=0)
      empty
  protected '_loadFileErrorStr' => null

我需要帮助从该对象中随机挑选一个项目并显示它。

1 个答案:

答案 0 :(得分:3)

这个PHP函数对你有用:

array_rand();

参考:array_rand() in PHP.net

因为它是一个对象,你以前可以这样做:

$arr = (array)$object;

https://stackoverflow.com/a/1897695/2228023

中所述