Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have the following code:

foreach ($row as $item) {
  if (!in_array($item['login_id'], $tmp)) {
    $tmp[] = $item['brand'];
    $tmp[] = $item['login_id'];
    $tmp[] = $item['name'];
  }
}

This provides the following output:

array(408) { 
 [0]=> string(4) "ABC" 
 [1]=> string(8) "r4ft6tg7" 
 [2]=> string(8) "Aberdeen" 
 [3]=> string(4) "ABC" 
 [4]=> string(8) "1ws3edft" 
 [5]=> string(18) "Birmingham Airport" 
 [6]=> string(4) "DDD" 
 [7]=> string(8) "bgt6yhnj" 
 [8]=> string(27) "Birmingham City"...}

I am trying to then loop through this array and add them to a dropdown using the following:

$a = 0;
$b = 1;
$c = 2;

foreach ($tmp as $value) {
    echo "<option name='".$value[$a]."' 
          value='".$value[$b]."'>
          ".$value[$c]."
          </option>";
$a=$a+3;
$b=$b+3;
$c=$c+3;
}

However the output is most odd:

<option name='I' value='b'>i</option>

The output I expected and need is:

<option name='ABC' value='r4ft6tg7'>Aberdeen</option>

Any suggestions, feedback on where I am going wrong would be appreciated.

share|improve this question
2  
$value is a string, so accessing $value[$x] will yield whatever character is at that index in the string...what are you trying to accomplish exactly? What are $a, $b and $c for? –  Clive Jul 6 at 11:53
    
Ah, I am trying to place the vales of what I thought we array elemens into the option output. So for example, <option name='ABC' value='r4ft6tg7'>Aberdeen</option> –  Homer_J Jul 6 at 11:57
    
@Clive - thanks for that, much appreciated. I didn't realise it was a string. –  Homer_J Jul 6 at 12:13

1 Answer 1

up vote 1 down vote accepted

I believe this is what you meant:

foreach ($row as $item) {
  if (! array_key_exists($item['login_id'], $tmp)) {
    $tmp[$item['login_id']] = array($item['brand'], $item['login_id'], $item['name']);
  }
}

EDIT: Fixed index of $tmp above (and how to check for index).

Then your following code could work the same, omitting the increments of $a, $b, $c (and hence omitting those three variables altogether):

foreach ($tmp as $value) {
    echo "<option name='".$value[0]."' 
          value='".$value[1]."'>
          ".$value[2]."
          </option>";
}

You were mistakenly treating $tmp as both a one- and two-dimensional array. Actually setting it up to be a two-dimensional array resolves that. As pointed out in the comments, in your original code, $value was a string, and accessing an index of a string like you would an array yields the given character in the string.

Also, for clarity, you might consider making each subarray in $tmp an asssociative array.
E.g. $tmp[$item['login_id']] = array('brand' => $item['brand'], ... and then accessing it accordingly in your latter foreach loop.

share|improve this answer
1  
Should $tmp[$item['login_id']] = array($item['brand'], $item['login_id'], $item['name'];)' actually be $tmp[$item['login_id']] = array($item['brand'], $item['login_id'], $item['name']);` - semi-colonin the wrong place? –  Homer_J Jul 6 at 12:04
    
Yes, absolutely, my bad. Thanks. –  faintsignal Jul 6 at 12:06
    
No probs - thanks for the help. Although I'm still not getting the output I expect. –  Homer_J Jul 6 at 12:06
    
I am now :-) perfect, thanks - that is exactly what I was after! –  Homer_J Jul 6 at 12:12
1  
in_array worked for your 1-dimensional array approach (so long as you had no other data that could have the same value as one of your login IDs). But since the login ID seems to be the value uniquely identifying each row, it makes sense to index the array by that value. Hence now it's the key and so check for the key instead of an array element. –  faintsignal Jul 6 at 12:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.