D
D
daniilakk2015-12-23 20:40:07
PHP
daniilakk, 2015-12-23 20:40:07

How to replace the array_rand function and access the array one by one?

There is a code

///  пример массива
$GLOBALS['test'] = [
   
    
  [
     	'id' => 30009,
    'url' => 333222,
  ],
  [
     	'id' => 33351,
    'url' => 555666,
  ]
  ]


///вывожу  рандомно часть
   $zapros = $GLOBALS['test'][array_rand($GLOBALS['test'])];

How to replace the array_rand function, and make the enum look like this:
first time run = 1 part
second time run = 2 part
third time run = 1 part
and so on

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Cat Anton, 2015-12-23
@daniilakk

function my_array_rand($array) {
    static $i = 0;
    $keys = array_keys($array);
    return $keys[$i++ % count($array)];
}

for ($i = 0; $i < 3; $i++) {
  var_dump($GLOBALS['test'][my_array_rand($GLOBALS['test'])]);
}

ideone.com/eKfjoZ

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question