Solved

PHP Loop

Posted on 2013-11-18
2
255 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 109

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 109

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

813 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

10 Experts available now in Live!

Get 1:1 Help Now