示例#1
0
 public function test_reference_2()
 {
     // Create Array
     $arr = new PrettyArray();
     $arr2 = array();
     $iterations = 10000;
     for ($i = 0; $i < $iterations; $i++) {
         $value = mt_rand(1000, 2000);
         $key = mt_rand(0, 1000);
         $arr2[$key] = $value;
         $arr->setByReference($key, $arr2[$key]);
     }
     // Update arr2
     $iterations = 10000;
     for ($i = 0; $i < $iterations; $i++) {
         $arr2[array_rand($arr2)] = mt_rand(2000, 3000);
     }
     // Check accuracy
     $this->assertEquals(count($arr2), $arr->count());
     $this->assertEquals($arr2, $arr->to_a());
 }
示例#2
0
 /**
  * Methods: getSet, getSet_
  *
  * Will get a 'set' from PrettyArray. Calling it destructively will force the return value to be references to the current PrettyArray.
  *
  * <code>
  * $arr = new PrettyArray(array(1,2,3,4,5));
  * $o = $arr->getSet(1, 2)->to_a();
  * print_r($o);
  * </code>
  * <pre>
  * Array
  * (
  *     [1] => 2
  *     [2] => 3
  * )
  * </pre>
  *
  * @link http://ruby-doc.org/core-1.9.3/Array.html#method-i-slice
  * @param mixed $start
  * @param int $length
  * @return PrettyArray
  */
 public function getSet_($start, $length)
 {
     $ret = new PrettyArray();
     $count = 0;
     foreach ($this->data as $key => &$value) {
         if ($start == $key || $count > 0) {
             $ret->setByReference($key, $value);
             $count++;
             if ($count == $length) {
                 break;
             }
         }
     }
     return $ret;
 }