Disorder the mass



  • There's a mass.

    $arr = [
        [ 'beg' => 75, 'end' => 75],
        [ 'beg' => 02, 'end' => 23],
        [ 'beg' => 75, 'end' => 48],
        [ 'beg' => 23, 'end' => 18],
        [ 'beg' => 48, 'end' => 02],
        [ 'beg' => 75, 'end' => 75],
        [ 'beg' => 02, 'end' => 02]
    ];
    

    The first element of the following line should be quantified to be the last element of the current line, with the help of the PHP, which would have at the exit:

    [
      02 => 75 , 
      75 => 23
      ...
    ]
    


  • Philosopher's option.

    Just for fun.

    function check($array){
        for($i = 1; $i < count($array); $i++){
            if($array[$i-1]['end'] != $array[$i]['beg']) return false;
        }
        return true;
    }
    $arr = [
        [ 'beg' => 75, 'end' => 75],
        [ 'beg' => 02, 'end' => 23],
        [ 'beg' => 75, 'end' => 75],
        [ 'beg' => 75, 'end' => 48],
        [ 'beg' => 23, 'end' => 18],
        [ 'beg' => 48, 'end' => 02],
        [ 'beg' => 02, 'end' => 02]
    ];
    

    while(true){
    if(check($arr)) break;
    shuffle($arr);
    }
    foreach($arr as $a){
    echo $a['beg'].' - '.$a['end'].PHP_EOL;
    }


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2