tags:

views:

70

answers:

4

Hi there,

I was wondering if this code snippet is considered legal:

$arr = array(123,456,789,123,456,789);
foreach($arr as $a) {
    $arr = $a;
    break;
}
//prints 123
echo $arr;

It executes, but are there any pitfalls i should know using this method?


Update: Here is the actual problem
You have an array as follows from the database query (select * from table where code = $code)

Array
(
    [0] => Array
        (
            [id] => 1
            [code] => 1234567
            [member_id] => 7
        )

    [1] => Array
        (
            [id] => 5
            [code] => 1234567
            [member_id] => 
        )

    [2] => Array
        (
            [id] => 67
            [code] => 1234567
            [member_id] => 43
        )

)

all you care about is finding the first (if any) row that has an empty member_id (this means the code has not been claimed).

So how do you go about doing this?

According to Felix Kling using the variable to hold the array of codes and then overwriting it with the row that you want is not the best solution, so what do you propose.

Also, bonus credit: How many different 7 digit codes can you generate using 32 characters (duplicate characters allowed)?

is it 32^32*7 or ((((((32^32)^32)^32)^32)^32)^32)^32)?

A: 

This might work, but it is definitely bad coding style and makes your code harder to understand. Because if you use more meaningful variable names, it makes no sense anymore:

$scores = array(123,456,789,123,456,789);
foreach($scores as $score) {
    $scores = $score;
    break;
}
//prints 123
echo $scores;

In this case, $scores suggests to hold something like a list, not a single element (or imagine you use $score_list... even worse). And it won't improve if you start giving your variables names that fit for different types of values ;)


Remember: Not everything that is possible should be done. I would definitely avoid this.

Felix Kling
@Hailwood: It does not change anything. It is bad practice. If you reuse the variable it makes your code harder to understand and more difficult to maintain in the long run. Fact is that code is much more often read than written. Create *readable* code. Performancewise, having an additional variable makes no difference.
Felix Kling
+1  A: 

So,

using this method works fine as long as you are only using a copy of the array.

If you were to pass in the array by reference foreach($code_info as &$code) then you will receive a Invalid argument supplied for foreach() error.

Hailwood
+1  A: 

As others explained, there is no reason to reuse the variable. Is there a reason for it? Why not create a new one?

$resultId = 0;

foreach ($result as $row) {
   if ($row["member_id"] == "") {
      $resultId = $row["id"];
      break;
   }
}

But here is the big question - why don't you simply do that in SQL? You're already creating an SQL query. What's wrong with select * from table where code = $code and member_id = null?

Btw, to answer your other question: Number of permutations with seven characters, each one with 32 different possibilities = 32^7.

EboMike
Because, If there are no free codes, i need to do some more checking to see if the person claiming the code has already claimed it (for detail editing purposes).
Hailwood
A: 
$empty_number=-1;
foreach($arr as $a=>$b)
{
  if($b['member_id']!='')
  {
    $empty_number=$a;
    break;
  }
}

The index for the empty number is stored in the $empty_number variable.. if there is no row with an empty member_id tag, the value of $empty_number is -1.

EDIT:

For your bonus question, the maximum would be 32^7 = 34 359 738 368

Kranu