Skip to content

Commit

Permalink
Merge a966b93 into 8d4e335
Browse files Browse the repository at this point in the history
  • Loading branch information
aiddroid committed May 11, 2016
2 parents 8d4e335 + a966b93 commit c3babd9
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/utilphp/util.php
Expand Up @@ -2342,6 +2342,66 @@ public static function array_pluck(array $array, $field, $preserve_keys = true,
return $new_list;
}

/**
* pull array value from specified fields
* @param array $array An array
* @param string|array $fields The field to get values from
* @param boolean $$preserve_keys Whether or not to preserve the array keys
* @return mixed
*/
public static function array_pull(array $array, $fields, $preserve_keys = true)
{
$new_array = array();
if(is_string($fields)){
if($preserve_keys){
$new_array[$fields] = isset($array[$fields]) ? $array[$fields] : null;
}else{
$new_array[] = isset($array[$fields]) ? $array[$fields] : null;
}
}else if(is_array($fields)){
foreach($fields as $field){
if($preserve_keys){
$new_array[$field] = isset($array[$field]) ? $array[$field] : null;
}else{
$new_array[] = isset($array[$field]) ? $array[$field] : null;
}
}
}

return $new_array;
}


/**
* pack given values into an array with specified keys
*
* example:
*
* $names = array('Allen', 'Jack', 'Lucy');
* $ages = array(20, 22, 24);
* $sexes = array('M', 'M', 'F');
*
* array_pack(array('name' => $names, 'age' => $ages, 'sex' => $sexes));
* //array(array('name' => 'Allen', 'age' => 20, 'sex' => 'M'), array('name' => 'Jack','age' => 22, 'sex' => 'M'), array('name' => 'Lucy','age' => 24, 'sex' => 'F'))
*
* @param array $array input array
* @return array $packed the packed array
*/
public static function array_pack($array)
{
$keys = array_keys($array);
$values = array_values($array);

$packed = array();
foreach ($values as $index => $value) {
foreach ($value as $i => $v) {
$packed[$i][$keys[$index]] = $v;
}
}

return $packed;
}

/**
* Searches for a given value in an array of arrays, objects and scalar
* values. You can optionally specify a field of the nested arrays and
Expand Down
20 changes: 20 additions & 0 deletions tests/UtilTest.php
Expand Up @@ -498,12 +498,32 @@ public function test_array_pluck()
$this->assertEquals($expected, util::array_pluck($array, 'name', false, false));
}

public function test_array_pull()
{
$array = array('id' => 1000, 'username' => 'allen','password' => '123456', 'email' => 'admin@admin.com', 'created_at' => 1461317101);

$this->assertEquals(array('realname' => null), util::array_pull($array, 'realname'));
$this->assertEquals(array('username' => 'allen'), util::array_pull($array, 'username'));
$this->assertEquals(array('id' => 1000, 'username' => 'allen'), util::array_pull($array, array('id', 'username')));
$this->assertEquals(array(1000, 'allen'), util::array_pull($array, array('id', 'username'), false));
}

public function test_htmlentities()
{
$this->assertEquals( 'One & Two', util::htmlentities( 'One & Two' ) );
$this->assertEquals( 'One & Two', util::htmlentities( 'One & Two', TRUE ) );
}

public function test_array_pack()
{
$names = array('Allen', 'Jack', 'Lucy');
$ages = array(20, 22, 24);
$sexes = array('M', 'M', 'F');

$expected = array(array('name' => 'Allen', 'age' => 20, 'sex' => 'M'), array('name' => 'Jack', 'age' => 22, 'sex' => 'M'), array('name' => 'Lucy', 'age' => 24, 'sex' => 'F'));
$this->assertEquals($expected, util::array_pack(array('name' => $names, 'age' => $ages, 'sex' => $sexes)));
}

public function test_htmlspecialchars()
{
$this->assertEquals( 'One & Two', util::htmlspecialchars( 'One & Two' ) );
Expand Down

0 comments on commit c3babd9

Please sign in to comment.