Quantcast
Channel: Remove empty array elements - Stack Overflow
Browsing all 28 articles
Browse latest View live

Answer by puiu for Remove empty array elements

With these types of things, it's much better to be explicit about what you want and do not want. It will help the next guy to not get caught by surprise at the behaviour of array_filter() without a...

View Article



Answer by Shahrukh Anwar for Remove empty array elements

As per your method, you can just catch those elements in an another array and use that one like follows, foreach($linksArray as $link){ if(!empty($link)){ $new_arr[] = $link } } print_r($new_arr);

View Article

Answer by Nabi K.A.Z. for Remove empty array elements

In short: This is my suggested code: $myarray = array_values(array_filter(array_map('trim', $myarray), 'strlen')); Explanation: I thinks use array_filter is good, but not enough, because values be like...

View Article

Answer by Ron Cemer for Remove empty array elements

The most popular answer on this topic is absolutely INCORRECT. Consider the following PHP script: <?php $arr = array('1', '', '2', '3', '0'); // Incorrect: print_r(array_filter($arr)); // Correct:...

View Article

Answer by Cranio for Remove empty array elements

The most voted answer is wrong or at least not completely true as the OP is talking about blank strings only. Here's a thorough explanation: What does empty mean? First of all, we must agree on what...

View Article


Answer by Ashish pathak for Remove empty array elements

try this ** **Example $or = array( 'PersonalInformation.first_name' => $this->request->data['User']['first_name'], 'PersonalInformation.last_name' =>...

View Article

Answer by mulquin for Remove empty array elements

If you are working with a numerical array and need to re-index the array after removing empty elements, use the array_values function: array_values(array_filter($array)); Also see: PHP reindex array?

View Article

Answer by Chrisdigital for Remove empty array elements

Just want to contribute an alternative to loops...also addressing gaps in keys... In my case I wanted to keep sequential array keys when the operation was complete (not just odd numbers, which is what...

View Article


Answer by iBet7o for Remove empty array elements

Remove empty array elements function removeEmptyElements(&$element) { if (is_array($element)) { if ($key = key($element)) { $element[$key] = array_filter($element); } if (count($element) !=...

View Article


Answer by HMagdy for Remove empty array elements

For multidimensional array $data = array_map('array_filter', $data); $data = array_filter($data);

View Article

Answer by Naitik Shah for Remove empty array elements

$out_array = array_filter($input_array, function($item) { return !empty($item['key_of_array_to_check_whether_it_is_empty']); } );

View Article

Answer by Naitik Shah for Remove empty array elements

$my = ("0"=>" ","1"=>"5","2"=>"6","3"=>" "); foreach ($my as $key => $value) { if (is_null($value)) unset($my[$key]); } foreach ($my as $key => $value) { echo $key . ':' . $value ....

View Article

Answer by Matt for Remove empty array elements

Just one line : Update (thanks to @suther): $array_without_empty_values = array_filter($array);

View Article


Answer by user2511140 for Remove empty array elements

$a = array(1, '', '', '', 2, '', 3, 4); $b = array_values(array_filter($a)); print_r($b)

View Article

Answer by Ankit Gupta for Remove empty array elements

use array_filter function to remove empty values: $linksArray = array_filter($linksArray); print_r($linksArray);

View Article


Answer by Mak Ashtekar for Remove empty array elements

foreach($arr as $key => $val){ if (empty($val)) unset($arr[$key]; }

View Article

Answer by helpse for Remove empty array elements

You can just do array_filter($array) array_filter: "If no callback is supplied, all entries of input equal to FALSE will be removed." This means that elements with values NULL, 0, '0', '', FALSE,...

View Article


Answer by concept w for Remove empty array elements

I use the following script to remove empty elements from an array for ($i=0; $i<$count($Array); $i++) { if (empty($Array[$i])) unset($Array[$i]); }

View Article

Answer by Matt for Remove empty array elements

I had to do this in order to keep an array value of (string) 0 $url = array_filter($data, function ($value) { return (!empty($value) || $value === 0 || $value==='0'); });

View Article

Answer by ali Farmani for Remove empty array elements

function trim_array($Array) { foreach ($Array as $value) { if(trim($value) === '') { $index = array_search($value, $Array); unset($Array[$index]); } } return $Array; }

View Article
Browsing all 28 articles
Browse latest View live




Latest Images