Compare array value on array key

Discussion in 'PHP' started by rajaroy, Feb 16, 2011.

  1. #1
    Hello coders,

    I currently am trying some new stuff with loops and array's.
    I have an array wich contains some details about a product.
    The value I want to use is color. This is een id for the color in the second array.

    Sample: $list_data_array
    Array(
    [0] => Array
    (
    [klant_id] => 1
    [product_id] => 564
    [color] => 300
    [quantity] => 6
    )
    [1] => Array
    (
    [klant_id] => 2
    [product_id] => 565
    [color] => 201
    [quantity] => 2
    )
    [2] => Array
    (
    [klant_id] => 3
    [product_id] => 566
    [color] => 023
    [quantity] => 4
    )
    [3] => Array
    (
    [klant_id] => 4
    [product_id] => 567
    [color] => 069
    [quantity] => 10
    )
    )

    The second array contains the color codes and the color names.
    The color code's are the key.

    Sample:
    $list_color_array
    Array(
    [300] => Array
    (
    [id] => 32
    [number] => 300
    [name] => green
    )
    [636] => Array
    (
    [id] => 32
    [number] => 636
    [name] => blue
    )
    [201] => Array
    (
    [id] => 32
    [number] => 201
    [name] => red
    )
    [023] => Array
    (
    [id] => 32
    [number] => 023
    [name] => yellow
    )
    [012] => Array
    (
    [id] => 32
    [number] => 012
    [name] => purple
    )
    [069] => Array
    (
    [id] => 32
    [number] => 069
    [name] => cyan
    )
    )

    What i want to do is check what color code's are in $list_data_array en fetch all color names from $list_color_array and echo the names.

    I know how to loop with forech and echo the value, but i dont know how to compare one's value to a array key en echo that value name.

    If you know how to solve this i really like to know how, cause i am busy with this for about an half day already.


    Thanx in advance,

    Roy
     
    rajaroy, Feb 16, 2011 IP
  2. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #2
    Not too sure exactly what you mean. Is something like this on the right path? It goes through the first array, and attempts to match the color with the second array.

    foreach( $list_data_array as $arr ) {
        list( $klant, $product_id, $color, $quantity ) = $arr;
        if( array_key_exists( $color, $list_color_array ) ) { // We've found a match, let's output the details
            list( $id, $number, $name ) = $list_color_array[ $color ];
            echo "We've found the colour id, number and name.";
        }
    }
    PHP:
     
    Alex Roxon, Feb 16, 2011 IP
  3. rajaroy

    rajaroy Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes Alex Roxon that was exactly what i was looking for.
    Get the color code value's of $list_data_array and check if they exist in the key of $list_color_array.

    I changed it al little bit and this is how it works for me now:
    
    foreach($list_data_array as $key=>$value) {
    		if(array_key_exists($value[kleur], $list_color_array)) { // We've found a match, let's output the details
            echo "We've found the colour ".$list_color_array[$value[kleur]][name];
    		}
    	}
    
    PHP:
    Unfortunatly i couldn't get it working with the list($var1, $var2) = $array; operation, there was'nt any value in it. Too bad :(

    Thank you for helping me out Alex Roxon. ;)
     
    rajaroy, Feb 17, 2011 IP