Saturday 15 September 2012

php - Retrieve object variables with dynamic key strings -


I have a "PHP" object, which is named "required". This "required" field is a nested array. I want to get the desired key (for attribute names and array index) from this object with dynamic strings. How can I get it?

  $ myObj = new StdClass; $ MyObj- & gt; = Array ("name_required" = & gt; 1, "course" => array ("course_name_required" => 1, "lessons_required" => 1, "lessons" => Lesson_name_required "=> 1," lesson_file_required "=> 1," is_viewed_required "=> 1,),)));); // The desired key I want to use dynamic string. $ Desired KArerepart = "[syllabus] [lesson] [commune_name_requard]"; $ Desired KAVIT Part = "required"; $ Desired = $ desired KAYAITUS part $ DesiredKeyArrayPart; // Get the value (1 return required) $ desired $ = $ myObj- & gt; $ Desired;   

I have to return 1 value, but it throws a runtime error: PHP notices: undefined property: stdClass :: $ required [syllabus] [lesson] [command_name_requard]

I understand how you can use an object / array properly, and to generate the path dynamically Are trying. The value you want the following code will allow you to pass a string, and filter that object through the object for multiple keys. array ("course_name_required" => 1, "lessons_required" => 1, "lessons" => Lesson_name_required "=> 1," lesson_file_required "=> 1," is_viewed_required "=> 1,),)));); // The desired key I want to use dynamic string. $ Desired KArerepart = "[syllabus] [lesson] [commune_name_requard]"; $ Desired KAVIT Part = "required"; // $ myObj-> our search object Set, use variable variable $ searchObject = $ myObj-> $ At the desired KAetive; // Match all the keys in our string preg_match_all ('/'Ä|(.*?)\]/', the desired $ KeyArrayPart, $ keyMap); // Foreach key that we got foreach ($ keyMap [1] as the $ key) {// $ $ in our $ search object in the value of the new key $ searchObject = $ searchObject [$ key]; } // We found our object var_dump ($ searchObject);

No comments:

Post a Comment