0

I have an array with some values as below:

$array1 =  [
    0 => "year",
    1 => "month"
];

I make a loop to this array and check if its value exist in following array.

array:3 [
  0 => array:3 [
    "name" => "year"
    "mandatory" => true
    "type" => "integer"
  ]
  1 => array:3 [
    "name" => "month"
    "mandatory" => true
    "type" => "integer"
  ]
  2 => array:3 [
    "name" => "id"
    "mandatory" => false
    "type" => "integer"
  ]
]

If if exists then I have to return array something like this

array[
   "year" => [
        "mandatory" => true
        "type" => "integer"
   ],
  "month" => [
        "mandatory" => true
        "type" => "integer"
   ],
]

This is the code so far I have tried.

$keys = array_keys($params);
$fields = $config["fields"];

$finalArr = array();
foreach($keys as $key) {
    if(in_array($key, array_column($fields, "name"))) {
        $finalArr[$key] = array();
    }
    $finalArr[$key][] = $fields;
}

But this returns all the records.

Can anybody help me.

3
  • All 3 items from the first array are all in the second, so why are you expecting only 1 element? Doesn't make any sense.. Commented Jun 25, 2019 at 8:49
  • I have updated the question Commented Jun 25, 2019 at 8:53
  • Later I can use this finalArray where I can simply check by the key " year" and "month" Commented Jun 25, 2019 at 8:55

1 Answer 1

1

Iterate over your $fields and check if field's name exists in $keys:

foreach ($fields as $field) {
    if (in_array($field['name'], $keys)) {
        $finalArr[$field['name']] = $field;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.