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.

7
  • Possible duplicate of How does PHP 'foreach' actually work? Commented Feb 15, 2019 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? Commented Feb 15, 2019 at 12:36
  • dunno if this is the logic your looking for: 3v4l.org/2sRDQ Commented Feb 15, 2019 at 12:37
  • I want to echo only bmw and audi Commented Feb 15, 2019 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 Commented Feb 15, 2019 at 12:38

6 Answers 6

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;
}
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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";

1 Comment

OP request using for-loop (I do agree this is better solution...)
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++;
}

Comments

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

Comments

-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;
    }
}

3 Comments

Why exit instead of a break?
Are you sure about this? What if the keys changed?
This is only the idea you can change your code as per your need

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.