0

How to return $value after loop with its returned data ? I think to create array before loop and equal it to $v to use it after loop but it didn't work.

Any idea on how to solve this problem ?

// create array
$v = array();

// start loop
foreach ($this->json_data->locations as $key => $value) {
    if ($value->country_name == $data['city']->country_name)
        // return $value with data
        return $v = $value ; 
}

echo $v->country_name
1
  • 1
    Use break; once you get the value. Commented Sep 23, 2013 at 9:24

4 Answers 4

4
try this:

$v = array(); 
foreach ($this->json_data->locations as $key => $value) {
 if ($value->country_name == $data['city']->country_name)
 {
    if(!in_array($value,$v))
    {
     array_push($v,$value);                 
    }
 }
}
Sign up to request clarification or add additional context in comments.

Comments

2

try this

 $v = array();
    $i=0;
    // start loop
                foreach ($this->json_data->locations as $key => $value) {
                    if ($value->country_name == $data['city']->country_name)
    // return $value with data
                         $i++;
                         $v[$i] = $value ; 
                }
    //print $v
                print_r($v)

Comments

0

If like using 'return' try this.

$v = iLikeUsingReturn($this,$data);

function iLikeUsingReturn($t,$d){
  foreach ($t->json_data->locations as $key => $value) {
                if ($value->country_name == $d['city']->country_name)
                    return $value ; 
  }
  return array();
}

Comments

0

I think the following code will helps you.

// create array
     $v = array();
// start loop
        foreach ($this->json_data->locations as $key => $value) {
            if ($value->country_name == $data['city']->country_name)
// return $value with data                  
           array_push($v, $value); 
        }
             return $v;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.