简体   繁体   English

PHP从数组中获取单个值

[英]Php Get single value from array

I hava a variable like $data= $regression->getCoefficients(); 我有一个像$data= $regression->getCoefficients();这样的变量$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 但是我需要从数组中获取单个值,例如Array[0]=>-125-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. 您应该像Mark Ba​​ker在评论中建议的那样研究Regression \\ Matrix方法:应该有一些方法公开受保护的MainMatrix成员。

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 ). 如果没有任何...貌似对象可以被类型强制转换成(关联)阵列和保护成员有前缀键chr(0).'*'.chr(0)见@ fardelian的评论在这里 )。 It would be rather against the Regression\\Matrix design, but you can write an "exposer": 这可能会违反Regression \\ Matrix设计,但是您可以编写一个“暴露者”:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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