user999
user999

Reputation: 53

PHP getting result from foreach loop

I am new to PHP I am trying to get the foreach loop looping result. The loop I have

foreach($results as $result){
   $result = $result['names'];
}

Inside the loop are strings (John, Fred, Ann);

I am trying to get this outside the loop, when I try to echo $result I only have John, what should I do to get all three names?

I have tried to create an array like

$resultData= array();
foreach($results as $result){
  $resultData = $result['names'];
}
echo $resultData;

This doesn't work, does anyone have any suggestions?

Updated question: When I try to var_dump($resultData) I am getting

string(7) "John" string(7)"Fred" string(7)"Ann"

Upvotes: 1

Views: 10807

Answers (6)

user999
user999

Reputation: 53

Yes, I got it working, this is the code. Thank you everyone.

      $results;         
      $results[0];
      $results[1];
      $results[2];
       $t = array();
        foreach($results as $key => $value)
        {        
          $t[]= $value['name'];
        }  
        $a = "'".implode("','", $t)."'";
        echo $a;

Upvotes: 0

XsiSecOfficial
XsiSecOfficial

Reputation: 964

Here is an example too:

$employeeAges;
$employeeAges["Lisa"] = "28";
$employeeAges["Martin"] = "16";
$employeeAges["Erik"] = "35";
$employeeAges["Victor"] = "46";
$employeeAges["Grace"] = "34";

foreach( $employeeAges as $key => $value){
    echo "Name: $key, Age: $value <br />";
}

Output
Name: Lisa, Age: 28
Name: Martin, Age: 16
Name: Erik, Age: 35
Name: Victor, Age: 46
Name: Grace, Age: 34

you can do it like this too:

foreach syntax: **$something as $key => $value
For each element of the $employeeAges associative array I want to refer to the key as $key and the value as $value.

$employeeAges;
$employeeAges["Lisa"] = "28";
$employeeAges["Martin"] = "16";
$employeeAges["Erik"] = "35";
$employeeAges["Victor"] = "46";
$employeeAges["Grace"] = "34";

foreach( $employeeAges as $name => $age){
    echo "Name: $name, Age: $age <br />";
}

The operator "=>" represents the relationship between a key and value. You can imagine that the key points => to the value. In our example we named the key $key and the value $value. However, it might be easier to think of it as $name and $age. Below our example does this and notice how the output is identical because we only changed the variable names that refer to the keys and values.

You still get the same output.

Output
Name: Lisa, Age: 28
Name: Martin, Age: 16
Name: Erik, Age: 35
Name: Victor, Age: 46
Name: Grace, Age: 34

Upvotes: 2

UWU_SANDUN
UWU_SANDUN

Reputation: 1193

Try with this code

 $resultData= array();`
 foreach($results as $key => $result){
      $resultData[$key] = $result['names'];
 }
  echo "<pre>", print_r($resultData, true);

Upvotes: 1

Amit Chauhan
Amit Chauhan

Reputation: 682

try to use explode in foreach loop

 foreach($results as $result)
 {
      $result = explode(",",$result['names']);
 }
 print_r($result);

if you have a single name in $result then implode it with "," and explode it out side

     foreach($results as $result)
     {
          $val = implode(",",$result['names']);
     }
     $value = explode(","$val);
     print_r($value[0]);

Upvotes: 0

Maha Dev
Maha Dev

Reputation: 3965

Here is the proper code. You have not declared the i properly

$resultData= array();
$i = 0;
foreach($results as $result){
      $resultData[$i] = $result['names'];
      $i++
}
//for single
echo $resultData[0];

//for whole
print_r($resultData);

Upvotes: 0

Drudge Rajen
Drudge Rajen

Reputation: 7987

Something like this :

foreach($results as $result){
$resultData[] = $result['your_value'];
}
print_r($resultData);
echo $resultData[0];

Upvotes: 1

Related Questions