0

I need to be able to echo a value from a private property in one of my classes if a method is called within the class. It's a little tricky to explain so let me demostrate and hopefully someone can fill in the blank for me :)

     <?php
     class test {
          private $array['teachers']['classes'][23] = "John";


             public function __construct($required_array) {

                 $this->array['teachers']['classes'][23] = "John";
                 $this->array['students'][444] = "Mary";
                 $this->echo_array($required_array);

             }
             public function echo_array($array) {

                     // Echo the value from the private $this->array;
                     // remembering that the array I pass can have either 
                     // 1 - 1000 possible array values which needs to be 
                     // appended to the search. 

             }
     }

     // Getting the teacher:     
     $test = new test(array('teachers','classes',23));

     // Getting the student:     
     $test = new test(array('students',444));

?>

Is this possible?

2 Answers 2

3
$tmp = $this->array;
foreach ($array as $key) {
    $tmp = $tmp[$key];
}
// $tmp === 'John'
return $tmp; // never echo values but only return them
Sign up to request clarification or add additional context in comments.

Comments

0

An other approach to get value;

class Foo {
    private $error = false,
            $stack = array(
        'teachers' => array(
            'classes' => array(
                23 => 'John',
                24 => 'Jack',
            )
        )
    );

    public function getValue() {
        $query  = func_get_args();
        $stack  = $this->stack;
        $result = null;
        foreach ($query as $i) {
            if (!isset($stack[$i])) {
                $result = null;
                break;
            }
            $stack  = $stack[$i];
            $result = $stack;
        }

        if (null !== $result) {
            return $result;
        }
        // Optional
        // trigger_error("$teacher -> $class -> $number not found `test` class", E_USER_NOTICE);
        // or
        $this->error = true;
    }

    public function isError() {
        return $this->error;
    }
}

$foo = new Foo();
$val = $foo->getValue('teachers', 'classes', 24); // Jack
// $val = $foo->getValue('teachers', 'classes'); // array: John, Jack
// $val = $foo->getValue('teachers', 'classes', 25); // error
if (!$foo->isError()) {
    print_r($val);
} else {
    print 'Value not found!';
}

3 Comments

although this would work, what im trying to achieve is to get a single value from row in the array, so not bound to specifically 1 fixed set of inputs. So I need to be able to return for example $this->stack['students'][23] as well which would then fail because of my parameters not matching.
Ok, see changes in getValue.
Great work, I would have never through about it that way :) I want to say that the question may be in vain as I was trying to get an answer for y before considering that the problem may be with x, but it's a good resource for someone else in the future with the same issue. Thanks again :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.