phpstdclass

Is there a way to escape the "." character in php?


I have an stdClass object that I have dynamically created from a JSON using json_decode(). I am trying to access a field by calling $value->V.X->processedField but this field has a period. This is giving me a syntax error. Is there a way to somehow escape the period in my code, or am I going to have to rename the field in my JSON?


Solution

  • <?php
    // example code
    
    $value = json_decode('{"V.X": {"processedField":"the value"}}');
    print_r($value->{"V.X"}->processedField);
    // or 
    $var = "V.X";
    print_r($value->$var->processedField);