简体   繁体   中英

Php Get single value from array

I hava a variable like $data= $regression->getCoefficients(); in Regression Analysis. When i print this i got this output

Regression\\Matrix Object ( [rows:protected] => 4 [columns:protected] => 1 [MainMatrix:protected] => Array ( [0] => Array ( [0] => -125 ) [1] => Array ( [0] => 9.8888888888869 ) [2] => Array ( [0] => 18.75 ) [3] => Array ( [0] => -1.1388888888905 ) ) )

But i need to get single value from array like Array[0]=>-125 or -125

Thank you in advance

You should look into Regression\\Matrix methods as Mark Baker suggests in the comment: there should be some method exposing the protected MainMatrix member.

And if there isn't any... looks like object can be typecasted into (associative) array and the protected members have keys prefixed with chr(0).'*'.chr(0) (see @fardelian's comment here ). It would be rather against the Regression\\Matrix design, but you can write an "exposer":

function getProtectedValue($obj,$name) {
  $array = (array)$obj;
  $prefix = chr(0).'*'.chr(0);
  return $array[$prefix.$name];
}

(You could achieve the same in less hacky, but more bulky way using reflection .)

Now you can access your desired value as

$data = $regression->getCoefficients();
$MainMatrix = getProtectedValue($data,"MainMatrix");
echo $MainMatrix[0][0]; // -125

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM