Wrong result array_map function - php

this is my input array...
$var = Array ( [0] => 57;5;7;Hertha BSC - SV Werder Bremen [1] => Heim )
I change it to another array with this function
$int_array = array_map("intval", explode(";", $var[0]));
When I call this array I get this result
Array ( [0] => 57 [1] => 5 [2] => 7 [3] => 0 )
But why is Array position [3] = 0. There must be "Hertha BSC..."?

The description of the intval function, according to the PHP docs is:
Get the integer value of a variable
The integer value of Hertha BSC - SV Werder Bremen (the 4th element of the array created by exploding ; on "57;5;7;Hertha BSC - SV Werder Bremen") is not clear.
Throwing a string at it will not yield meaningful results in this context. So, I don't think it's the wrong result.
Here's something that might get you a little closer to what I think you're after, though
$var = [
'57;5;7;Hertha BSC - SV Werder Bremen',
'Heim'
];
$int_array = array_map(
function($item) {
// In cases where the exploded array item is not entirely made up of digits, return it as a string.
if (0 === preg_match('/\d+/', $item)) {
return $item;
}
// else, cast to int.
return intval($item);
},
explode(";", $var[0])
);

Related

how to get the rightmost index value from an array with the same values using php

I'm making a program to take fingerprint values using a winnowing algorithm. which is where I have to take the smallest value from an array. but not only the smallest, there are conditions where if you find the same value, the rightmost value will be taken along with the index value. example array (3,5,5,8,6,7). from this example, I want to take the index of the second number 5, which is index 2, not index 1.
but when I try to get the same value as the rightmost position use the function min () in php, but the one I get is always the leftmost position.
I expect the output of
$a = array(3,5,5,8,6,7,6);
be
[3,0], [5,2], [8,3], [6,6], [7,5]
but the actual output is
[3,0], [5,1], [8,3], [6,4], [7,5]
Just a quick alternative, if you use array_flip() which swaps the value and key over, then this will automatically overwrite any previous key value, so when you flip [1=>5, 2=>5] then the second one will overwrite the first.
So
$a = array(3,5,5,8,6,7,6);
print_r(array_flip($a));
gives...
Array
(
[3] => 0
[5] => 2
[8] => 3
[6] => 6
[7] => 5
)
Use the value from an array as an index to filter out the most right value
$values = array(3,5,5,8,6,7,6);
$result = [];
foreach ($values as $index => $value) {
$result[$value] = [$value, $index];
}
print_r(array_values($result));
Use array_unique to solve this problem
$a = array(3,5,5,8,6,7,6);
$unique = array_unique($a);
print_r($unique);
answer should be
Array
(
[0] => 3
[1] => 5
[3] => 8
[4] => 6
[5] => 7
)

How to split array into all possible combinations

How can I loop through an array, split it into two arrays and run a function for each possible combination? Order does not matter.
// Original array
$array = array('a','b','c','d','e');
// Result 1
array('a');
array('b','c','d','e');
// Result 2
array('a', 'b');
array('c','d','e');
// Result 3
array('a', 'c');
array('b','d','e');
And so on...
Here's my take at this:
<?php
$ar = ['a','b','c','d','e'];
function permuteThrough($ar, $callback, $allowMirroredResults = true, $mode = 'entry', $desiredLeftCount = null, $left = [], $right = [])
{
switch($mode)
{
case 'entry':
// Logic:
// Determine how many elements we're gonna put into left array
$len = $allowMirroredResults ? count($ar) : floor(count($ar)/2);
for($i=0; $i <= $len; $i++)
{
call_user_func(__FUNCTION__, $ar, $callback, $allowMirroredResults, 'permute',$i);
}
break;
case 'permute':
// We have nothing left to sort, let's tell our callback
if( count($ar) === 0 )
{
$callback($left,$right);
break;
}
if( count($left) < $desiredLeftCount )
{
// Note: PHP assigns arrays as clones (unlike objects)
$ar1 = $ar;
$left1 = $left;
$left1[] = current(array_splice($ar1,0,1));
call_user_func(__FUNCTION__, $ar1, $callback, $allowMirroredResults, 'permute', $desiredLeftCount, $left1, $right);
}
// This check is needed so we don't generate permutations which don't fulfill the desired left count
$originalLength = count($ar) + count($left)+count($right);
if( count($right) < $originalLength - $desiredLeftCount )
{
$ar2 = $ar;
$right1 = $right;
$right1[] = current(array_splice($ar2,0,1));
call_user_func(__FUNCTION__, $ar2, $callback, $allowMirroredResults, 'permute', $desiredLeftCount, $left, $right1);
}
break;
}
}
function printArrays($a,$b)
{
echo '['.implode(',',$a).'],['.implode(',',$b)."]\n";
}
permuteThrough($ar, 'printArrays', true); // allows mirrored results
/*
[],[a,b,c,d,e]
[a],[b,c,d,e]
[b],[a,c,d,e]
[c],[a,b,d,e]
[d],[a,b,c,e]
[e],[a,b,c,d]
[a,b],[c,d,e]
[a,c],[b,d,e]
[a,d],[b,c,e]
[a,e],[b,c,d]
[b,c],[a,d,e]
[b,d],[a,c,e]
[b,e],[a,c,d]
[c,d],[a,b,e]
[c,e],[a,b,d]
[d,e],[a,b,c]
[a,b,c],[d,e]
[a,b,d],[c,e]
[a,b,e],[c,d]
[a,c,d],[b,e]
[a,c,e],[b,d]
[a,d,e],[b,c]
[b,c,d],[a,e]
[b,c,e],[a,d]
[b,d,e],[a,c]
[c,d,e],[a,b]
[a,b,c,d],[e]
[a,b,c,e],[d]
[a,b,d,e],[c]
[a,c,d,e],[b]
[b,c,d,e],[a]
[a,b,c,d,e],[]
*/
echo "==============\n"; // output separator
permuteThrough($ar, 'printArrays', false); // doesn't allow mirrored results
/*
[],[a,b,c,d,e]
[a],[b,c,d,e]
[b],[a,c,d,e]
[c],[a,b,d,e]
[d],[a,b,c,e]
[e],[a,b,c,d]
[a,b],[c,d,e]
[a,c],[b,d,e]
[a,d],[b,c,e]
[a,e],[b,c,d]
[b,c],[a,d,e]
[b,d],[a,c,e]
[b,e],[a,c,d]
[c,d],[a,b,e]
[c,e],[a,b,d]
[d,e],[a,b,c]
*/
My permuteThrough function takes three arguments.
The array, the callback, and an optional boolean indicating whether or not you want to allow mirrored results.
The logic is pretty straight forward:
First decide how many elements we want to put into our left array.
Then Recursively call the function like so:
Our working array with the remaining elements to sort.
Shift an element off and put it into the left array. The result gets sent to another layer of recursion.
Shift an element off and put it into the right array. The result gets sent to another layer of recursion.
If there's no elements left to shift off, call our callback with the resulting left and right arrays.
Finally, make sure we're not going over the desired left array element size determined by the for loop at the beginning and make sure the right size doesn't become so big that it makes the desired left size impossible to satisfy. Normally this would be done by two separate functions. One to decide how many elements should go into the left array. And one to be used for recursion. But instead I tossed in another argument to the recursion function to eliminate the need for a separate function so it can all be handled by the same recursive function.
This is the best I can do:
class Combos
{
/**
* getPossible then getDivide
*
* #param array $input
* #return array
*/
public function getPossibleAndDivided( array $input )
{
return $this->getMultiShiftAndDivided( $this->getPossible( $input ) );
}
/**
* return all possible combinations of input
*
* #param array $input
* #return array
*/
public function getPossible( array $inputs )
{
$result = [];
if ( count( $inputs ) <= 1 ) {
$result = $inputs;
} else {
$result = array();
foreach($inputs as $input){
//make it an array
$first = [ $input ];
//get all inputs not in first
$remaining = array_diff( $inputs, $first );
//send the remaining stuff but to ourself
$combos = $this->getPossible( $remaining );//recursive
//iterate over the above results (from ourself)
foreach( $combos as $combo ) {
$last = $combo;
//convert to array if it's not
if( !is_array( $last ) ) $last = [ $last ];
//merge them
$result[] = array_merge( $first, $last );
}
}
}
return $result;
}
/**
* shift and divide a multi level array
*
* #param array $array
* #return array
*/
public function getMultiShiftAndDivided( array $mArray )
{
$divided = [];
foreach ( $mArray as $array ) {
$divided = array_merge($divided, $this->getShiftAndDivided( $array ));
}
return $divided;
}
/**
* shift and divide a single level array
*
* #param array $array
* #return array
*/
public function getShiftAndDivided( array $array )
{
$divided = [];
$array1 = [];
while( count( $array ) ){
$array1[] = array_shift( $array );
$divided[] = [ $array, $array1 ];
}
return $divided;
}
}
How it works
Top level, I don't want to get into all the details. This is basically a 2 step process or at least it was easier to figure it out that way. I built it in a class to keep everything neat. It also allows better unit testing and re-usability.
This requires 2 operations, or at least it was easier for me to do it in 2 instead of as 1. They are combined in this method
public function getPossibleAndDivided( array $input )
{
return $this->getMultiShiftAndDivided( $this->getPossible( $input ) );
}
This is the main reason I made it a class, to keep everything packaged nicely together. I would call this a wrapper method.
Step One
$Combos->getPossible(array $input)
if only one item remains, return $inputs.
this is all the combinations it can ever have (its just a single element after all).
else It iterates thought $inputs with foreach as $input
Wraps $input as an array named $first
this is a single element from $inputs
Gets the remaining elements in $inputs in a non-destructive way as $remaining
using array_diff we need both elements as arrays (see aabove)
recursive call to $this->getPossible($remaining) (see #1) and returns as $combos
Iterates over $combos foreach as $combo
$combo is assigned to $last and turned into an array, if its not
sometimes combo is an array with several elements
sometimes is a single element. It depends on the recursive call
We add to our result set the merger of $first and $last
we need both as arrays so we can merge, without causing nesting
End Anything in $result is returned.
This basically rotates though all the combinations of the array and returns them in an array like this:
Array
(
[0] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
[4] => e
)
[1] => Array
(
[0] => a
[1] => b
[2] => c
[3] => e
[4] => d
)
[2] => Array
(
[0] => a
[1] => b
[2] => d
[3] => c
[4] => e
)
...
[117] => Array
(
[0] => e
[1] => d
[2] => b
[3] => c
[4] => a
)
[118] => Array
(
[0] => e
[1] => d
[2] => c
[3] => a
[4] => b
)
[119] => Array
(
[0] => e
[1] => d
[2] => c
[3] => b
[4] => a
)
)
Yes it returns 119 results, no I will not include them all.
Step Two
Don't forget the above output is a multi-dimensional array (this is important below).
$Combos->getMultiShiftAndDivided(array $mArray)
This method is intended to be used with multi-dimensional arrays (hence its name). We get this from "Step 1". Its basically a wrapper for $Combos->getShiftAndDivided($array)
Iterates over $mArray foreach as $array
It calls $this->getShiftAndDivided($array) returns and merges with $divided.
there was no need to store the results, so I didn't waste a variable on it.
Output example:
$input = array(array('a','b','c','d','e'));
print_r($combos->getMultiShiftAndDivided($input));
Array
(
[0] => Array
(
[0] => Array
(
[0] => b
[1] => c
[2] => d
[3] => e
)
[1] => Array
(
[0] => a
)
)
....
[4] => Array
(
[0] => Array
(
)
[1] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
[4] => e
)
)
)
$Combos->getShiftAndDivided(array $array)
This method is intended to be used single level arrays.
Loops as long as the count of $array is more then 0, while loop
$array1 gets the first element from $array added and that element is removed from $array (destructively)
we store both $array and $array1 in our results $divided
this records there current "state" at that moment
when there are no more items in $array we return our results
Output example:
$input = array('a','b','c','d','e');
print_r($combos->getShiftAndDivided($input));
Array
(
[0] => Array
(
[0] => Array
(
[0] => b
[1] => c
[2] => d
[3] => e
)
[1] => Array
(
[0] => a
)
)
....
[4] => Array
(
[0] => Array
(
)
[1] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
[4] => e
)
)
)
Basically this shifts the elements of a single array to two result arrays and records their state on each shift. I made it 2 functions so that it could be tested easier and be re-used easier.
Another issue is its kind of hard to check for multi-dimensional arrays. I know how to do it, but I didn't feel like it because it's kind of ugly and there is a better way. I say that because its possible to use a one level array in what is getMultiShiftAndDivided and it wouldn't give you what you would expect. Probably you would get an error like this:
//I say probably, but I actually test it ... lol
Warning: array_shift() expects parameter 1 to be array
Which could be confusing, one could think the code is buggered. So by having the second method call with a type set into it getShiftAndDivided( array $array ). When the wrapping method tries to call this with a string its going to blow up, but in a better way:
Catchable fatal error: Argument 1 passed to Combos::getShiftAndDivided() must be of the type array, string given
Hopefully that makes sense, it's something I always try to do in cases like this. It just makes life easier in the long run. Both function return the data in the same format, which is convenient (your welcome).
Summary
So the sum of what this does, is find all the combinations of our input, then it takes those and breaks each one into shifted and divided up array. There for it stands to reason that we will have all the possible combinations divided up into 2 arrays anyway possible. Because that is pretty much exactly what I said.
Now I am not 100% it does that, you can check them if you want, it returns like 599 elements at the end. So good luck on that, I would suggest testing just the results of $combos->getPossible($input). If that has all the combinations like it should, then this will have all that it needs. I am not sure if it returns duplicates, I don't think that was specified. But I didn't really check it.
You can call the main method like this:
$input = array('a','b','c','d','e');
print_r((new Combos)->getPossibleAndDivided($input));
Test It!
P.S. I spell breaks as brakes, but I can write code this, go figure...

delete all the duplicate element with array_unique PHP

I have this following array :
Array
(
[1] => Array
(
[0] => 2013-07
[1] => 4
[2] => 4
[3] => 3060
[4] => 1
)
[2] => Array
(
[0] => 2013-07
[1] => 270
[2] => 757
[3] => 13812810
[4] => 4
)
And i want to delete all the duplicated elements and replace $month[1][4] with it sum:
$result = array_reduce($month, function($cur, $x)
{
return $cur + $x[4];
}, 0);
$month = array_unique($month); //<---- Exception
$month[1][4]=$result;
It works fine but it displays me this exception Notice: Array to string conversion !
How can I prevent this from happening?
Thanks
Quick Answer: use:
$unique = array_unique($a, SORT_REGULAR);
// OR
$unique = array_map('unserialize', array_unique(array_map('serialize', $a)));
Explanation
The problem comes about because you're using a multi-dimensional array, array_unique() uses using string conversion before comparing the values to find the unique values:
Note: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same. The first element will be used.
An array will always convert to "Array" when casting it to a string:
var_dump("Array" === (string) array());
You can solve this by specifying the SORT_REGULAR mode in the second parameter of array_unique:
$unique = array_unique($a, SORT_REGULAR);
Or, if that doesn’t work, by serializing the arrays before and unserializing it after calling array_unique to find the unique values:
$unique = array_map('unserialize', array_unique(array_map('serialize', $a)));
This is what you need:
$month= array_unique($month, SORT_REGULAR);

PHP - Array size returns 1?

Under Return Values for Count()
Returns the number of elements in var. If var is not an array or an object with implemented Countable interface, 1 will be returned. There is one exception, if var is NULL, 0 will be returned.
I have a string which is filled with letters and numbers and I'm using preg_match_all() to extract those numbers. As I recall preg_match_all fills the contents of the array given in the 3rd parameter with the results. Why does it return 1?
What am I doing wrong in my code?
$string = "9hsfgh563452";
preg_match_all("/[0-9]/",$string,$matches);
echo "Array size: " . count($matches)."</br>"; //Returns 1
echo "Array size: " . sizeof($matches)."</br>"; //Returns 1
print_r($matches);
I would like to sum the contents of the array (which is all the numbers returned in the string) array_sum() didn't work ; it is a string array and I don't know how to convert it to an int array because I'm not using any delimiters like ' , ' and such. Is there a more efficient way in doing this?
Help appreciated.
The count is 1 because $matches is an array which contains another array inside. Specifically, $matches[0] is an array that contains each match of the zero-th capturing group (the whole regular expression).
That is, $matches looks like this:
Array
(
[0] => Array // The key "0" means that matches for the whole regex follow
(
[0] => 9 // and here are all the single-character matches
[1] => 5
[2] => 6
[3] => 3
[4] => 4
[5] => 5
[6] => 2
)
)
The result of preg_match_all is actually an array of an array:
Array
(
[0] => Array
(
[0] => 9
[1] => 5
[2] => 6
[3] => 3
[4] => 4
[5] => 5
[6] => 2
)
)
So you'll need to do something like:
echo "Array size: " . count($matches[0]);
echo "Array sum: " . array_sum($matches[0]);
This is due to the way preg_match_all returns results. Its main array elements are the preg brackets (expression matches), whilst the contents of them are what you matched.
In your case, you have no sub-expressions. The array will therefore only have one element - and that element will contain all your digits.
To sum them up, simply do this:
$sum = 0;
$j = count($matches[0]);
for ($i = 0; i < $j; ++$i) {
$sum += (int)$matches[0][$i];
}
Try using $matches[0] instead of $matches (returns 7).
Then if you want to sum all the numbers you can use the foreach function

The PHP function array_unique doesn't work

I have an array in PHP and I want to remove duplicates.
I simply turned to the function array_unique() to create a new array and remove the duplicates.
Here's the code:
$unlink = array();
$unlink = array_unique($linkphoto);
foreach ($unlink as $link) {
echo $link, "<br />";
}
Still it shows duplicates! Any suggestions about what's wrong?
According to the documentation, the condition for equality is as follows:
Note: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same. The first element will be used.
What sort of data are you using? If two items aren't string equal, then they'll both remain in the array.
We need more context in order to be able to help you, like what the contents are of $linkphoto before array_unique is applied to it. For example:
<?php
$array = Array('A','B','C','D','B');
print_r($array); // Array ( [0] => A [1] => B [2] => C [3] => D [4] => B )
$result = array_unique($array);
print_r($result); // Array ( [0] => A [1] => B [2] => C [3] => D )
?>
As #nobody_ mentioned, it will only eliminate duplicates if their string representations are the same.

Categories