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
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?
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:
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.
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