Searching a array in PHP, performance improvements - php

I've two A and B arrays, first one (A) is simple array where as the second one (B) is array of arrays. I want to find whether some element in A is equal to element in B. For doing this I'm currently doing nested loops which results in n^3 complexity. How can I improve upon this.
for ($i = 0; $i <= count($A); $i++) {
if (isset($A[$i])) {
foreach ($B as $items) {
foreach ($items as $item) {
if ($item['Column1'] == $A[$i]['Column1']) {
array_push(A, "result");
unset($A[$i]);
unset($items);
break;
}
}
}
}
}

array_walk_recursive($B, function($val) {
if (in_array($val, $A)) echo "$val is in the \$A array!";
});

Try this, array_seacrh() will remove your one foreach loop :
for ($i = 0; $i <= count($A); $i++) {
if (isset($A[$i])) {
foreach ($B as $items) {
$t = array_search($A[$i]['Column1'], $items);
array_push($A, "result");
unset($A[$i]);
unset($items[$t]);
}
}
}

when creating your arrays you can create a $lookup_A and $lookup_B arrays, where the keys are the values from the original $A and $B
Then you can do a simple loop on $lookup_A and check for if (isset($lookup_B[$lookup_A_key]))
Also in your example if(isset($A[$i])) is always true i think

Related

Find first duplicate in an array

Given an array a that contains only numbers in the range from 1 to a.length, find the first duplicate number for which the second occurrence has the minimal index. In other words, if there are more than 1 duplicated numbers, return the number for which the second occurrence has a smaller index than the second occurrence of the other number does. If there are no such elements, return -1.
My code:
function firstDuplicate($a) {
$unique = array_unique($a);
foreach ($a as $key => $val) {
if ($unique[$key] !== $val){
return $key;
}else{
return -1;
}
}
}
The code above will be OK when the input is [2, 4, 3, 5, 1] but if the input is [2, 1, 3, 5, 3, 2] the output is wrong. The second duplicate occurrence has a smaller index. The expected output should be 3.
How can I fix my code to output the correct result?
$arr = array(2,1,3,5,3,2);
function firstDuplicate($a) {
$res = -1;
for ($i = count($a); $i >= 1; --$i) {
for ($j = 0; $j < $i; ++$j) {
if ($a[$j] === $a[$i]) {
$res = $a[$j];
}
}
}
return $res;
}
var_dump(firstDuplicate($arr));
By traversing the array backwards, you will overwrite any previous duplicates with the new, lower-indexed one.
Note: this returns the value (not index), unless no duplicate is found. In that case, it returns -1.
// Return index of first found duplicate value in array
function firstDuplicate($a) {
$c_array = array_count_values($a);
foreach($c_array as $value=>$times)
{
if($times>1)
{
return array_search($value, $array);
}
}
return -1;
}
array_count_values() will count the duplicate values in the array for you then you just iterate over that until you find the first result with more then 1 and search for the first key in the original array matching that value.
Python3 Solution:
def firstDuplicate(a):
mySet = set()
for i in range(len(a)):
if a[i] in mySet:
return a[i]
else:
mySet.add(a[i])
return -1
function firstDuplicate($a) {
foreach($a as $index => $value) {
$detector[] = $value;
$counter = 0;
foreach($detector as $item) {
if($item == $value) {
$counter++;
if($counter >= 2) {
return $value;
break;
}
}
}
}
return -1;
}
It's easy to just get the first number that will be checked as duplicated, but unfortunately, this function exceeded 4 seconds with a large array of data, so please using it with a small scale of array data.
EDIT
I have new own code fixes execution time for large array data
function firstDuplicate($a) {
$b = [];
$counts = array_count_values($a);
foreach($counts as $num => $duplication) {
if($duplication > 1) {
$b[] = $num;
}
}
foreach($a as $value) {
if(in_array($value, $b)) {
$detector[] = $value;
$counter = 0;
foreach($detector as $item) {
if($item == $value) {
$counter++;
if($counter >= 2) {
return $value;
break;
}
}
}
}
}
return -1;
}
The new code target the current numbers having a reputation only by using array_count_values()
function firstDuplicate($a) {
$indexNumber = -1;
for($i = count($a); $i >= 1 ; --$i){
for($k = 0; $k < $i; $k++){
if(isset($a[$i]) && ($a[$i] === $a[$k]) ){
$indexNumber = $a[$k];
}
}
}
return $indexNumber;
}
Remove error from undefined index array.

Compare values from two Arrays PHP

I have two Arrays with Objects in it. I want to compare each property of the array with each other.
function compare ($array1, $array2)
{
$uniqueArray = array();
for ($i = 0; $i < count($array1); $i++)
{
for ($j = 0; $j < count($array2); $j++)
{
if(levenshtein($array1[$i]->getCompany(), $array2[$j]- >getCompany() > 0 || levenshtein($array1[$i]->getCompany(), $array2[$j]->getCompany()) < 3))
{
//add values to $unqiueArray
}
}
}
print_r($uniqueArray);
}
I'm not sure if my code is right. The iteration over the arrays and then the compare is that the right approach?
Object Properties:
private $_company;
private $_firstname;
private $_sirname;
private $_street;
private $_streetnumber;
private $_plz;
private $_place;
All prperties are strings.
You shouldn't use for (expr1; expr2; expr3) for iterating arrays; it's better to use foreach (array_expression as $value)
Also, you are comparing every element on array1 with every element on array2, but if there's a match you compare them again later.
Try something like this
foreach($array1 as $k1 => $v1) {
foreach($array2 as $k2 => $v2) {
if(your_condition()) {
$uniqueArray[] = $v1;
unset($array2[$k2])
}
}
}
Or maybe do some research on array_uintersect or array_walk
In the if, one parenthesis is mis-placed, which probably is the main problem and does unexpected results.
levenstein is not a trivial operation, you shouldn't do it two times just to compare it, it's better to store the result in a variable.
Without more details about the input and expected output, it's impossible to help you more.
Here is the code fixed.
function compare ($array1, $array2)
{
$uniqueArray = array();
for ($i = 0; $i < count($array1); $i++)
{
for ($j = 0; $j < count($array2); $j++)
{
$companyNamesLevenstein = levenshtein($array1[$i]->getCompany(), $array2[$j]->getCompany());
if($companyNamesLevenstein > 0 || $companyNamesLevenstein < 3)
{
$uniqueArray [] = $array1[$i];
}
}
}
print_r($uniqueArray);
}

Issue with custom script to sort Arrays ascending and descending order

I have an issue to deal with here (a logical error in my code 99%). I just can't seem to find the way to fix it, but I bet one of you will find the problem in no time!
I have to create a function which sorts array passed to it in asc or desc order, but can't use any array sorting functions !
I've been struggling with loops until now and I finally want to ask help from other devs ( you ).
Currently only code for ascending is worked on, descending will be no problem I assume once I do this one. It kinda of does sort values up to some point, but then stops ( it stops if the next smallest value is at the end of the passed array ). What could I do to prevent this and make it sort the whole array and it's elements?
Here is the code so far.
<?php
function order_array($array,$mode = 'ascending') {
$length = count($array);
if($mode == 'descending') {
return $array;
} else {
$sorted_array = array();
$used_indexes = array();
for($i = 0; $i < $length; $i++) {
$smallest = true;
echo $array[$i] . '<br/>';
for($y = 0; $y < $length; $y++) {
//echo $array[$i] . ' > ' . $array[$y] . '<br/>';
// if at ANY time during checking element vs other ones in his array, he is BIGGER than that element
// set smallest to false
if(!in_array($y,$used_indexes)) {
if($array[$i] > $array[$y]) {
$smallest = false;
break;
}
}
}
if($smallest) {
$sorted_array[] = $array[$i];
$used_indexes[] = $i;
}
}
return $sorted_array;
}
}
$array_to_sort = array(1, 3, 100, 99, 33, 20);
$sorted_array = order_array($array_to_sort);
print_r($sorted_array);
?>
I've solved the issue myself by doing it completely different. Now it sorts correctly all the elements of the passed in array. The logical issue I had was of using for() loop. The for() loop ran only a set ( length of passed array ) number of times, while we need it to loop more than that, because we will need to loop all the way untill we have a new sorted array in ascending order. Here is the code that will work
function order_array($array,$mode = 'ascending') {
if($mode == 'descending') {
// for() wont work here, since it will only loop an array length of times, when we would need it
// to loop more than that.
while(count($array)){
$value = MAX($array);
$key = array_search($value, $array);
if ($key !== false) {
unset($array[$key]);
}
$sorted[] = $value;
}
return $sorted;
} else {
// for() wont work here, since it will only loop an array length of times, when we would need it
// to loop more than that.
while(count($array)){
$value = MIN($array);
$key = array_search($value, $array);
if ($key !== false) {
unset($array[$key]);
}
$sorted[] = $value;
}
return $sorted;
}
}
function order_array($array,$mode = 'ascending') {
$length = count($array);
$sorted_array = array();
$used_indexes = array();
for($i = 0; $i < $length; $i++) {
$smallest = true;
echo $array[$i] . '<br/>';
for($y = 0; $y < $length; $y++) {
//echo $array[$i] . ' > ' . $array[$y] . '<br/>';
// if at ANY time during checking element vs other ones in his array, he is BIGGER than that element
// set smallest to false
if(!in_array($y,$used_indexes)) {
if($array[$i] > $array[$y]) {
$smallest = false;
break;
}
}
}
if($smallest) {
$sorted_array[] = $array[$i];
$used_indexes[] = $i;
}
if($mode == 'descending') {
return array_reverse($sorted_array);
}
return $sorted_array;
}
}

foreach output a set number of arrays only

How would I output only a set of numbers in an array, say if theres 10 arrays, I would only like to output 8 of them?
foreach($arrays as $array){
//do I use a for loop/
}
Thanks!
foreach is only the natural approach if you actually want to iterate over each item (as the name implies). However, you could do something like this:
$i = 0;
foreach($arrays as $array){
...
$i++;
if ($i == $limit) {
break;
}
}
$i = 0;
foreach($arrays as $array){
if($i < 8){
// do something
}
$i++;
}
foreach(array_slice($arrays, 0, 8) as $array){
//do I use a for loop/
}
You could use a foreach loop like this (already mentioned).
$i = 0;
foreach($arrays as $array){
//do I use a for loop/
if(++$i >= 8) break;
}
...or you can use a for loop, which is designed for doing actions a set number of times, e.g., 8 times.
for($i=0; $i < 8; $i++){
$array = $arrays[$i];
// body
}

PHP explode - running loop through each array item

Here's the issue:
I retrieve the following data string from my database:
$row->exceptions = '1,2,3';
After explode I need the below code to check each one of the exploded pieces
$exceptions = explode(",", $row->exceptions);
//result is
//[0] => 1
//[1] => 2
//[2] => 3
for ($i = 0; $i <= $row->frequency; $i++) {
if ($exceptions[] == $i) {
continue;
} else {
//do something else
}
}
How can I make $exceptions[] loop through all keys from the exploded array so it evaluates if ==$i?
Thanks for helping.
It should suffice to substitute:
if($exceptions[] == $i)
with:
if(in_array($i,$exceptions))
By the way, it eliminates the need for a nested loop.
Ah, should be straightforward, no?
$exceptions = explode(",", $row->exceptions);
for ($i = 0; $i <= $row->frequency; $i++) {
foreach($exceptions as $j){
if($j == $i){
// do something
break;
}
}
}
I think I understand what you are asking. Here's how you would test within that loop whether the key equals $i.
for ($i = 0; $i <= $row->frequency; $i++)
{
foreach ($exceptions as $key => $value)
{
if ($key == $i)
{
continue;
}
}
}

Categories