<?php
$abc = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","x","y","z");
$valores = array("luna","sol","abrir","acceso");
$contador = 0;
$result = array();
foreach ($valores as $value){
for ($i = 0; $i < strlen($value); $i++) {
$letra = $value[$i];
for($z = 0; $z < sizeof($abc); $z++){
if($abc[$z] === $letra){
echo $letra; // Debug
}
}
}
$contador++;
}
I have this code that checks that each letter of each word in the array values against the abc array . On echo letra
screen printing the result lunasolabriaccess . It checks it well, in that same piece of code I want to do what I am going to explain next and I don't know where to start.
As you can see there is a result array that is not being used at any time. In that array I want to save the words moon, sun .... from the values array but with the condition only save until in the abc array I don't have to go back in the dictionary.
So the result should be:
mon, s, apr, acces
I don't know if I explained it well, since it is somewhat complex to explain. If there is any doubt I will try to express myself better. I don't know how to start doing this. Thank you
Well, as far as I could understand, you want new words to be assembled according to the order of the alphabet and when a letter does not match the order, it stops and goes to the other word.
I made some modifications to your code, I leave it commented so that you understand what I did, in any case, if you have any doubts, do not hesitate to contact me.
Here the code:
Departure:
Just one observation in your example you put that the result of moon should be lun but the correct thing would be lu since n is before u .
Greetings. ;)