Help, why is that i can't do something with the last element of my array

Discussion in 'PHP' started by tiffany.tomol, Mar 9, 2011.

  1. #1
    $new_values is an array, if you print that it will display this:

    Array ( [0] => cat [1] => company_name [2] => email_address [3] => address [4] => city [5] => state [6] => postal_code [7] => phone_number [8] => fax_number [9] => sic_code [10] => sic_description )

    Now i have a for loop.

    for($x=0; $x < count($new_values); $x++)
    {
    if($new_values[$x] == "sic_description")
    echo 'test';
    }

    The problem here is the if statement will never be true. But as you can see sic_description is the last element of my array.

    Please help me whats wrong with this. I appreciate your help.

    Thanks!
     
    tiffany.tomol, Mar 9, 2011 IP
  2. artus.systems

    artus.systems Well-Known Member

    Messages:
    87
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    103
    #2
    What are you trying to do exactly ?
     
    artus.systems, Mar 9, 2011 IP
  3. kajfat

    kajfat Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    looooool

    Use "foreach" to loop through arrays, and everything will be OK!

    e.g.:

    foreach($new_values as $item){
    if($item == "text")
    echo "Yahoo!!";
    }
     
    kajfat, Mar 10, 2011 IP
  4. tiffany.tomol

    tiffany.tomol Greenhorn

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #4
    @kajfat: FYI, i did that already but it still doesn't work.

    @artus.systems:
    Actually i'm trying to compare 2 arrays here. Say, i have array1 and array2. I want to know if all elements of array1 is in array2. If there is one element in array1 that is not present in array2 the comparison should stop. It will return a flag whether all elements in array1 are present in array2 or not.

    i tried using in_array but i don't know why the last element of array1 can't be compared.

    $array1 = array('COMPANY_NAME','EMAIL_ADDRESS','ADDRESS','CITY','STATE','WEBSITE');
    $array2 = array('COMPANY_NAME','EMAIL_ADDRESS','ADDRESS','CITY','STATE','PHONE_NO','FAX_NO','WEBSITE');

    Having this, flag will always return 1.
     
    tiffany.tomol, Mar 10, 2011 IP
  5. artus.systems

    artus.systems Well-Known Member

    Messages:
    87
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    103
    #5
    Hope this helps...
    <?php 
    $array1 = array('COMPANY_NAME','EMAIL_ADDRESS','ADDRESS','CITY','STATE','WEBSITE');
    $array2 = array('COMPANY_NAME','EMAIL_ADDRESS','ADDRESS','CITY','STATE','PHONE_NO','FAX_NO','WEBSITE');
    
    $result = array_diff($array1, $array2);
    
    //print_r($result);
    $count=count($result);
    
    if($count>0) 
    {
    	$flag=1;
    } else 
    {
    	$flag=0;
    }
    
    ?>
    ?>
    Code (markup):
     
    artus.systems, Mar 10, 2011 IP
  6. tiffany.tomol

    tiffany.tomol Greenhorn

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #6
    I figured out why the last element of the array will always not equal its because of the array terminator. What i did if the loop reached the last element of the array to be compared i removed the terminator by substr('string', 0, -2).

    Now when i do array_diff(array1, array2) it will return correctly.

    @artus.systems, thanks for your reply. I do appreciate it. :)
     
    tiffany.tomol, Mar 15, 2011 IP