Bolt\Helpers\Arr::makeValuePairs PHP Method

makeValuePairs() public static method

Make a simple array consisting of key=>value pairs, that can be used in select-boxes in forms.
public static makeValuePairs ( array $array, string $key, string $value ) : array
$array array
$key string
$value string
return array
    public static function makeValuePairs($array, $key, $value)
    {
        $tempArray = [];
        if (is_array($array)) {
            foreach ($array as $item) {
                if (empty($key)) {
                    $tempArray[] = $item[$value];
                } else {
                    $tempArray[$item[$key]] = $item[$value];
                }
            }
        }
        return $tempArray;
    }

Usage Example

Exemplo n.º 1
0
 public function testMakeValuePairs()
 {
     $test = array(array('id' => 1, 'value' => 1), array('id' => 2, 'value' => 2));
     $this->assertEquals(array(1 => 1, 2 => 2), Arr::makeValuePairs($test, 'id', 'value'));
     $this->assertEquals(array(0 => 1, 1 => 2), Arr::makeValuePairs($test, '', 'value'));
 }
All Usage Examples Of Bolt\Helpers\Arr::makeValuePairs