Multiexplode function with array_intersect returning null elements

Asked

Viewed 50 times

-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?

View code

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!

  • 1

    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, the array_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.

  • I got it, I found a solution to the problem, thank you!

  • 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.

  • Thank you so much for the tip, I will improve the next posts!

1 answer

-1


Possible solution:

for($j=0; $j <= 3; $j++){

    $lista = implode($lista1);
    $lista3 = multiexplode(array(":SO"), $lista);

}
  • 2

    Could you complete your answer by explaining what the problem was and why this snippet of code solved? The code may seem simple to you, but to other users of the community it may not make sense, so it is always interesting you explain through text.

  • comment yes, I took to reach this solution and the other posts were not objective because the problem situation changed, the data comes from different comics, this complicated the situation!

  • 1

    That’s why I commented that this is looking like an XY problem. Instead of going to the source of the problem, it seems that you are creating a "solution" that generates another problem, then you need to solve this other problem generating a new problem... It would be much simpler if you described the real problem you need to solve and so we could know if what you’re doing makes sense or not. In my experience, given the codes you posted in the questions, you don’t. It seems like you’re complicating something that should be simple.

  • I described the problem together with the solution, if you know a way to optimize it, would be of great help!

  • This description of the problem should be part of the question, not the answer.

  • Amended post !

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.