I want to delete the ['Phone'] inside my array.
i tried foreach and unset but only the first array delete the ['Phone'].
my example array below.
Array
(
[0] => Array
(
[Name] => ads
[Phone] => 32132
)
[1] => Array
(
[Name] => ads
[Phone] => 321322
)
[2] => Array
(
[Name] => ads
[Phone] => 3213222
)
)
and my expected array.
Array
(
[0] => Array
(
[Name] => ads
)
[1] => Array
(
[Name] => ads
)
[2] => Array
(
[Name] => ads
)
)
You can use array_walk
array_walk($arr, function(&$v, $k){
unset($v['Phone']);
});
If all you want is to fetch the names, you can just pluck those using array_column().
$array = array_column($array, "Name");
Live demo at https://3v4l.org/VJTli
You can use map() function of Laravel Collection. and alter your original array however you want.
$newArray = collect($oldArray)->map(function($element) {
return [
'name' => $element['name'];
];
})->toArray();
Qirel's answer is faster I would imagine if you only have "name" and "phone". Another alternative that is easy on the eyes is:
foreach ($array as &$val) {
unset($val["Phone"]);
}
Related
I have a multi-dimensional array that looks like this:
Array
(
[0] => Array
(
[name] => nonce
[value] => 4OdIiR6JhZ,1565652176,9c1abd8d4e7c717bb1c8a27552aabce58b3bf4b3
)
[1] => Array
(
[name] => firstName
[value] => Honkey
)
[2] => Array
(
[name] => lastName
[value] => McDonalds
)
)
and I want to get an array that looks like this:
Array
(
[nonce] => 4OdIiR6JhZ,1565652176,9c1abd8d4e7c717bb1c8a27552aabce58b3bf4b3
[firstName] => Honkey
[lastName] => McDonalds
)
I know that I could accomplish this by doing a foreach loop and creating a new array.
$newForm = [];
foreach ($something as $index => $item) {
$newIndex = $item['name'];
$newForm[$newIndex] = $item['value'];
}
But I am wondering if there is a better way to do this (perhaps using one of PHP's array functions)?
This is what the 3 parameter form of array_column is perfect for:
$output = array_column($input, 'value', 'name');
Output:
Array
(
[nonce] => 4OdIiR6JhZ,1565652176,9c1abd8d4e7c717bb1c8a27552aabce58b3bf4b3
[firstName] => Honkey
[lastName] => McDonalds
)
Demo on 3v4l.org
With foreach,
$result = [];
foreach($array as $v){
$result[$v["name"]] = $v["value"];
}
Check Demo
I have following js array using serialisedArray -
Array
(
[0] => Array
(
[name] => sub_maintenance_template[1][maintenance_location_id]
[value] => 54321
)
[1] => Array
(
[name] => sub_maintenance_template[1][maintenance_problem_id]
[value] => 65432
)
[2] => Array
(
[name] => sub_maintenance_template[1][maintenance_priority_id]
[value] => 76896
)
[3] => Array
(
[name] => sub_maintenance_template[1][description]
[value] => sample description
)
)
Expected array -
[sub_maintenance_template] => Array (
[1] =>
(
[maintenance_location_id]=> 54321
[maintenance_problem_id]=> 65432
[maintenance_priority_id]=>76896
[description]=> sample description
)
)
I tried like this-
foreach( $tableData as $key => $value ) {
echo $key;
$newArray['sub_maintenance_template'][3][] = $value['name'];
$newArray['sub_maintenance_template'][3][] = $value['value'];
}
Even though I iterate it through foreach but failed to get desired output. IS there any way to get desired one?
It would be better to pass these as actual arrays in GET or POST, but since the string in name is how arrays would be passed in a URL query string, you can use parse_str:
foreach($array as $values) {
parse_str("{$values['name']} = {$values['value']}", $result);
}
print_r($result);
Or another way; extract and build key/value pairs to build a query string and then parse it:
parse_str(http_build_query(array_column($array, 'value', 'name')), $result);
print_r($result);
I have an array like this:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 1234
[name] => John
)
[1] => Array
(
)
)
[1] => Array
(
[0] => Array
(
[id] => 1234
[name] => John
)
[1] => Array
(
[id] => 5678
[name] => Sally
)
[2] => Array
(
[id] => 1234
[name] => Duke
)
)
My resulting array should be the following (basically merging and getting rid of duplicates and removing null values):
Array
(
[0] => Array
(
[id] => 1234
[name] => John
)
[1] => Array
(
[id] => 5678
[name] => Sally
)
[2] => Array
(
[id] => 1234
[name] => Duke
)
)
Is there an easy way to do this using PHP's array functions?
EDIT: So far I have this:
$result = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$result = array_merge($result, $value);
}
}
print_r($result);
Use array_merge to merge the arrays. Then use array_unique to remove duplicates. Finally, to remove null keys, use a loop.
foreach($array as $key=>$value)
{
if(is_null($value) || $value == '')
unset($array[$key]);
}
Documentation on these methods can be found here.
Flatten your unnecessarily depth array structure by unpacking the subarrays into a merge call.
Filter out the empty subarrays with array_filter().
Remove the duplicate subarrays with array_unique(). (SORT_REGULAR prevents "Warning: Array to string conversion")
Code: (Demo)
var_export(
array_unique(
array_filter(
array_merge(...$array)
),
SORT_REGULAR
)
);
Optionally, call array_values() on the resultant array to re-index it (remove the old keys).
I have one multi dimentional array, the problem is I want the array values of specific key. I already tried current() and end() of array which is not useful to me. So please suggest me appropriate solution to find array values of specific key without using any loop. My Demo array is
Array
(
[0] => Array
(
[EntityType] => Array
(
[Id] => 1
[Code] => SUP/13-14/10001
[Name] => Supplier
[DisplayName] => Supplier
[ModuleIdentifier] => 1
[IsAdd] =>
[IsEdit] => 1
[IsDelete] => 1
)
)
[1] => Array
(
[EntityType] => Array
(
[Id] => 2
[Code] => Emp/13-14-10002
[Name] => Employee
[DisplayName] => Employee
[ModuleIdentifier] => 1
[IsAdd] =>
[IsEdit] =>
[IsDelete] =>
)
)
[2] => Array
(
[EntityType] => Array
(
[Id] => 3
[Code] => CUS/13-14/10003
[Name] => Customer
[DisplayName] => Customer
[ModuleIdentifier] => 1
[IsAdd] => 1
[IsEdit] =>
[IsDelete] =>
)
)
)
I want array having name Customer. So how to get these array...
Thanks !
You may use array_filter in conjunction with array_map:
function findElem($array, $val) {
$result = array_map(
function ($v) { return $v['EntityType']; },
array_filter($array, function ($v) use($val) { return $v['EntityType']['Name'] == $val; })
);
return count($result)? $result[0] : false;
}
print_r(findElem($array, 'Customer'));
If you want to access n'th element of your array just try with:
$array[n]
Where n is an integer value, so:
$array[2]
This line will get you all values of the key "Name" assuming your source array is named $arr if that's what you wanted:
$names = array_map( function($item) { return $item["EntityType"]["Name"]; } , $arr );
You can access your array data like this: $array[0]["EntityType"]["ID"].
I have nested arrays that do not have keys. I want to add keys in a particular order. What is a clean way to do this?
Start with this array. It is only indexed by position.
[0] => (
[0] => Tyler
[1] => Durden
[2] => 05/07/1985
)
[1] => (
[0] => Edward
[1] => Norton
[2] => 03/21/1988
)
Now apply these keys in order:
['first_name'] =>
['last_name'] =>
['birthday'] =>
Final array:
[0] => (
['first_name'] => Tyler
['last_name'] => Durden
['birthday'] => 05/071985
)
[1] => (
['first_name'] => Edward
['last_name'] => Norton
['birthday'] => 03/21/1988
)
Bonus upvotes if your code allows for any key structure, instead of being hard-coded!
I think array_combine will do the trick:
foreach ($bigarray as &$x) {
$x = array_combine(array('first_name','last_name','birthday'),$x);
}
Also the classic algorythm:
foreach ( $array as $key=>$value ) {
$array[$key]['first_name'] = $array[$key][0];
$array[$key]['last_name'] = $array[$key][1];
$array[$key]['birthday'] = $array[$key][2];
unset($array[$key][0]);
unset($array[$key][1]);
unset($array[$key][2]);
}