Mohsen Nirouzad
Mohsen Nirouzad

Reputation: 27

Delete empty members of an PHP array

I have a array called $urls, I want to delete members with value null. I want first submit every member to a function that checks if a member is null, and then I delete the member from array. E.X.

$urls=array();
$url[0]=$_POST['urla'];
$url[1]=$_POST['urlb'];
$url[2]=$_POST['urlc'];
$url[3]=$_POST['urld'];
$url[4]=$_POST['urle'];

Well, I want to delete $urls members that has no value (because user didn't fill out the fields ), How can I do it? thank you for your help

Upvotes: 1

Views: 80

Answers (4)

Aman Srivastava
Aman Srivastava

Reputation: 151

<?php  
$urls = array_filter($url);
?>

Upvotes: 1

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72289

You can do it in below mentioned ways:-

1.array_filter():-

<?php
 array_filter($_POST);
 $urls = $_POST;
 print_r($urls);
?>

2.Use array_values():-

<?php
   $urls = array_values($_POST);
   print_r($urls);
?>

Reference:-

http://php.net/manual/en/function.array-filter.php

http://php.net/manual/en/function.array-values.php

Upvotes: 1

subbu
subbu

Reputation: 26

Please check the request post members like following

if(isset($_POST['urla']) && $_POST['urla'] != '') 
{
    $url[0]=$_POST['urla'];
}

if not working please send me more inputs.

Upvotes: 0

Manikiran
Manikiran

Reputation: 1

Simply use array_filter(), which conveniently handles this for you:

<?php
print_r(array_filter($urls));
?>

Upvotes: 2

Related Questions