How To get sub array keys from array by less than values - php

I want to get sub array keys from array by less than values.
This is an example:
$arr_less_than = array(55,60,10,70);
$BlackList = array(10,8,15,20);
$MasterArray = array(
10 => array(1 => array(50,20,5,40), 2 => array(70,77,58,10), 3 => array(155,95,110,105), 4 => array(250,215,248,188)),
11 => array(1 => array(5,65,49,100), 2 => array(80,85,60,30), 3 => array(175,85,95,120), 4 => array(235,205,218,284)),
12 => array(1 => array(82,80,55,80), 2 => array(90,90,74,110), 3 => array(180,122,156,222), 4 => array(255,225,233,263)),
13 => array(1 => array(350,360,400,375), 2 => array(95,99,111,75), 3 => array(188,112,66,111), 4 => array(66,69,33,110)),
);
Now I need to get sub array keys from $MasterArray by less than $arr_less_than if the sub array key is not in the array $BlackList.
For the above example, the result must return array(12,13).
Note: I don't want to use a foreach loop

There are 2 solutions here - one for all sub-arrays meet the criteria, and one for any sub-array meets the criteria (which it seems is what the OP had in mind). At the end, there are foreach based solutions for the latter case where ANY sub-array meets the criteria.
If I understand the problem correctly, the goal is to identify rows in MasterArray where all of the sub-arrays have values that are greater than the corresponding value in $arr_less_than.
The OP does not want to use foreach (See the end of the answer for foreach based answers which are much simpler) - which may actually produce a more efficient version because it can avoid unnecessary compares and save some cycles, so here is a heavily annotated version using array functions.
I've excluded the data which can be copied from OP's post:
function getMatchingRows($arr_less_than, $MasterArray, $BlackList)
{
return array_values( // When we're done we just want a straight array of the keys from $MasterArray
array_filter(
array_keys($MasterArray), // Iterate over $MasterArray's keys (Because we need the keys for the BlackList)
function ($v) use ($BlackList, $MasterArray, $arr_less_than) {
// Filter out MasterArray Entries that dont meet the criteria
echo "Evaluate $MasterArray[$v]" . PHP_EOL;
// Remove array entries whose key is in the BlackList
if (in_array($v, $BlackList)) {
echo "\tBlacklisted" . PHP_EOL;
return false;
}
// For each entry in the MasterArray value, add up the number of non-matching entries
$y = array_reduce(
$MasterArray[$v],
function ($c1, $sub) use ($arr_less_than) {
// For each subarray entry in a MasterArray value, reduce the array to a count
// of elements whose value is less than the corresponding value in the $arr_less_than
$s1 = array_reduce(
array_keys($sub),
function ($carry, $key) use ($sub, $arr_less_than) {
if ($sub[$key] <= $arr_less_than[$key]) {
return ++$carry;
}
},
0 // Initial value for the array_reduce method
);
// $s1 will be a count of non-matching values
return $c1 + $s1;
},
0 //Initial value for the array_reduce method
);
echo "\t$y" . PHP_EOL;
// Include the array value in the filter only if there are no non-matching values ($y == 0)
return !$y;
}
)
);
}
print_r(getMatchingRows($arr_less_than, $MasterArray, $BlackList));
The basic idea is to generate a list of keys from the outermost array - so we iterate over them with array_filter. Then we exclude those with a key in the blacklist. Rows that arent in the blacklist, are reduced into an integer by iterating over each sub=arrays values and comparing them positon-wise against $arr_less_than and adding 1 for each value that fails to be greater than the corresponding member in $arr_less_than. Then those values are summed for all of the members in the MasterArray row. If the result is zero, then the row passes. Finally, the ultimate result is passed to array_values to normalize the resulting array.
Note that this requires that all values be compared, even if the first sub-value in the first sub-array fails. For that reason a foreach approach that can escape may be more efficient.
This is essentially the same method without comments and couple of shortcuts:
function getMatchingRows($arr_less_than, $MasterArray, $BlackList)
{
return array_values( // When we're done we just want a straight array of the keys from $MasterArray
array_filter(
array_keys($MasterArray),
function ($v) use ($BlackList, $MasterArray, $arr_less_than) {
return !in_array($v, $BlackList) && !array_reduce(
$MasterArray[$v],
function ($c1, $sub) use ($arr_less_than) {
return $c1 ?: array_reduce(
array_keys($sub),
function ($carry, $key) use ($sub, $arr_less_than) {
return $carry ?: ($sub[$key] <= $arr_less_than[$key] ? 1 : 0);
},
0
);
},
0
);
}
)
);
}
Some of the methods in array_reduce are short-circuited using ?: operator since the actual count is irrelevant. Once the count exceeds zero, the row fails, regardless.
Here is similar code if the criterion is that AT LEAST ONE sub-array has all members greater than the reference array.
function getMatchingRowsAny($arr_less_than, $MasterArray, $BlackList)
{
return array_values( // When we're done we just want a straight array of the keys from $MasterArray
array_filter(
array_keys($MasterArray), // Iterate over $MastrArray's keys (Because we need the keys for theBlackList)
function ($v) use ($BlackList, $MasterArray, $arr_less_than) {
// Filter out MasterArray Entries that dont meet the criteria
echo "Evaluate \MasterArray[$v]" . PHP_EOL;
// Remove array entries whose key is in the BlackList
if (in_array($v, $BlackList)) {
echo "\tBlacklisted" . PHP_EOL;
return false;
}
// For each entry in the MasterArray value, add up the number of non-matching entries
$y = array_reduce(
$MasterArray[$v],
function ($c1, $sub) use ($arr_less_than) {
// For each subarray entry in a MasterArray value, reduce the array to a flag
// indicating if it has whose value is <= the corresponding value in the $arr_less_than
$s1 = array_reduce(
array_keys($sub),
function ($fail, $key) use ($sub, $arr_less_than) {
return $fail || $sub[$key] <= $arr_less_than[$key];
},
false
);
// This could be short-circuited above to avoid an unnecessary array_reduce call
return $c1 || !$s1;
},
false
);
echo "\t$y" . PHP_EOL;
// Include the array value in the filter if there are any matching values
return $y;
}
)
);
}
print_r(getMatchingRowsAny($arr_less_than, $MasterArray, $BlackList));
As an exercise (and because I'm a glutton for punishment) I rendered the same methods using foreach as both a generator and a function returning an array - mostly to illustrate that foreach may be the better choice, and is definitely simpler:
// Implemented as a generator - The associated foreach that uses it follows
function generateMatchingRows($arr_less_than, $MasterArray, $BlackList)
{
foreach ($MasterArray as $k => $v) {
if (in_array($k, $BlackList)) {
continue;
}
foreach ($v as $sub_array) {
$match = true;
foreach ($sub_array as $k1 => $v1) {
if ($v1 <= $arr_less_than[$k1]) {
$match = false;
break;
}
}
if ($match) {
yield $k;
break;
}
}
}
}
foreach (generateMatchingRows($arr_less_than, $MasterArray, $BlackList) as $k) {
echo $k . PHP_EOL; // Or push them onto an array
}
// Implemented as a function returning an array - classical approach - just return an array
function getMatchingRowsForEach($arr_less_than, $MasterArray, $BlackList)
{
$rv = [];
foreach ($MasterArray as $k => $v) {
if (in_array($k, $BlackList)) {
continue;
}
foreach ($v as $sub_array) {
$match = true;
foreach ($sub_array as $k1 => $v1) {
if ($v1 <= $arr_less_than[$k1]) {
$match = false;
break;
}
}
if ($match) {
$rv[] = $k;
break;
}
}
}
return $rv;
}
print_r(getMatchingRowsForEach($arr_less_than, $MasterArray, $BlackList));

Related

remove value from array what the words is not fully match

How i can remove a value from array what is not fully match the letters.
Array code example:
$Array = array(
'Funny',
'funnY',
'Games',
);
How I can unset all values from this array what is 'funny'
I try via unset('funny'); but is not removing the values from array, is removed just if i have 'funny' on array but 'funnY' or 'Funny' not working
Maybe there is some sophisticated solution with array_intersect_key or something which could do this in one line but I assume this approach is more easily read:
function removeCaseInsensitive($array, $toRemove) {
$ret = [];
foreach ($array as $v) {
if (strtolower($v) != strtolower($toRemove))
$ret[] = $v;
}
return $ret;
}
This returns a new array that does not contain any case of $toRemove. If you want to keep the keys than you can do this:
function removeCaseInsensitive($array, $toRemove) {
$keep = [];
foreach ($array as $k => $v) {
if (strtolower($v) != strtolower($toRemove))
$keep[$k] = true;
}
return array_intersect_keys($array, $keep);
}
You can filter out those values with a loose filtering rule:
$array = array_filter($array, function($value) {
return strtolower($value) !== 'funny';
});

How to simplify and optimize the function?

A now write this function for my script. It works well type but a little slows down. Consider the function and if you have options for optimally ask me to help.
Here is my code:
function izada($array) {
foreach ($array as $key => $value) {
if(substr_count($value, "ӣ") == 2) {
$result[] = str_replace("ӣ ", "ӣ, ", $value);
}
if(mb_substr($value, -1) !== "ӣ") {
unset($array[$key]);
}
if(substr_count($value, "ӣ") == 2) {
unset($array[$key]);
}
$array = array_filter(array_unique(array_merge($array, $result)));
}
foreach ($array as $key => $value) {
if(substr_count($value, "ӣ") > 2 || substr_count($value, "ӣ") < 1) {
unset($array[$key]);
}
}
return $array;
}
Input and function call:
$array = array (
"забони тоҷикӣ",
"хуҷандӣ бӯстонӣ",
"Тоҷикистон Ватанам",
"Ғафуровӣ Мичуринӣ Савхозӣ",
"Конверторӣ хуруфҳо"
);
$array = izada($array);
echo"<pre>";
print_r($array);
echo"</pre>";
Result must be:
Array (
[0] => забони тоҷикӣ
[1] => хуҷандӣ, бӯстонӣ
)
Jakub's answer is not optimized and is potentially incorrect according to your posted method.
It allows the possibility of a value with 2 ӣ's but not ending with ӣ to qualify. (If this is acceptable, then you should clarify your question requirements.)
It calls substr_count() 1 to 3 times per iteration (depending on conditional outcomes). The important thing to consider for efficiency is minimizing function calls.
This is a more accurate / efficient process:
Input:
$array=[
"забони тоҷикӣ",
"хуҷандӣ бӯстонӣ",
"Тоҷикистон Ватанам",
"Ғафуровӣ Мичуринӣ Савхозӣ",
"Конверторӣ хуруфҳо"
];
Method: (Demo)
foreach($array as $v){
if(mb_substr($v,-1)=="ӣ"){ // require last char to be ӣ
if(($count=substr_count($v,"ӣ"))==1){
$result[]=$v; // do not replace if only 1 ӣ
}elseif($count==2){
$result[]=str_replace("ӣ ","ӣ, ",$v); // replace qualifying ӣ's if 2 ӣ's
}
}
}
var_export($result);
Output:
array (
0 => 'забони тоҷикӣ',
1 => 'хуҷандӣ, бӯстонӣ',
)
Notice that my method first requires the final character to be ӣ, this offers the quickest return without declaring/overwriting $count for non-qualifying values.
$count is used to cache the result of substr_count() for each iteration. By doing this, the iteration only needs to make the function call once -- improving efficiency.
All the array_merge and array_unique are taking up unnecessary resources. Instead of trying to alter the original array, why not create an output array and fill it with the data you want?
There are also several redundant conditions - you are checking for the same thing several times. From what I understood, this is what you want:
Return all strings where ӣ is present once or twice, either at the end or twice anywhere. If it is present twice, add a coma.
So you could simplify it like
function izada($array) {
$ret = [];
foreach($array as $string){
if (substr_count($string, "ӣ") >= 1 && substr_count($string, "ӣ") <= 2) {
if(substr_count($string, "ӣ") == 2) {
$ret[] = str_replace("ӣ ", "ӣ, ",$string);
}
else if (mb_substr($string, -1) == "ӣ") {
$ret[] = $string;
}
}
}
return $ret;
}

PHP In_MultiArray Function

on http://php.net/manual/en/function.in-array.php - if you scroll down it gives a function to determine if a string is inside of a query in a multidimensional array. "If you found yourself in need of a multidimensional array in_array like function you can use the one below. Works in a fair amount of time"
Here's original code(working):
function in_multiarray($elem, $array)
{
$top = sizeof($array) - 1;
$bottom = 0;
while($bottom <= $top)
{
if($array[$bottom] == $elem)
return true;
else
if(is_array($array[$bottom]))
if(in_multiarray($elem, ($array[$bottom])))
return true;
$bottom++;
}
return false;
}
What I'm trying to do is instead of returning 'true' or 'false' - i'd like to return the ROW #. So my initial thought was to simply replace 'return true' with 'return $bottom; however it isn't returning the record number.
Modified Function (not working);
function in_multiarray($elem, $array)
{
$top = sizeof($array) - 1;
$bottom = 0;
while($bottom <= $top)
{
if($array[$bottom] == $elem)
return $bottom;
else
if(is_array($array[$bottom]))
if(in_multiarray($elem, ($array[$bottom])))
return $bottom;
$bottom++;
}
return false;
}
Does anyone have an idea how to modify this function to return the ROW number that contains the match?
Here's a sample of the array...
$sample = array
array ("oldpage1.php","newpage1.php"),
array ("oldpage2.php","newpage2.php"),
array ("oldpage3.php","newpage3.php"),
array ("oldpage4.php","newpage4.php"),
array ("oldpage5.php","newpage5.php")
etc.
);
$row = in_multiarray($input, $sample);
Therefore if we know the row # we can fetch the new page with a simple
$newpage=$sample[$row][1]
Thanks!
It's worth noting that a function like in_array is intended to tell you whether or not a value exists inside of an array. What you're looking for seems to be a lot closer to something like array_search, which is designed to actually provide you with the key that points to a given value in the array.
However, because you're using a multi-dimensional array what you're actually looking for is the key that points to the array that contains the value. Hence we can divide and conquer this problem with two simple steps.
Map
Filter
The first step is to map a function in_array to every element in the first array (which is just another array). This will tell us which elements of the primary array contain an array that contains the value we're searching for.
$result = array_map(function($arr) use($search) {
return in_array($search, $arr, true);
}, $arr, [$searchValue]);
The second step is to then return the keys to those arrays (i.e. filter the result).
$keys = array_keys(array_filter($result));
Now you have all the keys of any matching items. If you want to apply as just one custom filter that specifies exactly where in the subarray to look, you could also do it like this.
$search = "oldpage2.php";
$sample = [
["oldpage1.php","newpage1.php"],
["oldpage2.php","newpage2.php"],
["oldpage3.php","newpage3.php"],
["oldpage4.php","newpage4.php"],
["oldpage5.php","newpage5.php"],
];
$keys = array_keys(array_filter($sample, function($arr) use($search) {
return $arr[0] === $search;
}));
var_dump($keys);
And you get...
array(1) {
[0]=>
int(1)
}
So now you know that "oldpage2.php" is in row 1 in $sample[1][0] which means you can do this to get the results out of the array.
foreach($keys as $key) {
echo "{$sample[$key][0]} maps to {$sample[$key][1]}\n";
}
Giving you
oldpage2.php maps to newpage2.php
If you want to return only the first result you could do that as well with a function like this using similar approach.
function getFirstMatch($search, Array $arr) {
foreach($arr as $key => $value) {
if ($value[0] === $search) {
return $value[1];
}
}
}
echo getFirstMatch("oldpage4.php", $sample); // newpage4.php
The Better Alternative
Of course, the better approach is to actually use the oldpage names as the actual keys of the array rather than do this expensive search through the array, because array lookup by key in PHP is just an O(1) operation, whereas this needle/haystack approach is O(N).
So we turn your $samples array into something like this and the search no longer requires any functions...
$samples = [
"oldpage1.php" => "newpage1.php",
"oldpage2.php" => "newpage2.php",
"oldpage3.php" => "newpage3.php",
"oldpage4.php" => "newpage4.php",
"oldpage5.php" => "newpage5.php",
];
Now you can just do something like $newpage = $samples[$search] and you get exactly what you're looking for. So echo $samples["oldpage2.php"] gives you "newpage2.php" directly without the intermediary step of searching through each array.
You can use the following code to get the full path to the value:
function in_multiarray($elem, $array, &$result)
{
$top = sizeof($array) - 1;
$bottom = 0;
while($bottom <= $top)
{
if($array[$bottom] == $elem) {
array_unshift($result, $bottom);
return true;
}
else {
if(is_array($array[$bottom])) {
if(in_multiarray($elem, $array[$bottom], $result)) {
array_unshift($result, $bottom);
return true;
}
}
}
$bottom++;
}
array_shift($result);
return false;
}
$sample = array(
array ("oldpage1.php","newpage1.php"),
array ("oldpage2.php","newpage2.php"),
array ("oldpage3.php","newpage3.php"),
array ("oldpage4.php","newpage4.php"),
array ("oldpage5.php","newpage5.php")
);
$input = "newpage5.php";
$result = [];
in_multiarray($input, $sample, $result);
print_r($result);
Path is stored in $result;

search words in two arrays

I am trying to search two arrays and return the index of matching words that match in array 1 from array 2. following are the arrays:
$array1 = array('hello how are you', 'hello I am fine');
$array2 = array('hello','how');
I am trying the following code and it return 0,1 which is fine. But i only want to return 0. I want it to return only where both words are present in the array.
foreach ($array1 as $reference => $array) {
foreach($array2 as $key => $word) {
if(strpos($array, $word) !== false) {
echo $reference, PHP_EOL;
break;
}
}
}
You need to keep track of each entity from $array2 being checked against each entity from $array1 then compare after the inner loop to decide whether all elements from $array2 are present in $array1. Here's an example:
foreach($array1 as $reference => $array) {
$contains = 0;
foreach($array2 as $key => $word) {
if(strpos($array, $word) !== false) {
$contains++;
} else {
// for performance reasons, e.g. if you have a large array,
// you should break the loop here if the word isn't in the
// original array
break;
}
}
if($contains == count($array2)) {
// $array contains all words from $array2
echo $reference . PHP_EOL;
} else {
// $array doesn't contain all the words
}
}

PHP Array Search & Add?

I have an array generated by PHP
array(
array(
'name'=>'node1',
'id' => '4'
),
array(
'name'=>'node2'
'id'=>'7'
)
)
And I am trying to add an array to a specific id (so let's say id 4)
'children'=>
array(
array('name'=>'node2','id'=>'5'),
array('name'=>'node3','id'=>'6')
)
So then it would look like
array(
array(
'name'=>'node1',
'id' => '4'
'children'=>
array(
array('name'=>'node2','id'=>'5'),
array('name'=>'node3','id'=>'6')
)
),
array(
'name'=>'node2'
'id'=>'7'
)
)
but I can't seem to figure out a way to search a multidimensional array, and add a multidimensional array to that array.
Any help?
Use a foreach loop to iterate through the array (making sure to get the key too), check the value, add if needed and break when found.
foreach($array as $k=>$v) {
if( $v['id'] == 4) {
$array[$k]['children'] = array(...);
break;
}
}
foreach($a as $k => $v)
{
if(is_array($v))
{
foreach($v as $ke => $va)
{
if($ke == 'children')
{
......
}
}
}
}
It's a monstrosity but it does what you want. Using push in a recursive function fails because the reference is destroyed after the second function call, so if you don't know how deep the key is there's a choice between using an arbitrary number of loops and hoping for the best, importing variables and using eval to push new values, or pulling apart and rebuilding the array. I chose eval. The description of what you wanted is a little different from pushing a value because you're looking for a key-value pair within an array, not an array. This function finds the key value pair and adds whatever you want as a sibling, if no value is specified the value is added as sibling to the first key matched. If no pushval is specified it will return the chain of keys that points to the matched key/key-value.
$ref_array is the multi-array to be modified,
$key is the key you're looking for,
$val is the value of the key you're looking for,
$newkey is the new key that will reference the new value,
$pushval is the new value to be indexed by $newkey.
DON'T pass an argument for the $val_array parameter. It's for use with recusive calls only. It's how the function distinguishes new calls from recursive calls, and it's how the function finds the key-value without disrupting the pass-by-reference.
function deepPush(&$ref_array, $key, $val=null, $newkey=null, $pushval=null, $val_array=null)
{
static $r, $keys;
#reset static vars on first call
if(!$val_array){ $r = 0; $keys = array();}
#cap recursion
if($r > 100){ trigger_error('Stack exceeded 100'); return;}
#init val_array
$val_array = ($r) ? $val_array : $ref_array;
#specified search value???
$search_val = ($val!==null && !in_array($val, $val_array)) ? true : false;
if(!array_key_exists($key, $val_array) || $search_val) {
$i=0;foreach($val_array as $k=>$v){
if(gettype($v) == 'array') {
if($i>0){/*dead-end*/array_pop($keys); /*keep recusion accurate*/$r-=$i;}
$keys[] = $k;/*build keychain*/
$r++; $i++; /*increment recursion, iteration*/
if(deepPush($ref_array, $key, $val, $newkey, $pushval, $v)){ /*close stack on 1st success*/return $keys;}
}//if
}//foreach
}//if
else{
if($pushval === null){return $keys;}
#add $newkey to the keychain
$keys[] = $newkey;
#process $pushval based on type
$pushval = (gettype($pushval) == 'string') ? sprintf("'%s'", $pushval) : var_export($pushval, true);
#link keys together to form pointer
$p = '$ref_array';
for($j=0,$c=count($keys); $j<$c; $j++) {
$k = $keys[$j];
$p .= "['$k']";
}//for
#concat the value to be pushed
$p .= sprintf("= %s;",$pushval);
#push it
eval($p);
$keys = array();
return true;
}//else
}
deepPush($array, 'id', 4, 'children', $addThis);

Categories