2

I have an associative array in PHP

$a = array("d1" => "data", "d2" => NULL, "d3" => "data")

I want to get all keys and all values which are not NULL, in order to implode them:

// e.g.:
$sub_key    = array_keys($a, keys != NULL);
$sub_values = array_values($a, values != NULL);

echo "`".implode("`,`", $sub_key)."`";
echo "'".implode("','", $sub_key)."'";

Are there functions like array_keys() and array_values() that allow to take only vales that do not match the pattern?

4
  • Start with array_filter() which by default will return anything evaluating FALSE (more than just nulls); Commented May 22, 2013 at 10:59
  • Use array_filter to find all matching values, then use array_keys and array_values on the filtered result. Commented May 22, 2013 at 10:59
  • What would be your expected output ? Commented May 22, 2013 at 11:00
  • You could of course array_diff(array_keys($a),array_keys($a,NULL,true)); Commented May 22, 2013 at 11:00

5 Answers 5

5

Use array_filter before using array_keys and filter the array like this

$newArray = array_filter($a);

Then do

$sub_key    = array_keys($newArray);
$sub_values = array_values($newArray);
Sign up to request clarification or add additional context in comments.

3 Comments

So, false, '', array(), 0 are discarded as well, not just NULL?
Careful, this will also remove values like FALSE, empty strings, zeros, etc.
one has to define an own callback function as in the array_filter example.
2

You could use array_filter($a), but as one of the comments above pointed out, this would also filter out values like FALSE, empty strings, etc. So I'd use a foreach loop.

$new_array = array();

foreach ($a as $key => $value) {
    if (is_null($value) === false) {
        $new_array[$key] = $value;
    }
}

Comments

1

Please try this:

// Loop to find empty elements and  
// unset the empty elements 
foreach($array as $key => $value)          
    if(empty($value)) 
        unset($array[$key]); 

// Display the array elements         
foreach($array as $key => $value)          
    echo ($array[$key] . "<br>"); 

In you case you will replace $array with $a. This will work for null/empty key values.

Comments

1
$a = array("d1" => "data1", "d2" => NULL, "d3" => "data3");

$b = array_filter($a); // Not Null Values Array

$sub_key    = array_keys(array_filter($a));
$sub_values = array_values(array_filter($a));

echo "`".implode("`,`", $sub_key)."` <br/>";
echo "'".implode("','", $sub_values)."'";

Comments

0
$sub_key = array();
$sub_values = array();
foreach ($a as $key => $value) {
    if (!is_null($key) && !is_null($value)) { // you can also do is_empty() in stead of is_null() if you also wan't to avoid empty string
        $sub_key[] = $key;
        $sub_values[] = $value; // or use mysql_real_escape_string($value) if you are going to create a query with this! Otherwise you will create an SQL injection vulnerability here.
    }
}

// you can add if(count($sub_key)) here to only do the echoes, if there was at least 1 item in the array. Otherwise you will echo an empty ``
echo "`".implode("`,`", $sub_key)."`";
echo "'".implode("','", $sub_key)."'"; // don't you mean $sub_values here?

3 Comments

If you are going to use this to create queries, note it is better to parameterize your queries in stead of building your queries like this.
how? I want execute an INSERT. At the beginning I don't know, which colums get values,... stackoverflow.com/questions/60174/… give an example for PDO, and stackoverflow.com/questions/10884820/… gives an exapmle only for fixed columns. php.net/manual/de/mysqli.prepare.php has only selects, and only a single value added.
@JohnGarreth To be honest, I haven't yet written a query where the column names themselves are parameterized. And in the meanwhile, you are helped a bit further by using mysql_real_escape_string() as I advise in the comments. (If you are using mysqli_* functions, you should use mysqli_real_escape_string() in stead.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.