Foreach Not Displaying Duplicates

Discussion in 'PHP' started by lektrikpuke, Feb 4, 2013.

  1. #1
    Hi guys,

    $testArr = array('1' => 'b', '2' => 'y', '3' => 'o', '1' => 'b');
    foreach ($testArr as $key => $val){
        echo $key . ' ' . $val . "<br />\n";
    }
    Code (markup):
    I want the above to display byob, but it displays byo. How can I get foreach (or some other function) to echo EACH key and val?
     
    Solved! View solution.
    lektrikpuke, Feb 4, 2013 IP
  2. #2
    PHP overwrites things that are the same. Since your first entry's key is the same as the last entry's key, it overwrites the value of the first key, essentially removing the last key as a duplicate. In order to prevent this your best bet would to simply give the last key a unique value.

    You cannot solve this problem other than to make the keys unique, because you cannot use what is not there. Execute this code and you will see the following output.
    <?php
    $testArr = array(
        '1' => 'b',
        '2' => 'y',
        '3' => 'o',
        '1' => 'b'
    );
    print_r($testArr);
    PHP:
    Array
    (
    [1] => b
    [2] => y
    [3] => o
    )
    Code (markup):
    Either let PHP handle the keys for you automatically:
    $testArr = array('b','y','o','b');
    print_r($testArr);
    PHP:
    Or make all the keys unique:
    $testArr = array('1' => 'b', '2' => 'y', '3' => 'o', '4' => 'b');
    print_r($testArr);
    PHP:
     
    Last edited: Feb 4, 2013
    Blaxus, Feb 4, 2013 IP
  3. lektrikpuke

    lektrikpuke Well-Known Member

    Messages:
    297
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    113
    #3
    I am disappointed that PHP parses the array and then overwrites duplicates. I assumed it went through the array one key/val pair at a time and did what it was instructed to do. I guess no language/script is perfect, not even PHP.
     
    lektrikpuke, Feb 4, 2013 IP
  4. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #4

    This isn't an imperfection of PHP at all, but rather your coding methodology. In each element of an array, there needs to exist a unique key. This is the same in every other popular language out there. You can't assign two different values to the same array element (unless you start using two dimensional arrays and the like), without overriding the other.

    I honestly don't know what you expected to happen.
     
    Alex Roxon, Feb 4, 2013 IP
  5. lektrikpuke

    lektrikpuke Well-Known Member

    Messages:
    297
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    113
    #5
    Thanks for the answer. I knew what I was seeing, but I didn't want to accept it.
     
    lektrikpuke, Feb 5, 2013 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #6
    I don't see how you could think it works the way it does -- it's the entire POINT of keys being that you could look up:

    echo $testArr[1];

    Which one would you expect that to output?!? That's how it works in EVERY OTHER programming language -- keys are unique to each array element/data -- that's why it's CALLED a key.

    For what you are trying to do, have multiple values the same, you should be using an array OF arrays:
    
    $testArr = array(
    	array('1','b'),
    	array('2','y'),
    	array('3','o'),
    	array('1','b')
    );
    
    foreach ($testArr as $data) {
    	echo $data[0],' ',$data[1],'<br />';
    }
    
    Code (markup):
    Though naturally accessing them via indexing the first column won't work.
     
    deathshadow, Feb 5, 2013 IP