Rainbard
Rainbard

Reputation: 341

Retrieve object variables with dynamic key strings

I have a PHP object with an attribute named "required". This "required" field is a nested array. I want to retrieve a desired key from this object with dynamic strings (for attribute names and array indexes). How can I achieve that?

$myObj = new StdClass;
$myObj->required = array(
        "name_required" => 1,
        "courses" => array(
                "course_name_required" => 1,
                "lessons_required" => 1,
                "lessons" => array(
                        "lesson_name_required" => 1,
                        "lesson_file_required" => 1,
                        "is_viewed_required" => 1,
                    ),
            ),

);
// Desired Key I want to retrieve using dynamic strings.
$desiredKeyArrayPart = "[courses][lessons][lesson_name_required]";
$desiredKeyAttributePart = "required";
$desiredKey = $desiredKeyAttributePart . $desiredKeyArrayPart;

// Get the value (Should return 1)
$desiredValue = $myObj->$desiredKey;

I want the value 1 to be returned but this throws a runtime error: PHP Notice: Undefined property: stdClass::$required[courses][lessons][lesson_name_required]

Upvotes: 2

Views: 3076

Answers (3)

SpenserJ
SpenserJ

Reputation: 802

I'm assuming you know how to access an object/array properly, and are trying to dynamically generate the path to the value that you want. The following code will allow you to pass a string, and filter through an object for the multiple keys in that string.

<?php
$myObj = new StdClass;
$myObj->required = array(
  "name_required" => 1,
  "courses" => array(
    "course_name_required" => 1,
    "lessons_required" => 1,
    "lessons" => array(
      "lesson_name_required" => 1,
      "lesson_file_required" => 1,
      "is_viewed_required" => 1,
    ),
  ),
);
// Desired Key I want to retrieve using dynamic strings.
$desiredKeyArrayPart = "[courses][lessons][lesson_name_required]";
$desiredKeyAttributePart = "required";

// Set our searchObject to $myObj->required using Variable Variables
$searchObject = $myObj->$desiredKeyAttributePart;
// Match all of the keys in our string
preg_match_all('/\[(.*?)\]/', $desiredKeyArrayPart, $keyMap);
// Foreach key that we found
foreach ($keyMap[1] as $key) {
  // Change our $searchObject to the new key's value
  $searchObject = $searchObject[$key];
}
// We found our object
var_dump($searchObject);

Upvotes: 3

Dany Caissy
Dany Caissy

Reputation: 3206

Replace this :

$desiredKeyArrayPart = "[courses][lessons][lesson_name_required]";
$desiredKeyAttributePart = "required";
$desiredKey = $desiredKeyAttributePart . $desiredKeyArrayPart;

// Get the value (Should return 1)
$desiredValue = $myObj->$desiredKey;

By this :

$desiredValue = $myObj->required['courses']['lessons']['lesson_name_required'];

The way you were trying to do it will not work. Also, quoting the array values is preferable to avoid warnings.

Upvotes: 0

Patrick Kostjens
Patrick Kostjens

Reputation: 5105

The value of $desiredKey is required[courses][lessons][lesson_name_required]. That doesn't exist as a property. Right now $desiredKey is just a string. PHP doesn't interpret the string to access the array properties. You also forgot the quotes around the array keys. You can access the value you want like this:

$desiredValue = $myObj->required["courses"]["lessons"]["lesson_name_required"];

Upvotes: 2

Related Questions