BeHappy Posted November 21, 2020 Share Posted November 21, 2020 Hello; I would like to do the following operation; Count duplicate (adjacent) my code <?php $Array = array("test", "test", "hello", "test", "world", "world", "world", "hello", "test"); for( $i= 0 ; $i <= 6 ;$i++ ) { $j=1; if ($Array[$i] === $Array[$i+1]) { $j+=$j; echo $j; } ?> and showing something like 2,1,1,3,1,1 Thanks Quote Link to comment https://forums.phpfreaks.com/topic/311753-count-duplicate-adjacent-array/ Share on other sites More sharing options...
gw1500se Posted November 21, 2020 Share Posted November 21, 2020 Use array_count_values to determine which keys occur how many times. If you want an array of just the counts you can build an array from that with just the values. Quote Link to comment https://forums.phpfreaks.com/topic/311753-count-duplicate-adjacent-array/#findComment-1582563 Share on other sites More sharing options...
BeHappy Posted November 21, 2020 Author Share Posted November 21, 2020 2 minutes ago, gw1500se said: Use array_count_values to determine which keys occur how many times. If you want an array of just the counts you can build an array from that with just the values. Hi, thanks can you give me an example with code please 😇 Quote Link to comment https://forums.phpfreaks.com/topic/311753-count-duplicate-adjacent-array/#findComment-1582564 Share on other sites More sharing options...
gw1500se Posted November 21, 2020 Share Posted November 21, 2020 The link to the manual has examples. 1 Quote Link to comment https://forums.phpfreaks.com/topic/311753-count-duplicate-adjacent-array/#findComment-1582567 Share on other sites More sharing options...
BeHappy Posted November 21, 2020 Author Share Posted November 21, 2020 34 minutes ago, gw1500se said: The link to the manual has examples. There is no solution for adjacent duplicate in the link !!! Quote Link to comment https://forums.phpfreaks.com/topic/311753-count-duplicate-adjacent-array/#findComment-1582569 Share on other sites More sharing options...
Barand Posted November 21, 2020 Share Posted November 21, 2020 if array == array[i-1] then you have adjacent duplicates 1 Quote Link to comment https://forums.phpfreaks.com/topic/311753-count-duplicate-adjacent-array/#findComment-1582570 Share on other sites More sharing options...
BeHappy Posted November 21, 2020 Author Share Posted November 21, 2020 (edited) 15 minutes ago, Barand said: if array == array[i-1] then you have adjacent duplicates Thank you Barand; This my array $Array = array("test", "test", "hello", "test", "world", "world", "world", "hello", "test"); Well, i would like to show a list with adjacent duplicates for example the word test 2 not 4 duplicates and finally the output will be like this 2,1,1,3,1,1 the problem that array_count_values show the all duplicates, i would like to show the adjacent duplicates Edited November 21, 2020 by BeHappy Quote Link to comment https://forums.phpfreaks.com/topic/311753-count-duplicate-adjacent-array/#findComment-1582572 Share on other sites More sharing options...
Barand Posted November 21, 2020 Share Posted November 21, 2020 You've had some clues. What have you tried? Quote Link to comment https://forums.phpfreaks.com/topic/311753-count-duplicate-adjacent-array/#findComment-1582574 Share on other sites More sharing options...
BeHappy Posted November 21, 2020 Author Share Posted November 21, 2020 (edited) 11 minutes ago, Barand said: You've had some clues. What have you tried? I have created this code but i think there's a problem with it <?php $array = array("test", "test", "hello", "test", "world", "world", "world", "hello", "test"); $j=1; for( $i= 0 ; $i <= 7 ;$i++ ){ if ($array[$i] === $array[$i+1]) { echo $j .= $j+1; } else { echo "1"; } }?> output = 1211112131213121411 not 2,1,1,3,1,1 😅 Edited November 21, 2020 by BeHappy Quote Link to comment https://forums.phpfreaks.com/topic/311753-count-duplicate-adjacent-array/#findComment-1582575 Share on other sites More sharing options...
Solution Barand Posted November 22, 2020 Solution Share Posted November 22, 2020 try $arr = array("test", "test", "hello", "test", "world", "world", "world", "hello", "test"); $prev = ''; $results = []; $j = 0; foreach ($arr as $i => $v) { if ($v != $prev) { $j = $i; $results[$j] = 1; $prev = $v; } else { $results[$j]++; } } foreach ($results as $k => $v) { echo "$arr[$k] : $v <br>"; } Result: test : 2 hello : 1 test : 1 world : 3 hello : 1 test : 1 1 Quote Link to comment https://forums.phpfreaks.com/topic/311753-count-duplicate-adjacent-array/#findComment-1582576 Share on other sites More sharing options...
BeHappy Posted November 22, 2020 Author Share Posted November 22, 2020 Barand 😍🥰 Thank you so much, you are a genius 💗💞 Quote Link to comment https://forums.phpfreaks.com/topic/311753-count-duplicate-adjacent-array/#findComment-1582577 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.