Created
September 6, 2019 11:36
-
-
Save novia713/df74ed5abe647db084641b12668e4f26 to your computer and use it in GitHub Desktop.
my_array_column()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$obj_1 = new StdClass; | |
$obj_1->name = "Toby"; | |
$obj_2 = new StdClass; | |
$obj_2->name ="Martha"; | |
$obj_3 = new StdClass; | |
$obj_3->n = 5; | |
$ar1 = ['name'=> 'Paul']; | |
$ar2 = ['name'=> 'Jess']; | |
$ar3 = ['age'=> '19']; | |
$arr = [$obj_1, $obj_2]; | |
$arr2 = [$ar1, $ar2, $ar3]; | |
function my_array_column($arr, $name){ | |
$_ = []; | |
foreach ($arr as $e){ | |
if (is_array($e)){ | |
$_[] = @$e[$name]; | |
}else if (is_object($e)){ | |
$_[] = @$e->$name; | |
} | |
} | |
return $_; | |
}; | |
function my_array_column2($arr, $name){ | |
return array_map( | |
function ($e) use ($name) { | |
if (is_array($e)){ | |
return @$e[$name]; | |
}else if (is_object($e)){ | |
return @$e->$name; | |
} | |
} | |
,$arr | |
); | |
} | |
$res = implode("\n", my_array_column($arr, "name")); | |
echo $res; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment