0

I have an array like this:

$aMyArray = array(
            "bmw"=>"user1",
            "audi"=>"user2",
            "mercedes"=>"user3"
);

And I only want to show the first two elements bmw=>user1 and audi=>user2. But I want it by using a foreach loop.

brombeer
  • 8,716
  • 5
  • 21
  • 27
cbk38
  • 23
  • 1
  • 4
  • Possible duplicate of [How does PHP 'foreach' actually work?](https://stackoverflow.com/questions/10057671/how-does-php-foreach-actually-work) – Tiago Ornelas Feb 15 '19 at 12:34
  • 3
    What exactly is the logic that you are looking for? Should the loop stop when encountering the key `mercedes`? Do you only want the values where the keys are `bmw` and `audi`? Or do you only want the first 2 items from the array? Can you share any attempts? – The fourth bird Feb 15 '19 at 12:36
  • dunno if this is the logic your looking for: https://3v4l.org/2sRDQ – Xatenev Feb 15 '19 at 12:37
  • I want to echo only bmw and audi – cbk38 Feb 15 '19 at 12:37
  • @cbk38 yes but you can do it by e.g. checking the key for the value "mercedes" or by checking the key for one of the values ("bmw", "audi") or by only printing the first two in any case, or by printing all except the last, or by removing the last entry from the array before iterating. All those will give you the same result by applying different logic – Xatenev Feb 15 '19 at 12:38
  • keep the index as 1 in the starting of for loop and increment this index in loop.once it go to the value 2 then break the loop. – Madhur Sharma Feb 15 '19 at 12:52
  • You dont need a foreach for that. echo array['bmw']; – JoaoGRRR Feb 15 '19 at 13:17

6 Answers6

3

If you want the first 2 by name:

Using in_array (documentation) is what you looking for:

$aMyArray = array("bmw"=>"user1", "audi"=>"user2", "mercedes"=>"user3");
$valuesToPrint = array("bmw", "audi");
foreach($aMyArray as $key => $val) {
    if (in_array($key, $valuesToPrint))
        echo "Found: $key => $val" . PHP_EOL;
}

If you want the first 2 by index use:

init index at 0 and increment in each iteration as:

$aMyArray = array("bmw"=>"user1", "audi"=>"user2", "mercedes"=>"user3");
$i = 0;
foreach($aMyArray as $key => $val) {
    echo "Found: $key => $val" . PHP_EOL;
    if (++$i > 1)
        break;
}
dWinder
  • 11,597
  • 3
  • 24
  • 39
1
$counter = 1;
$max = 2;
foreach ($aMyArray as $key => $value) {
    echo $key, "=>", $value;
    $counter++;
    if ($counter === $max) {
        break;
    }
}

It is important to break execution to avoid arrays of any size looping until the end for no reason.

NabLa
  • 108
  • 6
0

Easiest way:

$aMyArray=array("bmw"=>"user1","audi"=>"user2","mercedes"=>"user3");
$i=0;
foreach ($aMyArray as $key => $value) {
 if($i<2)
 {
    echo $key . 'and' . $value;
 }
 $i++;
}
GregH
  • 5,125
  • 8
  • 55
  • 109
0
<?php
$aMyArray = array(
            "bmw"=>"user1",
            "audi"=>"user2",
            "mercedes"=>"user3"
);

reset($aMyArray);
echo key($aMyArray).' = '.current($aMyArray)."\n";
next($aMyArray);
echo key($aMyArray).' = '.current($aMyArray)."\n";
m ba
  • 134
  • 4
0

I know you're asking how to do it in a foreach, but another option is using array travelling functions current and next.

$aMyArray = array(
            "bmw"=>"user1",
            "audi"=>"user2",
            "mercedes"=>"user3"
);
$keys = array_keys($aMyArray);
//current($array) will return the value of the current record in the array. At this point that will be the first record
$first = sprintf('%s - %s', current($keys), current($aMyArray)); //bmw - user1
//move the pointer to the next record in both $keys and $aMyArray
next($aMyArray);
next($keys);
//current($array) will now return the contents of the second element.
$second = sprintf('%s - %s', current($keys), current($aMyArray)); //audi - user2
Richard Parnaby-King
  • 14,703
  • 11
  • 69
  • 129
-1

You are looking for something like this

$aMyArray = array(
            "bmw"=>"user1",
            "audi"=>"user2",
            "mercedes"=>"user3"
);
foreach($aMyArray as $k=>$v){
    echo $v;
    if($k=='audi'){
        break;
    }
}