0
class Field extends Thread{
        public $dataFields;

        public function addValue($table,$key,$data){
          print_r($this->dataFields[$table]);
          echo "\ndata: ".$data."\n";
          if(isset($this->dataFields[$table][$key])){
              $this->dataFields[$table][$key]= $data;
              echo "yes\n";
            }
          else echo "no\n";
          print_r($this->dataFields[$table]);

         }

         public function setVariables($con){
            $result = mysqli_query($con,"select * from property,city limit 1");
            $row=mysqli_fetch_fields($result);
            foreach ($row as $key => $value) {
                $propData[$value->table][$value->name]="";
            }
            $propData["property"]["portalId"]=2;
            $propData["property"]["area"]="delhi";
            $this->dataFields=$propData;
            $this->addValue("property","id","1");
            //print_r($this->dataFields);
            $this->dataFields["property"]["id"]=1;
            var_dump($propData);
            var_dump($this->dataFields);
        }
    }

as you can see the code I am updating the value of array through function addValue, if the key is set in the dataFields array then it will get updated but in this its not happening when I try to call the function:

addValue("property","id","1"); through function setVariables() (see the output below)

it prints the value of $data it prints "yes". The class variable dataFields is not getting updated.

[Edit] When I am using Threads extended class it don't get updated but when I don't use Thread extended class then it get updated.

    Array                                 
    (                                             
        [id] =>                                   
        [portalId] => 2                          
        [area] => delhi                           
    )                                             

    data: 1                                 
    yes                                           
    Array                                         
    (                                             
        [id] =>                                   
        [portalId] => 2                          
        [area] => delhi
    ) 
    array(2) {                        
         ["city"]=>                              
         array(3) {                              
           ["id"]=>                              
           string(0) ""                          
           ["name"]=>                            
           string(0) ""                          
           ["otherNames"]=>                      
           string(0) ""                          
         }                                                                
         ["property"]=>                          
         array(3) {                             
           ["id"]=>                              
           string(0) ""                          
           ["portalId"]=>                        
           int(2)                                
           ["area"]=>                            
           string(5) "delhi"                     
         }
        }
    array(2) {
          ["city"]=>
          array(3) {
            ["id"]=>
            string(0) ""
            ["name"]=>
            string(0) ""
            ["otherNames"]=>
            string(0) ""
          }
          ["property"]=>
          array(3) {
            ["id"]=>
            string(0) ""
            ["portalId"]=>
            int(2)
            ["area"]=>
            string(5) "delhi"
           }
        }
12
  • Please show us your input, exact output and your expected output. (Works fine for me) Also show us your full code Commented Feb 6, 2015 at 23:22
  • Why is the output: data: DEFAULT and not data: 1 ? Please show us the full code Commented Feb 6, 2015 at 23:35
  • sorry I copied output of different test case I have corrected it now! Commented Feb 6, 2015 at 23:38
  • Change addValue("property","id","1"); -> $this->addValue("property","id","1"); does that do the trick for you? Commented Feb 6, 2015 at 23:38
  • Change your print_r() calls to var_dump() and look into the source code if there are any "hidden" characters Commented Feb 6, 2015 at 23:40

1 Answer 1

0

I can confirm the following works for me in 5.6.3:

class Field {
    public $dataFields;

    public function addValue($table,$key,$data){
      print_r($this->dataFields[$table]);
      echo "\ndata: ".$data."\n";
      if(isset($this->dataFields[$table][$key])){
          $this->dataFields[$table][$key]= $data;
          echo "yes\n";
        }
      else echo "no\n";
      print_r($this->dataFields[$table]);

     }

     public function setVariables(){
        $propData["property"]["id"] = '';
        $propData["property"]["portalId"]=2;
        $propData["property"]["area"]="delhi";
        $this->dataFields=$propData;
        $this->addValue("property","id","1");
        //print_r($this->dataFields);
    }
}

$field = new Field;
$field->setVariables();

with output:

Array
(
    [id] => 
    [portalId] => 2
    [area] => delhi
)

data: 1
yes
Array
(
    [id] => 1
    [portalId] => 2
    [area] => delhi
)

I would recommend reinstalling the latest version of PHP.

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

19 Comments

This doesn't help OP or anyone, it's not a answer. You can put a comment under OP's question. Everyone could say: It works for me and we would have 200 answers here!
@ayushlodhi The output which you posted on your question is this copied from the source code of your script?
@ayushlodhi Did you clicked: right click on your page -> view source code and copied the var_dump from there?
I copied form the terminal
@Rizier123 You pretty much posted the exact same thing as me in the 13th comment of the question lol
|

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.