I am gettting two array one user and one ads I have to make another one by merging these two arrays in such a way that after every five users i will get one ad. Thanks in advance.
I like using Laravel's collections for stuff like this:
$users = range(0, 19); // users are numbers
$ads = range('a', 'd'); // ads are letters
$users = collect($users); // create a Collection from the array
$ads = collect($ads);
$result = $users->chunk(5) // break into chunks of five
->map(function($chunk) use (&$ads){
return $chunk->push($ads->shift()); // append an ad to each chunk
})->flatten() // combine all the chunks back together
->toArray(); // change the Collection back to an array
dump($result);
Gives:
array:24 [
0 => 0
1 => 1
2 => 2
3 => 3
4 => 4
5 => "a"
6 => 5
7 => 6
8 => 7
9 => 8
10 => 9
11 => "b"
12 => 10
13 => 11
14 => 12
15 => 13
16 => 14
17 => "c"
18 => 15
19 => 16
20 => 17
21 => 18
22 => 19
23 => "d"
]
I have several sorted sets stored in redis. Like:
ZADD tag:1 1 1 2 2 3 3 4 4 5 5 6 6
ZADD tag:2 21 1 22 2 23 3 24 4 25 5 26 6
ZADD tag:3 31 1 32 2 33 3 34 4 35 5 36 6
Here is my question: I want to get the data sorted by scores in tag:1 and tag:2, or tag:1 and tag:3, or tag:1,tag:2,and tag:3. That means I need to get data from different key combination([ 1 ] [ 2 ] [ 3 ] [ 1,2,3 ] [ 1,2 ] [ 2, 3 ] [ ... ] ). I have hundreds of this kind of sorted sets, with each sorted set that can be combined to any one/two/more of the others.
I kinda not choosing the ZUNIONSTORE, cause all the combination is temporary, and ZUNIONSTORE will create another new sorted set, and this set will have very low possibility for reusing. So is there any good idea to solve my problem, or any new solution to help me? Thanks in advance!
Despite your reluctance, use ZUNIONSTORE for this. Once you're done, just DEL the result. This workflow can be embedded in a Lua script that performs the actions and returns the unified result.
Hi may I know how i can perform this using matlab?
I tried nchoosek but only works on 1 type of combination. I would like to output all together in array
Let S={a,b,c,d,e}
I would like to get combinations as such which start from 3 combinations:
the 3-combinations : {a,b,c} , {a,b,d} , {a,b,e} , {a,c,d} , {a,c,e} , {a,d,e}
the 4-combinations : {a,b,c,d} , {a,b,c,e} , {a,c,d,e}
the 5-combinations : {a,b,c,d,e}
So the output would be like this:
{a,b,c} {a,b,d} {a,b,e} {a,c,d} {a,c,e} {a,d,e}{a,b,c,d} {a,b,c,e} {a,c,d,e}{a,b,c,d,e}
Thanks
You can use a loop there or arrayfun which is just a compact way to express such a loopy approach and not a vectorized approach -
combs = arrayfun(#(x) nchoosek(S,x),3:numel(S),'Uniform',0)
The output would be a cell array with each cell representing values for each combination. So, when you run the code, you would get -
>> combs{1}
ans =
2 7 4
2 7 1
2 7 9
2 4 1
2 4 9
2 1 9
7 4 1
7 4 9
7 1 9
4 1 9
which would be your 3-combinations set.
>> combs{2}
ans =
2 7 4 1
2 7 4 9
2 7 1 9
2 4 1 9
7 4 1 9
would be your 4-combinations set and so on.
I want to get the count of records from database.
In my table this is the values:
id emp_id number created_emp_id status timestamp
29 7 1 2 0 2012-09-24 15:18:28
30 16 1 2 0 2012-09-24 15:18:28
31 7 2 2 0 2012-09-24 15:18:54
32 19 2 2 0 2012-09-24 15:18:54
i have created_emp_id as 2.
So the result i needed her is 2.
That means the number is a repeated column.
This is the code i wrote for getting result:
$result = $this->TravancoDSRGroup->find('all', array('conditions' => array('created_emp_id= '.$emp_id),'fields' => array('DISTINCT TravancoDSRGroup.number')));
The $result return the two rows only.
But i need to get the count of this query....
Like...
$dsrPageCnt = $this->TravancoDSRGroup->find('count',................
How can i do this?
You can try this:
$dsrPageCnt = $this->TravancoDSRGroup->find('count', array('conditions' => array('created_emp_id= '.$emp_id),'fields' => array('DISTINCT TravancoDSRGroup.number')));
http://book.cakephp.org/1.3/view/1020/find-count
I have a multidimensional array of integers that only works inside of the function, but produces random numbers when I try to pass it to another function.
PS> $array
1
2
3
4
PS> $array[0]
1
2
PS> $array[0][1]
2
The array was originally formed with:
$array = #(($data1),($data))
and appended on $data for any array after that: ($data is cleared each iteration)
$array += ,($data)
Which seems to be fine, considering before I tried to create functions everything was working.
I then try to pass the array into a another function
theFunc ($array)
Function theFunc {
param ($theData)
#process data
}
I'm getting the correct ammount of arrays, but the numbers inside them do not match the multidimensional array that I passed in one bit.
Any help is appreciated, thank you.
I tried the following example and everything seems to be fine:
$data1 = #(1, 2, 3, 4)
$data2 = #(5, 6, 7, 8)
#($data1, $data2)
$array[0]
1
2
3
4
$array[1]
5
6
7
8
function myFunc { param($arr) $arr; $arr[0]; $arr[1]; }
myFunc($array)
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
Please provide output you get.