array question

Discussion in 'PHP' started by promotingspace.net, Jul 11, 2009.

  1. #1
    Hi
    I have an array like this:
    <?php
    $options[16][1]= array(" city","13");
    $options[16][2]= array(" another city","15");
    $options[16][3]= array(" another city","16");
    $options[16][4]= array(" another city","17");
    $options[16][5]= array(" another city","14");
    $options[16][6]= array(" another city","18");
    $options[16][7]= array(" another city ","19");
    $options[16][8]= array(" another city ","20");
    
    
    $options[17][1]= array(" another city","2");
    $options[17][2]= array("  another city","3");
    $options[17][3]= array(" another city","4");
    $options[17][4]= array(" another city","1");
    ?>
    PHP:

    the format:
    $options[state_id][a_number]= array(" city name","city_id");

    such a file will be included in my script
    assume that I have state_id and city_id; how can I find city name?
    Thank you for your help and ideas
     
    promotingspace.net, Jul 11, 2009 IP
  2. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #2
    In this format

    <? echo $options[17][4][0]; ?>

    The state and city id and then the [0]

    Regards
     
    Bohra, Jul 11, 2009 IP
  3. promotingspace.net

    promotingspace.net Peon

    Messages:
    361
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for your reply
    but that is when the array format is:
    $options[state_id][city_id]= array(" city name","a_number");
    not now that is:
    $options[state_id][a_number]= array(" city name","city_id");
    and my known information is state_id and city_id and I need city name
    any suggestions?
     
    promotingspace.net, Jul 11, 2009 IP
  4. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #4
    have a look the following code.

    $options = array();
    $options[16][1]= array(" city","13");
    $options[16][2]= array(" another city","15");
    $options[16][3]= array(" another city","16");
    $options[16][4]= array(" another city","17");
    $options[16][5]= array(" another city","14");
    $options[16][6]= array(" another city","18");
    $options[16][7]= array(" Wellington city ","19");
    $options[16][8]= array(" another city ","20");
    
    
    
    //okay let's find the following city name
    $state_id = '16';
    $city_id = '19';
    
    foreach($options as $arr1){
    
        foreach($arr1 as $arr2){
        
           $total_arr2 = count($arr2);  
           for($x = 0; $x < $total_arr2; $x++){
                
                if(($arr2[1] == $city_id) && (array_search($arr1, $options) == $state_id)){
                    echo $arr2[0];
                    exit;
                }
           }
        }
    }
    PHP:
     
    php-lover, Jul 11, 2009 IP
  5. promotingspace.net

    promotingspace.net Peon

    Messages:
    361
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I think you really love PHP
    Thanks
     
    promotingspace.net, Jul 11, 2009 IP
  6. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #6
    This should be ok for smaller scripts. For larger dealings, always use for loop instead of foreach, which makes a copy of the array and increases the overhead.
     
    ThePHPMaster, Jul 11, 2009 IP
  7. gilbertsavier

    gilbertsavier Guest

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Here is the sample code

    <?php
    $tstats=array ( "160x600" => array ("0" => "21","1"=> "1" ,"2" => "0" ,"3" => "0" ), "300x250" => array ( "0" => "32" ,"1" => "2" ,"2" => "0" ,"3" => "0" ) );
    //$products=array ( "160x600" => array ("0" => "21","1"=> "1" ,"2" => "0" ,"3" => "0" ));
    //$result="";
    while(list($k, $s) = each($tstats))
    {
    $prodects1=array ($k=>$s[0]);
    ///$result=array_push($tstats, $prodects1);
    print "<br>";
    //print_r($tstats);
    }
    ?>

    ----------------
    Thanks & regards
    Lokananth

    Live chat By miOOt
     
    gilbertsavier, Jul 13, 2009 IP