1

Is there any way to get the key range of same values and make a new array?

Let's say we have an Array Like this in php :

$first_array = ['1'=>'a','2'=>'a','3'=>'a','4'=>'b','5'=>'b','6'=>'a','7'=>'a'];

How can i get this array? Is there any function for this?

$second_array = ['1-3'=>'a','4-5'=>'b','6-7'=>'a'];
4
  • 8
    Have you tried anything Commented May 15, 2015 at 8:58
  • count a's in array, count b's in array. create new array Commented May 15, 2015 at 8:59
  • Are the as always associated with consecutive numbers? If not, should the output reflect their appearance or "group" them no matter where they appear? Be more specific, please. Commented May 15, 2015 at 9:02
  • Is it the keys you want to group who have a specific value (or the same)? Commented May 15, 2015 at 9:03

3 Answers 3

2

Loop through it, extract the keys, generate the ranges and insert to the new array -

$first_array = ['1'=>'a','2'=>'a','3'=>'a','4'=>'b','5'=>'b'];

$flip = array();
foreach($first_array as $key => $val) {
  $flip[$val][] = $key;
}

$second_array = [];
foreach($flip as $key => $value) {
    $newKey = array_shift($value).' - '.end($value);
    $second_array[$newKey] = $key;
}

Output

array(2) {
  ["1 - 3"]=>
  string(1) "a"
  ["4 - 5"]=>
  string(1) "b"
}
Sign up to request clarification or add additional context in comments.

4 Comments

thanks.but it does not work correct for some array like this :$first_array = ['1'=>'a','2'=>'a','3'=>'a','4'=>'a','5'=>'a','6'=>'b','7'=>'b','8'=>'a','9'=>'a'];
Yo have add check for that.
As @Amirabbasasadi pointed out, this doesn't always work. I had the same problem. Check out my answer below.
This wont work for same values as array cannot have same key multiple time. In that case you have to use sub arrays.
1

regarding your first question you can get range of each value using foreach() loop.

$first_array = ['1'=>'a','2'=>'a','3'=>'a','4'=>'b','5'=>'b'];

foreach($first_array as $key=>$value)
{
        //do your coding here, $key is the index of the array and $value is the value at that range, you can use that index and value to perform array manipulations
}

Regarding your second question it not exactly clear what are trying to implement there. But what ever you want to do like creating a new array with modified index and other things can be done within this foreach() loop itself

I hope this helps you.

Comments

1

If someone is still looking for an answer, here is what I did. Given the array

$first_array = ['0'=>'a',
                '1'=>'a',
                '2'=>'a',
                '3'=>'a',
                '4'=>'a',
                '5'=>'b',
                '6'=>'b',
                '7'=>'a',
                '8'=>'a']

I build a multidimensional array, in which each element is an array of three more elements:

[0] - The value in the first array
[1] - The key where the value starts repeating
[2] - The last key where the value stops repeating

The code

$arrayRange = [];

for($i = 0; $i < count($first_array); $i++){

    if(count($arrayRange) == 0){
        // The multidimensional array is still empty
        $arrayRange[0] = array($first_array[$i], $i, $i);
    }else{
        if($first_array[$i] == $arrayRange[count($arrayRange)-1][0]){
            // It's still the same value, I update the value of the last key
            $arrayRange[count($arrayRange)-1][2] = $i;
        }else{
            // It's a new value, I insert a new array
            $arrayRange[count($arrayRange)] = array($first_array[$i], $i, $i);
        }
    }
}

This way you get a multidimensional array like this:

$arrayRange[0] = array['a', 0, 4]; 
$arrayRange[1] = array['b', 5, 6];
$arrayRange[2] = array['a', 7, 8];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.