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?

John Garreth
  • 1,112
  • 2
  • 10
  • 17

5 Answers5

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);
chandresh_cool
  • 11,753
  • 3
  • 30
  • 45
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;
    }
}
Terry Harvey
  • 840
  • 6
  • 12
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.

Faisal
  • 1,907
  • 1
  • 22
  • 29
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)."'";
qɐʇǝɥɐW
  • 347
  • 3
  • 17
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?
nl-x
  • 11,762
  • 7
  • 33
  • 61
  • 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. – nl-x May 22 '13 at 11:22
  • how? I want execute an INSERT. At the beginning I don't know, which colums get values,... http://stackoverflow.com/questions/60174/how-to-prevent-sql-injection-in-php give an example for PDO, and http://stackoverflow.com/questions/10884820/mysqli-insert-into-prepare-error gives an exapmle only for fixed columns. http://php.net/manual/de/mysqli.prepare.php has only selects, and only a single value added. – John Garreth May 22 '13 at 11:45
  • @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.) – nl-x May 22 '13 at 11:53