fork download
  1. <?php
  2. $row=array(
  3. "comments"=>"this is a test comment",
  4. "title"=>"title text", // key in $row not a value in $dictionary
  5. "current_path"=>"testpath"
  6. );
  7.  
  8. $dictionary=array(
  9. "0"=>"title", // $dictionary key equals zero (falsey)
  10. "current_directory"=>"current_path",
  11. "comments"=>"comments",
  12. "bogus2"=>"bogus2" // $dictionary value not a key in $row
  13. );
  14.  
  15. foreach($row as $k=>$v){
  16. if(($newkey=array_search($k,$dictionary))!==false){ // if not false, store value as $newkey
  17. $result[$newkey]=$v; // store value using new key
  18. }else{
  19. $result[$k]=$v; // no key swap, store unchanged element
  20. }
  21. }
  22. var_export($result);
Success #stdin #stdout 0.01s 82880KB
stdin
Standard input is empty
stdout
array (
  'comments' => 'this is a test comment',
  0 => 'title text',
  'current_directory' => 'testpath',
)