Solved

PHP Loop

Posted on 2013-11-18
2
243 Views
Last Modified: 2014-04-22
need code to do the following in php

Specify a max nbr to loop thru like 1 thru 1000
Specify a start value for index any numeric value
Each time index should increase by 1
2nd value should be a random 10 digit nbr

the output needs to look like this

$cb->set("index", $randomnumber);
0
Comment
Question by:AlexPonnath
  • 2
2 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39657771
<?php // RAY_temp_alexponnath.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28297482.html

/**
 * Specify a max nbr to loop thru like 1 thru 1000
 * Specify a start value for index any numeric value
 * Each time index should increase by 1
 * 2nd value should be a random 10 digit nbr
 * the output needs to look like this: $cb->set("index", $randomnumber);
 */

// THE CLASS DEFINITION FOR "$cb"
Class Cb
{
    public function set($index, $randomnumber)
    {
        $this->data[$index] = $randomnumber;
    }
}

// INSTANCE OF THE CB CLASS
$cb = new CB;

// RANGE OF TEN DIGIT NUMBERS
$lo = 1000000000;
$hi = 9999999999;

// NUMBER OF ELEMENTS TO CREATE
$lim = 32;

// STARTING INDEX
$ndx = 16;

// THE ARRAY KEYS
$arr = range($ndx, $ndx+$lim);
foreach ($arr as $key)
{
    $cb->set($key, rand($lo, $hi));
}

// SHOW THE WORK PRODUCT
echo '<pre>';
print_r($cb);

Open in new window

Best regards, ~Ray
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40016129
@AlexPonnath: Please see the EE grading guidelines:
http://support.experts-exchange.com/customer/portal/articles/481419

Out of your last ten questions, you've given only one grade of "A" -- a behavior that is astonishingly out of step with an online community where the default and expected grade is "A" unless the responses are deficient.

In the instant case you got a tested and working code example that does exactly what you asked for, and you got it within 15 minutes of posting the question.  So I think you owe an explanation for the marked down grade.  Specifically, what were you expecting that you did not get?

Standing by to hear back from you! ~Ray
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now