PHP combinations of array elements - php

I want to generate all possible combination of array elements to fill a placeholder, the placeholder size could vary.
Let say I have array $a = array(3, 2, 9, 7) and placeholder size is 6. I want to generate something like the following:
3,3,3,3,3,3
2,3,3,3,3,3
2,2,3,3,3,3
...........
...........
7,7,7,7,7,9
7,7,7,7,7,7
However (2,3,3,3,3,3) would be considered the same as (3,2,3,3,3,3) so the later one doesn't count.
Could anyone point me to the right direction? I know there is Math_Combinatorics pear package, but that one is only applicable to placeholder size <= count($a).
Edit
I am thinking that this one is similar to bits string combination though with different number base

I have no PHP source code for you but some sources that might help.
Some C code. Look at 2.1:
http://www.aconnect.de/friends/editions/computer/combinatoricode_g.html
Delphi code: combination without repetition of N elements without use for..to..do
Wiki article here

Well it took quit some time to figure this one out.
So i split the question into multiple parts
1.
I firsrt made an array with all the possible value options.
function create_all_array($placeholder, array $values)
{
if ($placeholder <= 0) {
return [];
}
$stack = [];
$values = array_unique($values);
foreach ($values as $value) {
$stack[] = [
'first' => $value,
'childs' => create_all_array($placeholder - 1, $values)
];
}
return $stack;
}
2.
Then I made a function to stransform this massive amount of data into string (no check for uniques).
function string($values, $prefix = '')
{
$stack = [];
foreach($values as $value) {
$sub_prefix = $prefix . $value['first'];
if (empty($value['childs'])) {
$stack[$sub_prefix] = (int)$sub_prefix;
} else {
$stack = array_merge($stack, string($value['childs'], $sub_prefix));
}
}
return $stack;
}
3.
Then the hard part came. Check for duplicates. This was harder than expected, but found some good anser to it and refactored it for my use.
function has_duplicate($string, $items)
{
$explode = str_split ($string);
foreach($items as $item) {
$item_explode = str_split($item);
sort($explode);
$string = implode('',$explode);
sort($item_explode);
$item = implode($item_explode);
if ($string == $item) {
return true;
}
}
return false;
}
4.
The last step was to combine the intel into a new funciton :P
function unique_string($placeholder, array $values)
{
$stack = string(create_all_array($placeholder, $values));
$check_stack = [];
foreach($stack as $key => $item) {
if (has_duplicate($item, $check_stack)) {
unset($stack[$key]);
}
$check_stack[] = $item;
}
return $stack;
}
Now you can use it simple as followed
unique_string(3 /* amount of dept */, [1,2,3] /* keys */);
Ps the code is based for PHP5.4+, to convert to lower you need to change the [] to array() but I love the new syntax so sorry :P

Related

How to perform this array filtering without using any function in PHP? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Let's say I have
$a = [1,4,5,8,9,...];
well containing a large range of discontinuing numbers, and
$p = [
1 => [...],
3 => [...],
8 => [...],
10 => [...],
...
];
containing arrays which indexes are discontinued as well.
I need to remove every number in $a that has a corresponding index in $p ...wait... without using any function.
Is it possible ? If yes, how ? If no, what is the most optimized way (wallclock-wise) to resolve that ?
Sure, it's possible:
$filtered_array = [];
foreach ($a as $index => $value) {
foreach ($p as $key => $sub_array) {
if ($key == $value) {
// this $value of $a corresponds with an existing index in $p, so
// do NOT add it to our $filtered_array but move on to the next
// value of $a
continue 2;
}
}
$filtered_array[$index] = $value;
}
Of course this will be very slow since for every entry in $a that is not in $p you will need to iterate over the entire $p array to find out that it isn't in there. A more efficient solution would be to leverage PHP's OutOfBoundsException that is thrown when you try to access an array index that doesn't exist:
$filtered_array = [];
foreach ($a as $index => $value) {
try {
$p[$value];
} except (OutOfBoundsException $e) {
// $value does not exist as an index of $p
$filtered_array[$index] = $value;
}
}
Performance might be improved somewhat if you don't need to preserve the array keys (you won't need $index in that case, which saves a memory assignment for every item in $a), but I think that difference will be negligible.
Using some array functions will be more efficient:
$filtered_array = [];
foreach ($a as $index => $value) {
if (!array_key_exists($value, $p)) {
$filtered_array[$index] = $value;
}
}
Since you won't need a try/catch mechanism that passes through PHP's error handling, this will be faster.
It would probably be even faster if you allow yourself to use unset() to remove values that are in $p, so you don't need to create a new array but can instead modify $a in place. From your code example and the short array syntax, I'm assuming you use PHP 7. Since PHP 7 no longer uses the internal array pointer for foreach, you can safely unset() items while iterating. If you're running on PHP 5, you can still do that but you run the risk of foreach skipping over some items.
foreach ($a as $index => $value) {
if (array_key_exists($value, $p)) {
unset($a[$index]);
}
}
Doing this will reduce the memory overhead of having a second (potentially large) array hanging around.
But since now you're already breaking your own "no functions" rule anyway, you might as well go all the way and use array_filter (although this does not modify the existing array but instead builds a new array, which will degrade performance for very large arrays):
$filtered_array = array_filter($a, function($value) use ($p) {
return !array_key_exists($value, $p);
});
So I answer my own question, people too busy to downvote and childishly comment saying "it's not possible", "is it homework". No it is not homework, I was just hoping some little help here (you know, as in community).
What I tried to achieve, remove every number in an array that has an index in another array (both having discontinued values) without using any function :
// temporary $a
$a_temp = [];
foreach ($a as $avalue) {
foreach ($p as $k => $v) {
// if iterator $a is in the $p (we disregard)
if ($avalue === $k) {
continue 2;
}
}
$a_temp[] = $avalue;
}
$a = $a_temp;
(if you see a function above, please send me an email)
Here some testing .
/********************************
* initialize fake data
********************************/
$p = [];
for ($i = 0; $i < 10; ++$i) {
$p[rand(0, 500)] = null;
}
for ($j = 0; $j < 1000; ++$j) {
$a[rand(0, 500)] = null;
}
$a = array_keys($a);
/********************************
* first test (without functions)
********************************/
$start = microtime(true);
for ($i = 0; $i < 9999; ++$i) {
$a_temp = [];
foreach ($a as $avalue) {
foreach ($p as $k => $v) {
if ($avalue === $k) {
continue 2;
}
}
$a_temp[] = $avalue;
}
}
$firstTestExecTime = (microtime(true) - $start);
// average time : 8s6757750511169
/********************************
* second test, with functions
********************************/
$start = microtime(true);
for ($i = 0; $i < 9999; ++$i) {
$a_temp = [];
foreach ($a as $avalue) {
if (!array_key_exists($avalue, $p)) {
$a_temp[] = $avalue;
}
}
}
$secondTestExecTime = (microtime(true) - $start);
// average time : 5.1003220081329
/********************************
* printing results
********************************/
printf('first test execution time : %s', $firstTestExecTime);
printf('second test execution time : %s', $secondTestExecTime);
Sometimes using functions with a big set of data is not recommended, but in that case, it seems like array_search has better performance than trying to make a native code.

PHP - dynamically add dimensions to array

I have a :
$value = "val";
I also have an array :
$keys = ['key1', 'key2', 'key3'...]
The keys in that array are dynamically generated, and can go from 2 to 10 or more entries.
My goal is getting this :
$array['key1']['key2']['key3']... = $value;
How can I do that ?
Thanks
The easiest, and least messy way (ie not using references) would be to use a recursive function:
function addArrayLevels(array $keys, array $target)
{
if ($keys) {
$key = array_shift($keys);
$target[$key] = addArrayLevels($keys, []);
}
return $target;
}
//usage
$keys = range(1, 10);
$subArrays = addARrayLevels($keys, []);
It works as you can see here.
How it works is really quite simple:
if ($keys) {: if there's a key left to be added
$key = array_shift($keys); shift the first element from the array
$target[$key] = addArrayLevels($keys, []);: add the index to $target, and call the same function again with $keys (after having removed the first value). These recursive calls will go on until $keys is empty, and the function simply returns an empty array
The downsides:
Recursion can be tricky to get your head round at first, especially in complex functions, in code you didn't write, but document it well and you should be fine
The pro's:
It's more flexible (you can use $target as a sort of default/final assignment variable, with little effort (will add example below if I find the time)
No reference mess and risks to deal with
Example using adaptation of the function above to assign value at "lowest" level:
function addArrayLevels(array $keys, $value)
{
$return = [];
$key = array_shift($keys);
if ($keys) {
$return[$key] = addArrayLevels($keys, $value);
} else {
$return[$key] = $value;
}
return $return;
}
$keys = range(1, 10);
$subArrays = addARrayLevels($keys, 'innerValue');
var_dump($subArrays);
Demo
I don't think that there is built-in function for that but you can do that with simple foreach and references.
$newArray = [];
$keys = ['key1', 'key2', 'key3'];
$reference =& $newArray;
foreach ($keys as $key) {
$reference[$key] = [];
$reference =& $reference[$key];
}
unset($reference);
var_dump($newArray);

array iterations too many foreach? any ideas on reducing it

protected static function arrayShuffling($itemsArray)
{
$itemSwitching = array();Switching
$shItem= array();
foreach ($itemArray as $i => $myItem) {
if (!in_array($myItem->_id, $itemSwitching)) {
$itemSwitching[$i]['_id'] = $myItem->_id;
$itemSwitching[$i]['poistion'] = $myItem['details']['move_to_poistion'];
}
foreach ($itemSwitching as $t => $pinPrep) {
if ($event->_id == $pinPrep['_id']) {
$shuffeledItem[$itemSwitching[$t]['poistion']] = $myItem;
unset($itemArray[$i]);
}
}
}
foreach($shItem as $key=>$shffuledItem){
array_splice( $itemArray, $key, 0, array($shffuledItem));
}
}
I have this method that takes an array and then look at $myItem['details']['move_to_poistion'] index to see what position that element should be moved to. After that it takes that specific element out of the array and then uses splice to insert it back to the $myItem['details']['move_to_poistion'] position.
I am worried about too many foreach lops and want to know if some how we can shorten this.
thanks

Most efficient way to search for object in an array by a specific property's value

What would be the fastest, most efficient way to implement a search method that will return an object with a qualifying id?
Sample object array:
$array = [
(object) ['id' => 'one', 'color' => 'white'],
(object) ['id' => 'two', 'color' => 'red'],
(object) ['id' => 'three', 'color' => 'blue']
];
What do I write inside of:
function findObjectById($id){
}
The desired result would return the object at $array[0] if I called:
$obj = findObjectById('one')
Otherwise, it would return false if I passed 'four' as the parameter.
You can iterate that objects:
function findObjectById($id){
$array = array( /* your array of objects */ );
foreach ( $array as $element ) {
if ( $id == $element->id ) {
return $element;
}
}
return false;
}
Edit:
Faster way is to have an array with keys equals to objects' ids (if unique);
Then you can build your function as follow:
function findObjectById($id){
$array = array( /* your array of objects with ids as keys */ );
if ( isset( $array[$id] ) ) {
return $array[$id];
}
return false;
}
It's an old question but for the canonical reference as it was missing in the pure form:
$obj = array_column($array, null, 'id')['one'] ?? false;
The false is per the questions requirement to return false. It represents the non-matching value, e.g. you can make it null for example as an alternative suggestion.
This works transparently since PHP 7.0. In case you (still) have an older version, there are user-space implementations of it that can be used as a drop-in replacement.
However array_column also means to copy a whole array. This might not be wanted.
Instead it could be used to index the array and then map over with array_flip:
$index = array_column($array, 'id');
$map = array_flip($index);
$obj = $array[$map['one'] ?? null] ?? false;
On the index the search problem might still be the same, the map just offers the index in the original array so there is a reference system.
Keep in mind thought that this might not be necessary as PHP has copy-on-write. So there might be less duplication as intentionally thought. So this is to show some options.
Another option is to go through the whole array and unless the object is already found, check for a match. One way to do this is with array_reduce:
$obj = array_reduce($array, static function ($carry, $item) {
return $carry === false && $item->id === 'one' ? $item : $carry;
}, false);
This variant again is with the returning false requirement for no-match.
It is a bit more straight forward with null:
$obj = array_reduce($array, static function ($carry, $item) {
return $carry ?? ($item->id === 'one' ? $item : $carry);
}, null);
And a different no-match requirement can then be added with $obj = ...) ?? false; for example.
Fully exposing to foreach within a function of its own even has the benefit to directly exit on match:
$result = null;
foreach ($array as $object) {
if ($object->id === 'one') {
$result = $object;
break;
}
}
unset($object);
$obj = $result ?? false;
This is effectively the original answer by hsz, which shows how universally it can be applied.
You can use the function array_search of php like this
$key=array_search("one", array_column(json_decode(json_encode($array),TRUE), 'color'));
var_dump($array[$key]);
i: is the index of item in array
1: is the property value looking for
$arr: Array looking inside
'ID': the property key
$i = array_search(1, array_column($arr, 'ID'));
$element = ($i !== false ? $arr[$i] : null);
Well, you would would have to loop through them and check compare the ID's unless your array is sorted (by ID) in which case you can implement a searching algorithm like binary search or something of that sort to make it quicker.
My suggestion would be to first sort the arrays using a sorting algorithm (binary sort, insertion sort or quick sort) if the array is not sorted already. Then you can implement a search algorithm which should improve performance and I think that's as good as it gets.
http://www.algolist.net/Algorithms/Binary_search
This is my absolute favorite algorithm for very quickly finding what I need in a very large array, quickly. It is a Binary Search Algorithm implementation I created and use extensively in my PHP code. It hands-down beats straight-forward iterative search routines. You can vary it a multitude of ways to fit your need, but the basic algorithm remains the same.
To use it (this variation), the array must be sorted, by the index you want to find, in lowest-to-highest order.
function quick_find(&$array, $property, $value_to_find, &$first_index) {
$l = 0;
$r = count($array) - 1;
$m = 0;
while ($l <= $r) {
$m = floor(($l + $r) / 2);
if ($array[$m]->{$property} < $value_to_find) {
$l = $m + 1;
} else if ($array[$m]->{$property} > $value_to_find) {
$r = $m - 1;
} else {
$first_index = $m;
return $array[$m];
}
}
return FALSE;
}
And to test it out:
/* Define a class to put into our array of objects */
class test_object {
public $index;
public $whatever_you_want;
public function __construct( $index_to_assign ) {
$this->index = $index_to_assign;
$this->whatever_you_want = rand(1, 10000000);
}
}
/* Initialize an empty array we will fill with our objects */
$my_array = array();
/* Get a random starting index to simulate data (possibly loaded from a database) */
$my_index = rand(1256, 30000);
/* Say we are needing to locate the record with this index */
$index_to_locate = $my_index + rand(200, 30234);
/*
* Fill "$my_array()" with ONE MILLION objects of type "test_object"
*
* 1,000,000 objects may take a little bit to generate. If you don't
* feel patient, you may lower the number!
*
*/
for ($i = 0; $i < 1000000; $i++) {
$searchable_object = new test_object($my_index); // Create the object
array_push($my_array, $searchable_object); // Add it to the "$my_array" array
$my_index++; /* Increment our unique index */
}
echo "Searching array of ".count($my_array)." objects for index: " . $index_to_locate ."\n\n";
$index_found = -1; // Variable into which the array-index at which our object was found will be placed upon return of the function.
$object = quick_find($my_array, "index", $index_to_locate, $index_found);
if ($object == NULL) {
echo "Index $index_to_locate was not contained in the array.\n";
} else {
echo "Object found at index $index_found!\n";
print_r($object);
}
echo "\n\n";
Now, a few notes:
You MAY use this to find non-unique indexes; the array MUST still be sorted in ascending order. Then, when it finds an element matching your criteria, you must walk the array backwards to find the first element, or forward to find the last. It will add a few "hops" to your search, but it will still most likely be faster than iterating a large array.
For STRING indexes, you can change the arithmetic comparisons (i.e. " > " and " < " ) in quick_find() to PHP's function "strcasecmp()". Just make sure the STRING indexes are sorted the same way (for the example implementation): Alphabetically and Ascending.
And if you want to have a version that can search arrays of objects sorted in EITHER ascending OR decending order:
function quick_find_a(&$array, $property, $value_to_find, &$first_index) {
$l = 0;
$r = count($array) - 1;
$m = 0;
while ($l <= $r) {
$m = floor(($l + $r) / 2);
if ($array[$m]->{$property} < $value_to_find) {
$l = $m + 1;
} else if ($array[$m]->{$property} > $value_to_find) {
$r = $m - 1;
} else {
$first_index = $m;
return $array[$m];
}
}
return FALSE;
}
function quick_find_d(&$array, $property, $value_to_find, &$first_index) {
$l = 0;
$r = count($array) - 1;
$m = 0;
while ($l <= $r) {
$m = floor(($l + $r) / 2);
if ($value_to_find > $array[$m]->{$property}) {
$r = $m - 1;
} else if ($value_to_find < $array[$m]->{$property}) {
$l = $m + 1;
} else {
$first_index = $m;
return $array[$m];
}
}
return FALSE;
}
function quick_find(&$array, $property, $value_to_find, &$first_index) {
if ($array[0]->{$property} < $array[count($array)-1]->{$property}) {
return quick_find_a($array, $property, $value_to_find, $first_index);
} else {
return quick_find_d($array, $property, $value_to_find, $first_index);
}
}
The thing with performance of data structures is not only how to get but mostly how to store my data.
If you are free to design your array, use an associative array:
$array['one']->id = 'one';
$array['one']->color = 'white';
$array['two']->id = 'two';
$array['two']->color = 'red';
$array['three']->id = 'three';
$array['three']->color = 'blue';
Finding is then the most cheap: $one = $array['one];
UPDATE:
If you cannot modify your array constitution, you could create a separate array which maps ids to indexes. Finding an object this way does not cost any time:
$map['one'] = 0;
$map['two'] = 1;
$map['three'] = 2;
...
getObjectById() then first lookups the index of the id within the original array and secondly returns the right object:
$index = $map[$id];
return $array[$index];
Something I like to do in these situations is to create a referential array, thus avoiding having to re-copy the object but having the power to use the reference to it like the object itself.
$array['one']->id = 'one';
$array['one']->color = 'white';
$array['two']->id = 'two';
$array['two']->color = 'red';
$array['three']->id = 'three';
$array['three']->color = 'blue';
Then we can create a simple referential array:
$ref = array();
foreach ( $array as $row )
$ref[$row->id] = &$array[$row->id];
Now we can simply test if an instance exists in the array and even use it like the original object if we wanted:
if ( isset( $ref['one'] ) )
echo $ref['one']->color;
would output:
white
If the id in question did not exist, the isset() would return false, so there's no need to iterate the original object over and over looking for a value...we just use PHP's isset() function and avoid using a separate function altogether.
Please note when using references that you want use the "&" with the original array and not the iterator, so using &$row would not give you what you want.
This is definitely not efficient, O(N). But it looks sexy:
$result = array_reduce($array, function ($found, $obj) use ($id) {
return $obj['id'] == $id ? $obj : $found;
}, null);
addendum:
I see hakre already posted something akin to this.
Here is what I use. Reusable functions that loop through an array of objects. The second one allows you to retrieve a single object directly out of all matches (the first one to match criteria).
function get_objects_where($match, $objects) {
if ($match == '' || !is_array($match)) return array ();
$wanted_objects = array ();
foreach ($objects as $object) {
$wanted = false;
foreach ($match as $k => $v) {
if (is_object($object) && isset($object->$k) && $object->$k == $v) {
$wanted = true;
} else {
$wanted = false;
break;
};
};
if ($wanted) $wanted_objects[] = $object;
};
return $wanted_objects;
};
function get_object_where($match, $objects) {
if ($match == '' || !is_array($match)) return (object) array ();
$wanted_objects = get_objects_where($match, $objects);
return count($wanted_objects) > 0 ? $wanted_objects[0] : (object) array ();
};
The easiest way:
function objectToArray($obj) {
return json_decode(json_encode($obj), true);
}

Array manipulation in PHP

I am using PHP. I need to implement the following logic:
My $List has the following data:
array('id' =>'1','flag'=>'1','place1' =>'val2' ,'place2'=>'val3')
array('id'=> '1','flag'=>'0','place1' =>'val3' ,'place2'=>'val7')
array('id'=> '1','flag'=>'0','place1' =>'val4' ,'place2'=>'val8')
array('id'=> '1','flag'=>'0','place1' =>'val5' ,'place2'=>'val9')
array('id'=> '2','flag'=>'1','place1' =>'val10','place2'=>'val11')
array('id'=> '3','flag'=>'1','place1' =>'val11','place2'=>'val14')
My logic:
if (flag == 0){
Store the values of place1 ,Place2
in the Index of id
/*example
$store[1]= {place1}+{place2}
hence output:
store[1]= {val3+val4+val5}+{val7+val8+val9}
similarly
store[2]= empty
store[3]= empty
*/
}
else{
Print the Values of place1,place2 of its index
/* example
print place1[1]="val2" ,place2[1]='val3'
place1[2]="val10",place2[2]='val11'
place1[3]="val11",place2[3]='val14'
*/
}
My partial snippet has:
foreach ($List as $key => $val)
{
if($val['flag']!==0){
//how to implementation
}
else{
echo '$val['place1']'.'$val['place2']';
}
}
What is the best way to proceed?
Assumed that you actually have numeric values in place1 and place2 and thus commutative addition is allowed. If you really have text and need the braces, then I would join the two parts with another pass over store later (but then I would not understand the {0}+{0}).
I tried to keep your quotation marks in the else part. Not tested at all.
$store = array();
foreach ($List as $key => $val)
{
$id = $val['id'];
if (!array_key_exists($id, $store)) {
$store[$id] = 0;
}
if ($val['flag'] == 0) {
$store[$id] += $val['place1'];
$store[$id] += $val['place2'];
} else {
printf("place1[%d]=\"%s\",place2[%d]='%s'\n", $id, $val['place1'], $id, $val['place2']);
}
}
There is a nice function for filtering data out of an array: array___filter. The function requires a callback function, a function that will make the decision whether an element should stay in the array. We create a filter function using create_function and extract the interesting part of the array.
$reducingFunction = create_function('$value', 'return $value["flag"] == 0;');
$reduced = array_filter($list, $reducingFunction);
The next neat function (array_walk) is one that transforms the elements of our array (i.e. $value = $value['param1']), so we create another callback function:
$extractingFunction = create_function('&$value, $key, $index', '$value = $value[$index];');
$valuesParam1 = $reduced;
$valuesParam2 = $reduced;
array_walk($valuesParam1, $extractingFunction, 'param1');
array_walk($valuesParam2, $extractingFunction, 'param1');
Now we have two arrays containing all the data we need:
$store = array(1 => implode('', $valuesParam1).implode('', $valuesParam2));
I don't seem to understand what you want the array $store to contain else. Or what the else-branch should do, so this would require further explanation.
The code newbie friendly :
$store = array(); // Don't forget to initialize your vars
foreach ($List as $key => $val)
{
if ($val['flag']==0)
{
// we check if $store[$val['id']] contains any value first and set it to zero
if (isset($store[$val['id']]))
{
// we add the previous value with the new one
$store[$val['id'] = $store[$val['id']] + $val['place1'] + $val['place2'];
}
else
{
// first time seeing this id, we set the value to 0
$store[$val['id']] = 0;
}
}
else
{
echo '$val['place1']'.'$val['place2']'."\n"; // adding line break (use </ br> if in a web browser)
}
}
I didn't get if "flag" should be 0 or not, so tweak it on.
Version 2 :
If "flag" is just used for boolean logic, you'd may just replace the above code with :
if (!$val['flag'])
Then you can use PHP shortcuts to sum up :
$store[$val['id'] += $val['place1'] + $val['place2'];
Bracelets are not required for one line operations :
if (isset($store[$val['id']]))
$store[$val['id'] = $store[$val['id']] + $val['place1'] + $val['place2'];
else
$store[$val['id']] = 0;
And you don't need the "$key" var either.
So the code becomes :
$store = array();
foreach ($List as $val)
if (!$val['flag'])
if (isset($store[$val['id']]))
$store[$val['id'] += $val['place1'] + $val['place2'];
else
$store[$val['id']] = 0;
else
echo '$val['place1']'.'$val['place2']'."\n";
Version 3 :
if / else/ set operations can be shorten using the ternary operator (beware it's less readable and slower):
$store[$val['id'] = isset($store[$val['id']]) ? 0 : $store[$val['id']] + $val['place1'] + $val['place2'];
So the code becomes :
$store = array();
foreach ($List as $val)
if (!$val['flag'])
$store[$val['id'] = isset($store[$val['id']]) ? 0 : $store[$val['id']] + $val['place1'] + $val['place2'];
else
echo '$val['place1']'.'$val['place2']'."\n";
Pro version
Once you get this one, check soulmerge's code. It is really smarter to use fonctional programming when you can since it's smaller, and result can be piped to each others, so the code is more reusable. What's more, using generators consume often less memory.

Categories