0

Have array like below :

Array
(
[0] => stdClass Object
    (
        [city_id] => 2222
        [city_name] => newCity
    )

[1] => stdClass Object
    (
        [city_id] => 4444
        [city_name] => oldCity
    )

[2] => stdClass Object
    (
        [city_id] => 6666
        [city_name] => newCity2

    )
)

Now, I want get city_id when I set city_name, how can I get it in my codes?

For example, If I set newCity2 for city_name then I see 6666 for city_id and etc.

user1978142
  • 7,946
  • 3
  • 17
  • 20
sIiiS
  • 195
  • 1
  • 1
  • 11
  • 1
    http://stackoverflow.com/questions/5156841/stdclass-object-and-array-how-to-using-php?rq=1.. Perhaps this? – briosheje Apr 27 '14 at 22:07

2 Answers2

0

You need to loop through it as

foreach($data as $key=>$val){
  echo 'City ID ::'.$val->city_id."<br />";
  echo 'City Name ::'.$val->city_name."<br />";
}

If you want to see a particular data then you can modify above as

foreach($data as $key=>$val){
  if($val->city_name == 'newCity2'){
   echo 'City ID ::'.$val->city_id."<br />";
   break;
  }
}


function getCityIDByName($cityname,$data){

    $return = '';
    foreach($data as $key=>$val){
          if($val->city_name == $cityname){
           $return = $val->city_id
           break;
          }
    }
    return $return ;
}

$city_id = getCityIDByName ('newCity2',$data);
Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63
0

You would have to iterate over the array and check city_name to get the corresponding ID. You could also do this initially to set up an array with city_name as the key and city_id as the value.

foreach ($array as $value) {
    if ($value->city_name == $compareCityName) {
        $city_id = $value->city_id;
    }
}
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • I did set like you but when I want to see result then got error `Undefined variable: cityId`, my code is : `$city = 'oldCity'; foreach($results as $value){ $cityName = $value->city_name; if ($cityName == $city) { $cityId = $value->city_id; } } var_dump ($cityId); exit;` – sIiiS Apr 27 '14 at 22:28
  • @sIiiS that means that no city named `oldCity` was found – Explosion Pills Apr 27 '14 at 22:28
  • I did set before `$city = 'oldCity';` as you can see in my codes, but why can not find it? – sIiiS Apr 27 '14 at 22:32
  • @sIiiS because it's not there? Depends on the contents of `$results` – Explosion Pills Apr 27 '14 at 22:35
  • value of `$results` is same as `$array` not else, and in the `$results` in my main Q, oldCity is available – sIiiS Apr 27 '14 at 22:41
  • @sIiiS are you sure that `$results` is defined in your code? Did you `var_dump` it? What does it say? – Explosion Pills Apr 27 '14 at 22:42
  • yes, sure, I set print_r for `$results` and then see array codes in my main question in the first post – sIiiS Apr 27 '14 at 22:45
  • @sIiiS code is working fine for me. I copied the code from your comment and the array you posted above and it worked perfectly. Only thing I can think of is that `oldCity` has some non-printing character or something. Instead of comparing to the string `oldCity`, try comparing to `$results[1]->city_name` – Explosion Pills Apr 27 '14 at 22:48
  • yes, you right! when I var_dump value of `city_name` from `$results` then back me oldCity, oldCity has 7 string but it return 13 string and I don't know why!!! ... the value of `city_name` are unicode – sIiiS Apr 27 '14 at 23:00
  • also find that, all string return false, they back (2x(string)city_name)+1 , do you know, how can I solve it ? – sIiiS Apr 27 '14 at 23:04