How to print max() value in JSON - php

How can I print highest value in JSON form result of 3 functions.
Controller :
class TimeFrameStatisticsController extends Controller
{
public function one()
{
return 3;
}
public function two()
{
return 1;
}
public function three()
{
return 4;
}
//FUNCTION FOR 4rth to get HIGHEST value
public function HighestValue() {
$func_names = ['three','one','two'];
// Call each method and store result.
foreach($func_names as $name) {
$results[$name] = $name();
}
$max = max($results);
$max_funcs = array_keys($results, $max);
return response()->json($max_funcs);
}
}
Error in OUTPUT of public function HighestValue() for API : Call
to undefined function public function one()
AND
Other 3 functions are result by API:
My Question is what should i do? Should I create Object and how ?

This function giving me perfect result of highest value from 3 different.
public function allCmp()
{
$firstTradeValue = $this->one();
$lastTradeValue = $this->two();
$otherTradeValue = $this->three();
//Set all in one array
$allTrades = array("firstHourTrades" => $firstTradeValue, "lastHourTrades" => $lastTradeValue, "otherHoursTrades" => $otherTradeValue );
//Get the highest value
$highestValue = max($allTrades);
//Get the corresponding key
$key = array_search($highestValue, $allTrades);
return response()->json($key);
}

Related

Define value of class when assigned to variable in PHP

In PHP I can use \ArrayAccess to create an object such that it can be assigned as variable (an array) but also execute some other stuff while doing it, is it possible to do the same with a single value variable?, for example:
$a = new MyClass(5, "some other stuff")->print(); //Prints 'some other stuff'
$b = $a * 2;
echo $b; //Prints 10
The idea would be that the first parameter is returned when the variable is assigned but at the same time I can do something else with the second parameter, so far I have only manage to do it ussing the __invoke magic method, but the end result is something like this:
$a = new MyClass(5, "some other stuff")->print(); //Prints 'some other stuff'
$b = $a() * 2;
echo $b; //Prints 10
Is it possible to do it without the __invoke method?
This is the way I managed to do it with \ArrayAccess (just in case it helps)
class MySQLK_customArray implements \ArrayAccess{
private $storage = array();
private $queryk;
public function __construct($fetch_array, $query){
$this->queryk = $query;
$this->storage = $fetch_array;
}
public function offsetSet($key, $value)
{
if (is_null($key)) {
$this->storage[] = $value;
} else {
$this->storage[$key] = $value;
}
}
public function offsetExists($key)
{
return isset($this->storage[$key]);
}
public function offsetUnset($key)
{
unset($this->storage[$key]);
}
public function offsetGet($key)
{
if (! isset($this->storage[$key])) {
return null;
}
$val = $this->storage[$key];
if (is_callable($val)) {
return $val($this);
}
return $val;
}
public function print(){
echo $this->queryk.";";
return $this;
}
public function get(){
return $this->storage;
}
}
It can be used this way:
$arr = new MySQLK_customArray(array("id" => 1),"SELECT * FROM Table LIMIT 1")->print(); //Prints 'SELECT * FROM Table LIMIT 1'
echo $arr['id']; //Prints 1

Change variable returned as reference from object method PHP

I would like overwrite array element returned as reference. I can do it like this:
$tmp = $this->event_users_details;
$tmp = &$tmp->firstValue("surcharge");
$tmp += $debt_amount;
I would do it in one line like:
$this->event_users_details->firstValue("surcharge") += $debt_amount;
but I get Can't use method return value in write context
Where $this->event_users_details is a object injected in constructor.
My function look like:
public function & firstValue(string $property) {
return $this->first()->{$property};
}
public function first() : EventUserDetails {
return reset($this->users);
}
and users is a private array.
You can't do it without temporary variable stores "surcharge" value.
From documentation:
To return a reference from a function, use the reference operator & in both the function declaration and when assigning the returned value to a variable:
<?php
function &returns_reference()
{
return $someref;
}
$newref =& returns_reference();
?>
I checked it with this code:
class Item
{
public $foo = 0;
}
class Container
{
private $arr = [];
public function __construct()
{
$this->arr = [new Item()];
}
public function &firstValue($propNme)
{
return $this->first()->{$propNme};
}
private function first()
{
return reset($this->arr);
}
}
$container = new Container();
var_dump($value = &$container->firstValue('foo')); // 0
$value += 1;
var_dump($container->firstValue('foo')); // 1

Assign property value using an anonymous function

My class is like this:
<?php
class ExampleClass{
private $example_property = false;
public function __construct(){
$this->example_property = function() {
$this->example_property = 1;
return $this->example_property;
};
}
public function get_example_property(){
return $this->example_property;
}
}
$example = new ExampleClass();
echo $example->get_example_property();
Property $example_property must be false until you call it, then, the first time it is called, I want to assign 1 to it. What's wrong with my code?
Error: Error Object of class Closure could not be converted to string on line number 20.
I just tried to play a little bit with your code.
To make it possible, you'll have to find out, whether your variable is a function (defined in your __construct) or the number set by this function
<?php
class ExampleClass{
private $example_property = false;
public function __construct(){
$this->example_property = function() {
$this->example_property = 1;
return $this->example_property;
};
}
public function get_example_property(){
$func = $this->example_property;
if(is_object($func) && get_class($func)=='Closure'){
return $func();
}
return $func;
}
}
$example = new ExampleClass();
echo $example->get_example_property(); //returning 1
echo $example->get_example_property(); //returning 1 again
But anyway, I don't see any sense in doing this.
The typical solution would be something like this:
class ExampleClass{
private $example_property = false;
public function __construct(){
//usually initializing properties goes here.
//$this->example_property = 1;
}
public function get_example_property(){
// I think you want a value to be initialzed only if needed.
// so then it can go here.
if(!$this->example_property) {
$this->example_property = 1; //initialize it, if needed
}
return $this->example_property;
}
}
$example = new ExampleClass();
echo $example->get_example_property(); //returning 1
echo $example->get_example_property(); //returning 1 again

Automatically sort all Arrays in controller when getting from database PHP

So I have this controller that passes an associative array called $pagedata to the view. Inside this array are 3 more associative arrays, and the view renders 3 select elements with the array data as options. I want to sort the 3 arrays but I don't want to write sort 3 times here or add order_by into the query methods, because there are dozens of similar pages and I don't want to write hundreds of sort method calls. I was told I could solve this in the constructor. I was wondering if there's an OOP solution that lets me automatically sort all child arrays inside $pagedata.
class Sku extends CI_Controller {
protected $pagedata = array();
public function __construct()
{
parent::__construct();
$this->load->model('mc');
}
public function inventory()
{
$this->pagedata['class_ids'] = $this->mc->get_class_ids();
$this->pagedata['retail_readys'] = $this->mc->get_all_retail_ready();
$this->pagedata['statuses'] = $this->mc->get_all_status();
}
}
Edit:
I'm exploring using an ArrayObject or wrapping $pagedata in an object and watch for changes.
ok this will be painfull for codeigniter but yes a kind of solution
public function __construct()
{
parent::__construct();
$this->load->model('mc');
$class_ids = $this->mc->get_class_ids();
$class_ids = $this->sortAscending($class_ids, $key);
$this->pagedata['class_ids'] = $class_ids;
$retail_readys = $this->mc->get_all_retail_ready();
$class_ids = $this->sortAscending($class_ids, $key);
$this->pagedata['class_ids'] = $class_ids;
$statuses = $this->mc->get_all_status();
$statuses = $this->sortAscending($class_ids, $key);
$this->pagedata['statuses'] = $statuses;
}
function sortAscending($accounts, $key)
{
$ascending = function($accountA, $accountB) use ($key) {
if ($accountA[$key] == $accountB[$key]) {
return 0;
}
return ($accountA[$key] < $accountB[$key]) ? -1 : 1;
};
usort($accounts, $ascending);
return $accounts;
}
public function inventory()
{
// already get values
//$this->pagedata['class_ids'] = $this->mc->get_class_ids();
//$this->pagedata['retail_readys'] = $this->mc->get_all_retail_ready();
//$this->pagedata['statuses'] = $this->mc->get_all_status();
$this->load->view('index',$this->pagedata);
}
public function another_function()
{
// already get values
//$this->pagedata['class_ids'] = $this->mc->get_class_ids();
//$this->pagedata['retail_readys'] = $this->mc->get_all_retail_ready();
//$this->pagedata['statuses'] = $this->mc->get_all_status();
$this->load->view('another page',$this->pagedata);
}

codeigniter: how to access more than one variable of other functions in one class?

For example I have a class like this
public function data() {
$a = 1;
$b = 2;
$c = 3;
}
public function codeigniter() {
$a, $b, $b //how i can get the value??
}
How can I get the value of $a, $b, $c so that I can use it in codeigniter() function? I use codeigniter? Do I need to add a return;?
I assume the two function are in both class and in a controller class of codeigniter
class Sample extends CI_Controller {
// declare variables
public $a;
public $b;
public $c;
public function __construct() {
// call codeigniter method
$this->codeigniter();
}
public function data() {
$this->a = 10;
$this->b = 11;
$this->c = 12;
}
public function codeigniter() {
// call method data();
$this->data();
echo $this->a; // prints 10
echo $this->b; // prints 11
echo $this->c; // prints 12
}
}
First create a array and store the values in it and return the array.
public function data() {
$data = array();
$data['a']=1;
$data['b']=2;
$data['c']=3;
return $data;
}
Then you can simply call the function and access the returned data.
public function codeigniter() {
$data_value = $this->data();
echo $data_value['a'];
echo $data_value['b'];
echo $data_value['c'];
}
To get more than one variable from a function you must place those variables in an array and return the array
public function data() {
$a = 1;
$b = 2;
$c = 3;
$data = array();
array_push($data, $a,$b,$c);
return $data;
}//data function ends
public function codeigniter() {
$data = $this->data();
//now use $data
}

Categories