How to unset last item in multidimensional array? - php

I have a array like below
Array
(
[0] => Array
(
[0] => Date
[1] => Name
[2] => Hours
)
[1] => Array
(
[0] => 2013-01-02
[1] => Test User
[2] => 7:59
)
[2] => Array
(
[0] => 2013-01-03
[1] => Test User
[2] => 7:53
)
[3] => Array
(
[0] => 2013-01-04
[1] => Test User
[2] => 8:12
)
.
.
.
.
[16] => Array
(
[0] =>
[1] => Total
[2] => 103:1
)
[17] => Array
(
[0] =>
)
)
And want to remove last item from array, I have tried array_pop but this is not working after passing above array to array_pop gives me output
Array
(
[0] =>
)
How can I achieve this with minimum code.

Try:
unset ($array_name[count($array_name)-1]);

$callback = function(&$array) { array_pop($array); };
array_walk($array, $callback);
This will pop the last element from each triplet.

Try like
$my_cnt = count($my_arr);
unset($my_arr[$my_cnt-1]);

if all your arrays are indexed by numbers from zero to max without any breaks, you can use
unset($ar[count($ar)-1]);
otherwise try
end($ar);
unset($ar[key($ar)]);

You are seeing the "popped" element instead of the modified array.
array_pop() returns the data in the element that it removes from the array.
This means that you wrote:
print_r(array_pop($array));
Instead, modify the array with array_pop(), then print the array.
array_pop($array);
print_r($array);

Related

How to remove duplicate values from array - php

I am trying to remove duplicate and empty values from array with array_unique() function, but getting wrong output.
Data:
Array (
[0] => Array (
[0] =>
[1] => 1
[2] =>
[3] => 108
[4] =>
)
[1] => Array (
[0] =>
[1] => 1
[2] =>
[3] => 108
[4] =>
[5] => 101
)
[2] => Array (
[0] =>
[1] =>
[2] => 108
[3] =>
)
)
PHP:
$array = array_filter($userids);
$arrays = array_unique($array, SORT_REGULAR);
print_r($arrays);
nothing happens with SORT_REGULAR - output comes same as raw data, and without SORT_REGULAR this output is coming:
$array = array_filter($userids);
$arrays = array_unique($array);
print_r($arrays);
output:
Array (
[0] => Array
(
[0] =>
[1] => 1
[2] =>
[3] => 108
[4] =>
)
)
output I am looking for:
Array (
[0] => Array
(
[0] => 1
[1] => 108
[2] => 101
)
)
Those array functions only works on a single level. If you flatten the array (adding all elements in a single element array), it should be pretty straight forward.
Flatten the array
$array = array_merge(...$array);
Note: This method works fine for flattening indexed arrays like in your example, but not for associative arrays where any of the sub arrays contains the same keys.
Then filter out all empty
$array = array_filter($array);
and then remove all duplicates
$array = array_unique($array);
Or as a one-liner:
$array = array_unique(array_filter(array_merge(...$array)));
Demo: https://3v4l.org/pEJAJ

Unwrap array inside another array PHP

I am getting a string separated from commas and I am trying to split them into an array, which works. The only problem is that there is an outer array wrapping the array I want to use. So I don't want to use the $excludes[0] when passing the array to a function. Does anyone know a function I can use to unwrap the array inside $excludes[0]
$excludes = [];
Array ( [0] => Array (
[0] => company_logo,
[1] => social_links,
[2] => rss_link,
[3] => telephone ) )
My expected results would be the below.
Array (
[0] => company_logo,
[1] => social_links,
[2] => rss_link,
[3] => telephone
)
You can simple do :
1st Option:
print_r(array_shift($excludes));
2nd Option:
$new_array = $excludes[0];
print_r($new_array);
Hope this will work
check this
$excludes = Array ( 0 => Array (
0 => 'company_logo',
1 => 'social_links',
2 => 'rss_link',
3 => 'telephone' ) ) ;
$excludes=$excludes[0];
print_r($excludes);die();

Removing parts of a multidimensional array in PHP

I have this array $data :
Array
(
[0] => 86086
[1] => Arnel
[2] => Paras
)
Array
(
[0] => 86085
[1] => Arnely
[2] => Para
)
Array
(
[0] =>
)
How do i remove the bottom array that contains no values totally so it only contains :
Array
(
[0] => 86086
[1] => Arnel
[2] => Paras
)
Array
(
[0] => 86085
[1] => Arnely
[2] => Para
)
I have tried using array_filter($data, strlen) and it just does this :
Array
(
)
array_pop() pops and returns the value of the last element of array, shortening the array by one element and will do exactly what you describe. A.
array_shift() does the opposite (removes first element from array and returns the value)
array_pop() on PHP.net
So you can either do:
$firstVal = array_pop($data)
or just
array_pop($data)
depending on if you want the value back or not.
Might help.
array_values(array_filter($data))

Array push to one of my multidimentional array

I have array like below. I would like to push it to 0 element array.
$csvdata is contain original array $pushHeaderSpec variable is what i want to push into original array i have also tried array_merge but not work as expected merge well on output only but when i print original data in csv it is not there.i m generating $csvdata array first and then append this array on last.
Array
(
[Ruder] => no value need on this
[Glas] => no value need on this
[Not] => no value need on this
)
My Multidimention array look something like
Array
(
[0] => Array
(
[0] => Sort order
[1] => Sku
[2] => Title
)
)
Many more element on above array so i just want to merge my first array keys to this array on first element that is 0.
I did try using below code but it doesn't give me output what i want.
array_push($csvdata[0],array_keys($pushHeaderSpec));
Output from code
Array
(
[0] => Array
(
[0] => Sort order
[1] => Sku
[2] => Title
[3] =>array (
[0] => Ruder
[1] => Glas
[2] => Not
)
)
)
Expecting Output
Array
(
[0] => Array
(
[0] => Sort order
[1] => Sku
[2] => Title
[3] => Ruder
[4] => Glas
[5] => Not
)
)
It was just
foreach (array_keys($pushHeaderSpec) as $key => $value) {
array_push($csvdata[0],$value);
}
Is that what you are looking for ?
foreach ($pushHeaderSpec as $key => $val) {
$csvdata[0][] = $key;
}

Rename multidimensional array keys

I'm trying to rename my keys in a multidimensional array. I looked at this: Renaming the keys in multidimensional associate arrays and it does rename it, but only for 1 of my 2 arrays. How can I get it to rename the 1st array time and the second array count?
My output right now is :
Array
(
[0] => Array
(
[0] => 00:00
[1] => 00:15
[2] => 00:30
)
[1] => Array
(
[0] => 8
[1] => 9
[2] => 8
)
)
I need [0] to be time and [1] to be count.
If I use this:
foreach ($sliced_array as $id => $dataset) {
$newArray["time"] = $dataset;
}
I can get it to output only array [1] renamed as time. It should be count and array [0] disappears entirely. Is there a way to focus which dataset the foreach targets? dataset[0] does not work.
My expected output is:
Array
(
[time] => Array
(
[0] => 00:00
[1] => 00:15
[2] => 00:30
)
[count] => Array
(
[0] => 8
[1] => 9
[2] => 8
)
)
Did you just try:
$myArray["time"] = $myArray[0];
$myArray["count"] = $myArray[1];
unset($myArray[0]);
unset($myArray[1]);
or just:
$newArray["time"] = $myArray[0];
$newArray["count"] = $myArray[1];
?
$newArray = array_combine(array('time','count'),$sliced_array);
http://es1.php.net/array_combine

Categories