-2
Hello, I have the following PHP script:
Code:
<?php
$lista1 = ["CURITIBA:SO","SP","BH","RS"];
$lista2 = ["RJ","SC","AM","CURITIBA"];
function multiexplode ($delimiters,$string) {
$ready = str_replace($delimiters, $delimiters[0], $string);
$launch = explode($delimiters[0], $ready);
return $launch;
}
for($j=0; $j <= 3; $j++){
$lista3 = multiexplode(array(":H", ":SO"), $lista1[$j]);
}
$matches = array_intersect($lista3, $lista2);
for($i =0; $i <= 3; $i++){
if(array_key_exists($i, $matches)){
echo "O valor encontrado igual foi: $matches[$i]"."</br>";
}else{
echo "Não existe valor igual neste elemento da lista"."</br>";
}
}
?>
Output:
There is no equal value in this list widget There is no equal value in this list widget There is no equal value in this list widget There is no equal value in this list widget
Above is the output, but if I change the variable $lista1[$j]
that is inside a being that traverses all elements of the same, it returns the desired value that is the word CURITIBA without :ONLY.
Altering:
for($j=0; $j <= 3; $j++){
$lista3 = multiexplode(array(":H", ":SO"), $lista1[0]);
}
In this way it returns me the desired value, however I passed the index manually, I would like it to go through all the indexes, because my real list has about 14 thousand elements each one. How to do this in the above script?
Goal:
There are two lists with elements that contain names, some have an abbreviation contains an acronym of the same next to the name, when performing the match between these two lists should be returned only the value of the name that exists between the two lists, I used a multiexplode function to remove all the values I need and after an implode to receive all values in my third list and check the arrays after. If someone can make this script smarter, the comment will be very welcome!
Since it is already the third question related to this, it seems that what you are doing is an XY problem. One of the problems with your code is attributing
$lista3
within the loop and with it will always change the value. In this way, thearray_interseft
you will only look at the last item on list 1. This may not be the only problem, as it seems that you are increasingly complicating the solution of something that may be simple, so I suggest that instead of asking what is wrong with your code, you provide a detailed description of your problem.– Woss
I got it, I found a solution to the problem, thank you!
– buddy-stack
Hi, Vinhali, hi, hi. The formatting of your post is very good, but the content got confused (we only have your text, the rest only you know) , and probably for lack of essential information have votes to close and negatives. As you seem to be struggling to get it right, I believe these links can help in the elaboration of your posts: One of them is like making a [mcve], and another of them explains better what is the Problemaxy, mentioned in due course by @Andersoncarloswoss - If you can apply to your doubts, it will make it easier to get help.
– Bacco
Thank you so much for the tip, I will improve the next posts!
– buddy-stack