$tab1 = [“echelle”, “trio”, “pamplemousse”, “legumes”];
$tab2 = [“lama”, “geranium”, “pendule”, “elephant”];
question: Sort the 2 tables in such a way that table 1 contains only words that have at least 1 time the letter "u" and the second table the rest of the words that do not.
Hello, i'm a bit lost on displaying my code. I got the idea, which is adding elements that contains the letter 'u' to the 1st table, and deleting elements that doesn't contain the letter, and vice versa for the 2nd table, but i'm not sure how to do it.
i considered swapping elements, not sure if that would work:
for ($i=$position; $i < $tableau1[$position]; $i ) {
if (substr($tableau1 , $i , 1) == "u") {
$temporaire = $tableau1[$position];
$tableau1[$position] = $tableau2[$position];
$tableau2[$position] = $temporaire;
}
}
maybe this way:
while ($tableau1[$position]) {
if (substr($tableau1 , $i , 1) == "u") {
array_push($tableau2, $tableau1[$position]);
}
$position ;
}
CodePudding user response:
Merge the two arrays together then sort everything into the groups you want.
$tab1 = ["echelle", "trio", "pamplemousse", "legumes"];
$tab2 = ["lama", "geranium", "pendule", "elephant"];
// Merge the two arrays into one so we can look at each word in turn
$tab3 = array_merge( $tab1, $tab2 );
// Create new empty arrays so we can add the words into the right place
$tab1 = array();
$tab2 = array();
foreach ( $tab3 as $word ) {
if ( strpos( $word, 'u' ) !== FALSE ) {
$tab1[] = $word; // Words with a 'u' go in table 1
} else {
$tab2[] = $word; // Words without a 'u' go in table 2
}
}
print_r( $tab1 );
print_r( $tab2 );
Result:
Array
(
[0] => pamplemousse
[1] => legumes
[2] => geranium
[3] => pendule
)
Array
(
[0] => echelle
[1] => trio
[2] => lama
[3] => elephant
)
CodePudding user response:
using a foreach loop and strpos()
will allow you traverse an array simply and remove unset()
the specific values in the original array.
strpos()
will return FALSE if the searched for character does not exist, this make it a simple test for if its false then keep the array item otherwise remove it or visa versa to keep the items with a 'u' in
$tab1 = ["echelle", "trio", "pamplemousse", "legumes"];
$tab2 = ["lama", "geranium", "pendule", "elephant"];
// remove the items with a `u` in
foreach ( $tab1 as $i=>$t) {
if ( strpos($t, 'u') !== FALSE ) {
unset($tab1[$i]);
}
}
// remove the items without a `u` in
foreach ( $tab2 as $i=>$t) {
if ( strpos($t, 'u') === FALSE ) {
unset($tab2[$i]);
}
}
print_r($tab1);
print_r($tab2);
RESULTS
Array
(
[0] => echelle
[1] => trio
)
Array
(
[1] => geranium
[2] => pendule
)
CodePudding user response:
If I understand you correctly, you want to filter the first data array with all words that contain a "u". The second data array should contain all words that do not contain a "u". But if you need the word with and unless "u" from both arrays you can merge first and assign to a new array $tab_merged = array_merge( $tab1, $tab2 );
. Then you can use the function the_u_function() below. One time with true and second time with false as second parameter.
I would do it with a function. Like this:
<?php
$tab1 = ["echelle", "trio", "pamplemousse", "legumes"];
$tab2 = ["lama", "geranium", "pendule", "elephant"];
$tab_merged = array_merge( $tab1, $tab2 );
function the_u_function(array $arr, bool $with_u = true): array {
$returnArray = [];
foreach($arr as $elem) {
if ( strpos($elem, 'u') != $with_u ) {
$returnArray[] = $elem;
}
}
return $returnArray;
}
$results_with_u = the_u_function($tab_merged, true);
$results_without_u = the_u_function($tab_merged, false);
print_r($results_with_u);
print_r($results_without_u);
result
Array
(
[0] => echelle
[1] => trio
[2] => lama
[3] => elephant
)
Array
(
[0] => pamplemousse
[1] => legumes
[2] => geranium
[3] => pendule
)