0

I have two arrays

$arrayOne = ( [0] => 4892 [1] => 98508 [2] => 7834 [3] => 47826 )
$arrayTwo = ( [1] => Car [2] => Computer )

Notice the elements of arrayTwo does not start at 0, but which is what i want because it will be used to pair with arrayOne, ie. Car matches with 98508.

I want to populate the second array where there are no entries with a string for example arrayTwo output:

$arrayTwo = ([0] => its empty [1] => Car [2] => Computer [3] => its empty

How can i achieve this desired output?

4
  • $arrayTwo = [null,'Car','Computer',null]; doesn't work for you? Commented Feb 22, 2018 at 18:52
  • Get the existing keys from arrayOne (php.net/manual/en/function.array-keys.php). Loop on that. If arrayTwo does not have the index, add it = empty. If it`s there do nothing. Try it out, post some code here if you are stuck. Or loop using foreach. Commented Feb 22, 2018 at 18:53
  • @IncredibleHat i would like to input a string where there are no entries Commented Feb 22, 2018 at 18:54
  • FYI, while providing answers is nice, getting him to work it out is better, IMHO. Commented Feb 22, 2018 at 18:55

2 Answers 2

1

Loop the first and check for the key. If it doesn't exist, set it:

foreach($arrayOne as $key => $val) {
    if(!isset($arrayTwo[$key])) { $arrayTwo[$key] = 'its empty'; }
}
Sign up to request clarification or add additional context in comments.

Comments

0
foreach ($arrayOne as $key => $value){

    if (!array_key_exists($key, $arrayTwo)){
        $arrayTwo[$key] = 'its empty';
    }

}

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.