php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44737 array_rand doesn't return object
Submitted: 2008-04-15 23:34 UTC Modified: 2008-04-16 06:34 UTC
From: pcdevil at freemail dot hu Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.2.5 OS: ubuntu linux
Private report: No CVE-ID: None
 [2008-04-15 23:34 UTC] pcdevil at freemail dot hu
Description:
------------
if i use array_rand in an array with object values, it return only an integer (key?) value.

Reproduce code:
---------------
<?php
class SimpleClass {
	public $integer=0;
	
	function __construct($i) {
		$this->integer=$i;
	}
}

$array=array();
for ($i=0;$i<10;++$i)
	$array[]=new SimpleClass($i);

var_dump(array_rand($array));

?>

Expected result:
----------------
//like that:

object(SimpleClass)#125 (1) {
  ["integer"]=>
  int(3)
}


Actual result:
--------------
//like that:

int(2)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-04-15 23:54 UTC] scottmac@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

array_rand returns a key, so its picking a key randomly from $array.
 [2008-04-16 06:34 UTC] pcdevil at freemail dot hu
sorry, i missunderstood the manual.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Nov 03 23:00:01 2025 UTC