Jump to content


Photo

Insert new array data inside a stdClass Object

php array mysql

Best Answer objnoob, 28 October 2013 - 11:33 PM

Anyways, you should try

$a = (array) $array[$key];
$a['stared'] = 1;
$array[$key] = (object) $a;
Go to the full post


  • Please log in to reply
3 replies to this topic

#1 lilmer

lilmer

    Advanced Member

  • Members
  • PipPipPip
  • 108 posts
  • LocationSomewhere here in the universe.

Posted 28 October 2013 - 11:19 PM

I've got this sample of array data

Array
(
    [0] => stdClass Object
        (
            [fileid] => 333
            [filename] => Douglas           
            [datecreated] => 2013-10-25 09:53:27
            [datemodified] => 2013-10-25 09:53:27
            [fileAccess] => 1
            [user_id] => 70          
        )

    [1] => stdClass Object
        (
            [fileid] => 326
            [filename] => Yeah baby           
            [datecreated] => 2013-10-24 09:02:05
            [datemodified] => 2013-10-24 09:02:05
            [fileAccess] => 1
            [user_id] => 70                     
        )

    [2] => stdClass Object
        (
            [fileid] => 329
            [filename] => UNTITLED File
            [datecreated] => 2013-10-25 07:14:53
            [datemodified] => 2013-10-25 07:14:53
            [fileAccess] => 1
            [user_id] => 70            
        )

    [3] => stdClass Object
        (
            [fileid] => 330
            [filename] => UNTITLED FILE
            [datecreated] => 2013-10-25 07:17:55
            [datemodified] => 2013-10-25 07:17:55
            [fileAccess] => 1
            [user_id] => 70               
        )

    [4] => stdClass Object
        (
            [fileid] => 332
            [filename] => UNTITLED FILE
            [datecreated] => 2013-10-25 08:42:35
            [datemodified] => 2013-10-25 08:42:35
            [fileAccess] => 1
            [user_id] => 70
            [statuss] => 1
        )

)

and by fileid I would like to check if the fileid is also existing on the other mysql. .

 

so I did this 

foreach($array as $key => $row){
   
   $query = $this->model->get($row->fileid); 
   //query return true or false;
   
   if($query){
     $array[$key]['stared'] = 1;
   }  
}

but it gives an error. How will I going to insert a new variable on the current array loop if the fileid is existing. 

 

e.g.

 [4] => stdClass Object
        (
            [fileid] => 332
            [filename] => UNTITLED FILE
            [datecreated] => 2013-10-25 08:42:35
            [datemodified] => 2013-10-25 08:42:35
            [fileAccess] => 1
            [user_id] => 70
            [statuss] => 1
            [stared] => 1 //IF STARED RETURN TRUE
        )

Thanks

 

 

 



#2 objnoob

objnoob

    Advanced Member

  • Members
  • PipPipPip
  • 332 posts

Posted 28 October 2013 - 11:26 PM

What is your error?



#3 objnoob

objnoob

    Advanced Member

  • Members
  • PipPipPip
  • 332 posts

Posted 28 October 2013 - 11:33 PM   Best Answer

Anyways, you should try

$a = (array) $array[$key];
$a['stared'] = 1;
$array[$key] = (object) $a;

Edited by objnoob, 28 October 2013 - 11:34 PM.


#4 lilmer

lilmer

    Advanced Member

  • Members
  • PipPipPip
  • 108 posts
  • LocationSomewhere here in the universe.

Posted 28 October 2013 - 11:57 PM

Nice, thanks it works.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com