1
<?php
$x = array(
       "C_Card_ID" => array(
                              "dbName"=>"CardID",
                              "type"=>"disabled",
                              "key"=>"primary"
                          ),
       "C_Payroll_ID" => array(
                              "dbName"=> "PayrollID",
                              "key"=>"unique"
                          ),
       "C_First_Name" => array("dbName"=>"FirstName")
?>

I want keys of $x which has "key" index in its second array. In simple words, i need C_Card_ID and C_Payroll_ID as an output in array, so later i will implode them.

Required output Sample : Array("C_Card_ID","C_Payroll_ID")

Please don't use Loop algo. I need to use some build-in function.

3 Answers 3

2

You can use array_filter:

syntax is:

$filtered_array = array_keys(array_filter($x, function($a){ return isset($a['key']); }));
Sign up to request clarification or add additional context in comments.

Comments

2

This should do:

$result = array_keys(array_filter($x, function($arr){
  return array_key_exists('key', $arr);
}));

2 Comments

Warning: array_key_exists() expects parameter 2 to be array, string given in xx.php on line xx
instead of array_key_exists('key', $arr); .... isset($arr['key']) work fine for all string and array. nice answer, thanks
2

Try to use array_slice() like,

<?php
    $x = array(
           "C_Card_ID" => array(
                                  "dbName"=>"CardID",
                                  "type"=>"disabled","key"=>"primary"
                              ),
           "C_Payroll_ID" => array(
                                  "dbName"=> "PayrollID",
                                  "key"=>"unique"
                              ),
           "C_First_Name" => array("dbName"=>"FirstName"));
    print_r(array_slice(array_keys($x),0,2));
    //Outputs
    //Array ( [0] => C_Card_ID [1] => C_Payroll_ID ) 
?>

Tested on http://writecodeonline.com/php/

1 Comment

correct but i am using associative array and my indexing is not fixed at 0,1 or 2 etc. however helpful

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.