Reputation: 375
I have an array of elements, I need to change elements at the beginning of an array and I'm using the foreach.
Array
(
[0] => Array
(
[0] => user
[1] => address
[2] => detail
[3] => name
[4] => family
)
[1] => Array
(
[0] => user
[1] => address
)
[2] => Array
(
[0] => user
[1] => address
[2] => detail
[3] => name
)
)
I want this output:
Array
(
[0] => Array
(
[0] => user.address.detail.name
[1] => family
)
[1] => Array
(
[0] => user
[1] => address
)
[2] => Array
(
[0] => user.address.detail
[1] => name
)
)
this is my code but it seems it's not working.
$firstTemp = "";
foreach ($temps as $row => $temp) {
if (count($temp) > 2) {
foreach ($temp as $k => $content) {
$firstTemp .= $content . '.';
$endTemp = end($temp);
}
}
}
Is there a better and more concise way to accomplish this?
Upvotes: 2
Views: 156
Reputation: 18557
I made some modification in your snippet to make it work,
$result = [];
foreach ($temps as $row => $temp) {
if (count($temp) > 2) {
// I am taking a slice of the array except for last element and imploding it with `.`
// then I am fetching the last element of an array
// creating an array and pushing it into the result variable
$result[] = [implode(".", array_slice($temp, 0, count($temp) - 1)), end($temp)];
}else{
$result[] = $temp;
}
}
I am imploding all the elements except last element using array_slice.
end
I used to fetch the last element of an array.
Demo.
EDIT 1
One more way to achieve the same,
$result = [];
foreach ($temps as $row => $temp) {
if (count($temp) > 2) {
// except last elements
$result[] = [implode(".", array_slice($temp, 0, -1)), end($temp)];
}else{
$result[] = $temp;
}
}
Demo.
EDIT 2
$result = [];
foreach ($temps as $row => $temp) {
$result[] = (count($temp) > 2 ? [implode(".", array_slice($temp, 0, -1)), end($temp)] : $temp);
}
Demo.
EDIT 3
$result = array_map(function($temp){
return (count($temp) > 2 ? [implode(".", array_slice($temp, 0, -1)), end($temp)] : $temp);
},$temps);
Demo.
EDIT 4: Without condition
$result = array_map(function($temp){
return [implode(".",array_slice($temp,0,-1)),array_pop($temp)];
},$temps);
EDIT 5
$result = array_map(function($temp){
return [implode(".",array_slice($temp,0,-1)),end($temp)];
},$temps);
Demo.
Output:
Array
(
[0] => Array
(
[0] => user.address.detail.name
[1] => family
)
[1] => Array
(
[0] => user
[1] => address
)
[2] => Array
(
[0] => user.address.detail
[1] => name
)
)
Upvotes: 4
Reputation: 3562
You can pull the last element
and implode()
rest of the element from array Like:
$formatted = [];
foreach($temps as $arr) {
if(count($arr) == 0){
return;
}
$last = array_pop($arr);
if(count($arr) > 0) //check if array not empty.
$formatted[] = [implode(".", $arr), $last];
else
$formatted[] = [$last];
}
Upvotes: 3
Reputation: 1175
You can do it without using two foreach
structures. For example:
$result = array_map(function (&$temp) {
$last = array_pop($temp);
$first = implode('.', $temp);
return [$first, $last];
}, $temps);
Upvotes: 0
Reputation: 859
$data = [
[
'user',
'address',
'detail',
'name',
'family',
], [
'user',
'address',
], [
'user',
'address',
'detail',
'name',
'family',
]
];
$result = array_map(function($item) {
$last = array_pop($item);
return [implode('.', $item), $last];
}, $data);
Upvotes: 0