Consider some array:
$data = [
"k0"=>"v0",
"k1"=>"v1",
"k2"=>"v2",
"k3"=>"v3",
"k4"=>"v4",
];
Traverse over array $data and print array $result_1
$result_1 = [];
while (key($data)) {
$result_1[key($data)] = current($data);
next($data);
}
print_r($result_1);
//Then perform the same operation in a function supplying the same array $data as argument
//(mind that the internal pointer of $data is at the end):
traverse($data);
function traverse($arr){
$result_2 = [];
while (key($arr)) {
$result_2[key($arr)] = current($arr);
next($arr);
}
print_r($result_2);
}
If running the above code on php-5.5 $result_1 and $result_2 are the same:
//Array ( [k0] => v0 [k1] => v1 [k2] => v2 [k3] => v3 [k4] => v4 )
If running on php-7.1 $result_1 is the same as above but $result_2 is empty:
//Array ( )
Why does array traversal differ between php-5.5 and php-7.1?
I've filed a bug in PHP :: Bug #77014. The correct/intended behaviour is the one present in PHP 7+. I'm quoting the answer from nikic#php.net:
The behavior is intended. Passing an array to a function does not
change the position of the internal array pointer. I believe the PHP 5
behavior was some kind of artifact of key() accepting the array by
reference and triggering a separation there.
If you'd like to make sure that you are iterating the array from the
start rather than from the previous position of the internal array
pointer, you can perform an explicit call to reset().
(I'd recommend moving away from these functions entirely, if it is at
all possible. If complex iteration patterns are involved,
ArrayIterator may be an alternative.)
Mystery solved I guess.
Related
I have come across at this statement, and as a newby at dart, I cant figure out how it works, and how exactly does it selection from the List, please help!
var playersL=players.map<Player>((String _playerID) =>
store.state.players.players.firstWhere((Player _player) => _player?.id == _playerID)
)
Depending on what you are doing with playersL, it is not going to work as expected since the expression returns a Iterable<Player>. I guess the L means you want a List so you should put a toList() at the end:
var playersL = players
.map<Player>((String _playerID) => store.state.players.players
.firstWhere((Player _player) => _player?.id == _playerID))
.toList();
But what the code are doing is the following:
Go though players (which I guess are List<String> and convert each String into a Player object.
To do so, we check (for each String), store.state.players.players (which I guess are List<Player>, and finds the first Player object which have an id which the same as the String coming from players.
var fruits = ['apples', 'oranges', 'bananas'];
fruits[0]; // apples
fruits.add('pears');
fruits.length == 4;
fruits.where((f) => f.startsWith('a')).toList();
The example in the documentation shows the above.
I dont really understand the documentation of the method either.
https://api.dartlang.org/stable/1.21.1/dart-collection/IterableMixin/where.html
I currently see a lambda function as a parameter inside where, with where having the argument f. What is f though? Im a bit confused.
It would be great if I could see a working example. As it stands now I dont really get it. I dont know how it works or what it really does apart from that it acts as some sort of filter.
Is an anonymous function and f is the parameter it accepts
(f) => f.startsWith('a')
where(...) calls that passed function for each element in fruits and returns an iterable that only emits the values where the function returned true
where(...) is lazy, therefore the iteration and call of the passed function will only happen when the result is actually accessed, like with .toList().
DartPad example
update
"anonymous" means the function has no name in contrary to a named function like
myFilter(f) => f.startsWith('a');
main() {
fruits.where(myFilter).toList();
}
also
myFilter(f) => f.startsWith('a');
is just a shorter form of
myFilter(f) {
return f.startsWith('a');
}
I need to read data from ODataModel in loop to calculate values. So oData.Read() is not good for me as it will call Asynchronously and will call another method. I want to loop as like looping in Array and probably oDataModel.getProperty() can help me. I am executing below code in Chrome Console and getting below result.
m1 = this.getView().getModel("Model Name");
m1.getProperty("/")
Result is:
Object {SEARCH('61451144935589051'): Object, SEARCH('61451144935589052'): Object, SEARCH('61451144935589053'): Object, SEARCH('61451144935589054'): Object, SEARCH('61451144935589055'): Object…}
However if I try with below code then getting undefined as output.
m1.getProperty("/SEARCH")
It is absolutely correct that you get undefined. Obviously you have an entity type SEARCH with a single key and your model stores several entities of this entity type.
You can grab all data stored in your model and process it like in the appended code example. However this strongly not recommended as you put to much logic to the client. A better approach would be a function or even an extra entity at your OData service.
var data, i, name, names, sum;
data = m1.getProperty("/");
names = Object.getOwnPropertyNames(data);
sum = 0;
for (i = 0; i < names.length; i += 1) {
name = names[i];
// you have to check for the correct entity
if (/SEARCH/.test(name )) {
sum += data[name].value;
}
}
I am still very new to Zend and running into some issues on exporting my data to a CSV.
I found a great resource that explains the headers and download part here however I am running into issues when trying to export the actual data.
If I create a variable like $content = "test" the export works fine using the code above.
However when I duplicate my indexAction code, make some changes, and bring it into my downloadAction, I am getting issues that I believe are due to my content being returned as an Object rather than an array or string.
My Module is grabbing the SQL by using:
public function fetchAllMembers($order = null , $order_by = null, $selectwhere = null) {
$session = new SessionContainer('logggedin_user');
$sql = new Sql($this->adapter);
$select = new Select();
$select->from(array('u' => 'tbl_all_data'));
if ($selectwhere != null){
$select->where($selectwhere);
}
$select->order($order_by . ' ' . $order);
$selectString = $sql->getSqlStringForSqlObject($select);
$results = $this->adapter->query($selectString, Adapter::QUERY_MODE_EXECUTE);
$results->buffer();
return $results;
}
and my Controller is calling that SQL by using:
$content = $modulesTable->fetchAllMembers($order, $order_by, $where);
Any help would be greatly appreciated, and I don't need anyone to write the code for me just help with pointoing me in the right direction.
$this->adapter->query returns a Zend\Db\ResultSet object. So you need to call $results = $results->toArray(); to send an array.
Also you need to loop through the array and echo it out in your view file.
Results, returned by adapter are ResultSet type. I guess you need to call at least
current()
method to grab some data. And they will be of array type, so, again you need to do something with them.
toArray() is often used to quickly get data.
More sophisticated way to get data, is to use next() method with current():
$firstThing = $result->current();
$result->next();
$result->next();
$thirdThing = $result->current();
It's just an example, but it can be useful in some cases.
I'm pretty new to OpenSCAD, but have got most of it down. However, I'm not sure how I can do stack-like operations. It doesn't really matter what data structure is used, as long as I can push and pop numbers. Is this possible?
I worked extensively with stacks while implementing a string processing library.
The trick is to use nested right associative lists of size = 2, e.g. ["foo", ["bar", []]].
If you want to push something to a stack:
function push(stack, item) = [item, stack];
If you want to pop the stack:
function pop(stack) = stack[1];
And if you want to peek or retrieve the value that was popped:
function peek(stack) = stack[0];
You can also implement map/reduce functions using recursion:
function map(stack) =
push(
map(pop(stack)),
f(peek(stack))
);
function reduce(stack) =
f(peek(stack))?
push(
reduce(pop(stack)),
f(peek(stack))
)
:
reduce(pop(stack))
;
Of course, now that version 2015.03 is out you might consider using list comprehension, assuming that's what you really need.