Parkour\Transform::reindex PHP Method

reindex() public static method

Reindexes a list of values.
public static reindex ( array $data, array $map, $keepUnmapped = true ) : array
$data array Data.
$map array An map of correspondances of the form ['currentIndex' => 'newIndex'].
return array $keepUnmapped Whether or not to keep keys that are not remapped.
    public static function reindex(array $data, array $map, $keepUnmapped = true)
    {
        $reindexed = $keepUnmapped ? $data : [];
        foreach ($map as $from => $to) {
            if (isset($data[$from])) {
                $reindexed[$to] = $data[$from];
            }
        }
        return $reindexed;
    }

Usage Example

 /**
  *
  */
 public function testReindex()
 {
     $data = ['foo' => 'bar'];
     $map = ['foo' => 'baz'];
     $expected = ['foo' => 'bar', 'baz' => 'bar'];
     $this->assertEquals($expected, Transform::reindex($data, $map));
     $expected = ['baz' => 'bar'];
     $this->assertEquals($expected, Transform::reindex($data, $map, false));
 }
All Usage Examples Of Parkour\Transform::reindex