1

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"
4
  • have you tried array_push()? Commented Mar 8, 2016 at 3:44
  • you should consult the manual more in this topic, this is just simple array usage Commented Mar 8, 2016 at 3:45
  • Can you try Explode In For each Loop ? Commented Mar 8, 2016 at 3:56
  • Thank you for all your replies, this loop is an multidimensional array, I am trying to get a single loop result out, so I don't think explode the loop is what I wanted. Commented Mar 8, 2016 at 4:03

6 Answers 6

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, this was very helpful.
Let me know if you got any questions I may be able to help you =) Regards
1

Something like this :

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

3 Comments

can you please update your question with the array you get by doing var_dump($results) ?
Yes you are right, I am trying to get this print_r result. I tried $results = print_r($resultData, true); but this returns nothing, what is the best way to get this result? Thank you very much for your help
@user2983797 try this var_dump($results)
1

Try with this code

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

Comments

0

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);

6 Comments

just asking, why do you need to declare $i, if you use foreach anyway?
@GaLaBoOnZ , actually i relates the answer with the question. He used variable 'i', that why i told him the correct format of using $i. Nothing else. so many other ways are available but user should understand, what mistake he had done.
@MahaDev Oh ok, my bad just noticed that the post was edited and removed $i from the post. :P
@GaLaBoOnZ, whatever i got -1. :(
@MahaDev +1 for explanation :)
|
0

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]);

1 Comment

if you have signle name in $result then use implode of it and explode it in out side
0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.