I have an array in php:
$array = [1,2,3];
When I do:
while(yield $array->advance())
I get Call to a member function advance() on array
How do I turn my array into an iterator?
You can call ->advance() only on instances of Amp\Iterator.
So you need to convert your basic php array first with the fromIterable method.
Amp\Iterator\fromIterable($array)
Related
i want return data if a < b then a = b, if using foreach i have to make empty array first and parsing data to te empty array, how if record i much, that take time
public function getDepreciation(){
$values = $this->repository->getDepreciation();
$data = [];
foreach($values as $value){
if($value->depresiation_per_month > $value->balance_value){
$value->depresiation_per_month = $value->balance_value;
}
array_push($data,$value->depresiation_per_month,$value->balance_value);
}
return $data;
}
is there is simple code than my above, since i have to make relist many array
You can convert this array in a Collection then handle it easier by using the transform method.
First you have to create the collection using your array Create Collection
$values = collect($values);
Then you can iterate over the collection using the transform method and apply your logic Transform Method
Finally you have to convert your collection into an array using the toArray method toArray Method
I hope this works for you
I have object with setter and getter
class obj{
private $a;
private $b;
}
I would like to create an array that get only the value of the object , for example
array = ["1","2"]
tried get_object_vars but its an associative array
To condense an associative array to an indexed array, use array_values:
array_values(get_object_vars($object));
However, given that you have private member variables, you might just want to use an array cast instead of get_object_vars:
array_values((array)$object);
See it online at 3v4l.org.
My question is what is a neat way to obtain an array of objects from an array of classes.
The array of classes I get by using array_filter() on get_declared_classes().
EDIT:
My own attempts were pretty correct, the thing was I forgot to return value of in_array() in callback function :
$classes_array = array_filter(
get_declared_classes(),
function($class_name){
return in_array('IItem', class_implements($class_name));
}
$objects_array;
foreach($classes_array as $class){
$objects_array[] = new $class();
}
You can use array_map(), and refer to Creating PHP class instance with a string
$objects = array_map(function($v){
return new $v();
}, get_declared_classes());
I am attempting to use array_merge to merge two 2d arrays, but the result is always empty. There is no need to show you the whole php class, but I will paste the relevent code here. The problem is that when I use array_merge I get an empty array. I am calling my function called charts which in turn gets 2 arrays by calling 2 functions, then I wish to merge these arrays. The array functions do return a valid array, but when I merge them, I get nothing.
public function charts(){
$sales=$this->salesChart();
$expenses=$this->$this->expensesChart();
$result = array_merge($sales,$expenses);
print_r($result);
return $result;
}
private function salesChart(){
$salesArray = array(
array('2016-05', 14690),array('2016-06', 2785),array('2016-07', 14690),array('2016-08',23345),array('2016-09', 10345),array('2016-10',12456)
);
return $salesArray;
}
private function expensesChart(){
$expensesArray = array(
array('2016-05', 14690),array('2016-06', 2785),array('2016-07', 14690),array('2016-08',23345),array('2016-09', 10345),array('2016-10',12456)
);
return $expensesArray;
}
You have to change this line
$expenses=$this->$this->expensesChart();
for
$expenses=$this->expensesChart();
because I tested the array_merge and it works.
It is what it seems for the code you have provided.
How come that I can't access array by index $obj->property[0] but can iterate through the array (each has only one value) and then get the value.
foreach($obj->property as $inner_obj)
{
foreach($inner_obj->property as $inner_inner_obj)
echo $inner_inner_obj->property;
}
If I access array by index I get Undefined offset: 0
Edit: Output of var_dump
array(1) {
[0]=> object(FooClass)#141 {
// yadda yadda
}
}
if you make class by implements Countable, Iterator you can make a object that work with foreach but it is not array
$obj is not array its stdClass Object and
because of that $obj->property[0] will show you Undefined offset: 0
try to use it like this for getting 1st value of property
$obj->property->property;
Because you're looping over properties of an array in a foreach. With [0] you explicitly try to access it as an array. You can have your object extend ArrayObject and make php access your object as an array which is what you want I think.
The interfaces that gives the ability to use your object as an array:
http://www.php.net/manual/en/class.arrayaccess.php
http://www.php.net/manual/en/class.countable.php
http://www.php.net/manual/en/class.iteratoraggregate.php
The object itself which you can extend:
http://www.php.net/manual/en/class.arrayobject.php
Imo, it's never a good idea to loop over an object without array interface as it means you are using public variables. My suggestion is to always make them protected or private unless not possible otherwise (SOAP?). You can access them by getters and setters.
The $obj->property could be an array without having key 0.
The keys in your array might not be numeric. So your array might be an associative array not a regular.
If you want numeric indexing try this:
$numArray = array_values($obj->property);
var_dump($numArray);
If you want to access the first value of an array use:
$first = reset($obj->property);
It works if your variable is an array.