0

Today i found new stack ( maybe stack with me ). I'm working for a new simple PHP framework to learning, but when a need make a array of data to add to database. This array make from 2 array : one is Default array and one left is new request array. i will demo :

`

array $default = (
[cat] = '0',
[dog] = '0',
[bird] = '0'
)

array $request = (
[cat] = '10',
[dog] = '12',

[someanimal] = '100'
)

`

now i want make a new array with result like this: array $new = ([cat]='10', [dog]='12', [bird]='0'). In PHP may be have some command for this ? or we need make a custom function for?

Thanks everyone for help :).

3 Answers 3

3

Try

$result = $default;
foreach ($request as $key=>$value) {
  if (isset($result[$key])) {
    $result[$key] = $value;
  }  
}

Or

$result = array_intersect_key(array_merge($default, $request), $default)
Sign up to request clarification or add additional context in comments.

Comments

0

Are you after something like this:

$finalArray = array_merge($default, $request); 

Comments

0

Try $result = array_merge($array1, $array2);

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

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.