1

I wan't to add values continuously in a while loop in associative array. I have used the following technique, but when I am displaying it, it stores the last value only and by searching I have found out that people do the same. What is the problem in the code?

My Php code is:

while($row2=mysqli_fetch_array($result2))
            {
           $first=$row2['MDid'];
           $second=$row2['MD_FullName'];          

           $data=array($first=>$second);

            }

           var_dump($data);

My Output is:

array(75) { [0]=> array(1) { ["AB0001"]=> string(29) "Arthur Boshnack, M.D, F.A.C.G" } [1]=> array(1) { ["AJ0001"]=> string(16) "Anwer Jaffri, MD" } [2]=> array(1) { ["AK0001"]=> string(16) "Dr.Adam Kotowski" } [3]=> array(1) { ["ALZ001"]=> string(22) "Kheir Al-Zouhayli, M.D" } [4]=> array(1) { ["AM0001"]=> string(17) "Dr.Ambrose Mgbako" } [5]=> array(1) { ["AMW001"]=> string(21) "Dr.Audrey M. Weissman" } [6]=> array(1) { ["AN0001"]=> string(25) "Dr.Anthony Napolitano,M.D" } [7]=> array(1) { ["BS0001"]=> string(17) "Bhupendra Shah,MD" } [8]=> array(1) { ["BT0001"]=> string(19) "Dr.Birendra Trivedi" } [9]=> array(1) { ["CA0001"]=> string(14) "Claudia Aroche" } [10]=> array(1) { ["CG0001"]=> string(13) "Dr.Cary Golub" } [11]=> array(1)

But when I echo:

$data["VSV001"]

I get nothing

2
  • 1
    $data[$first]= $second;.. Commented Mar 17, 2016 at 11:56
  • Or $data[] = array($first=>$second); (depending on exactly what you want to do) Commented Mar 17, 2016 at 11:57

3 Answers 3

2

Try doing it like this.

while($row2=mysqli_fetch_array($result2)){
    $first=$row2['MDid'];
    $second=$row2['MD_FullName'];          
    $data[$first]= $second;
}
var_dump($data);
Sign up to request clarification or add additional context in comments.

Comments

0

I wan't to add values continuously in a while loop in associative array. I have used the following technique, but when I am displaying it, it stores the last value only and by searching I have found out that people do the same. What is the problem in the code?

       $data=array(); //declare it globally, some versions of php need initialization of the variables when they are accessed out of any scope.

       while($row2=mysqli_fetch_array($result2))
       {
        $first=$row2['MDid'];
        $second=$row2['MD_FullName'];          
        $data[$first]=>$second;
       }

       var_dump($data);

Comments

0

Use this code

       $data=array();
       while($row2=mysqli_fetch_array($result2))
        {
       $first=$row2['MDid'];
       $second=$row2['MD_FullName'];          

       $data[]=array($first=>$second);

        }

       var_dump($data);

Do not use just

$data["VSV001"]

You have to use like

$data[73]["VSV001"]

Instead of 73 use your index number.

 { [0]=> array(1) { ["AB0001"]=> string(29) "Arthur Boshnack, M.D, F.A.C.G" }

like

echo $data[0]["AB0001"];

You will get output like

Arthur Boshnack, M.D, F.A.C.G

2 Comments

This is not wrong...but is wrong according to the requirement. This way it will insert the new array at a new index in $data array, which is wrong
Instead of var_dump($data) use print_r($data) it looks better

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.