Skip to content

Instantly share code, notes, and snippets.

@jmlagace
Forked from yoshitsugu/to_ruby_hash.php
Last active December 29, 2020 21:23
Show Gist options
  • Save jmlagace/b542a491e6ee250b37f95580e439e3a5 to your computer and use it in GitHub Desktop.
Save jmlagace/b542a491e6ee250b37f95580e439e3a5 to your computer and use it in GitHub Desktop.
PHPのArrayをRubyのHashに変換。PHP Array to Ruby Hash
function to_ruby_hash($array){
$result = "{";
foreach($array as $key => $value){
$result .= "\"".$key."\" => ";
if(is_array($value) || is_object($value)){
$result .= to_ruby_hash($value).",\n";
}else{
$result .= "\"".$value."\",";
}
}
return $result."}";
}
function to_ruby_hash2($array){
$result = "{";
foreach($array as $key => $value){
$result .= $key.": ";
if(is_array($value) || is_object($value)){
$result .= to_ruby_hash2($value).",\n";
}else{
$result .= "\"".$value."\", ";
}
}
return $result."}";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment