0

I have twomultidimensional array in below format

$skillInfo = Array
(
    "Mechanical" => Array
        (
            "0" => 100
            "1" => 400
        )

    "Understanding" => Array
        (
            "0" => 200
        )

    "Application/Appreciation" => Array
        (
            "0" => 300
        )

);

$skillMaster = [
["skillID" => 1, "skillName" => "Mechanical"],
["skillID" => 2, "skillName" => "Understanding"],
["skillID" => 3, "skillName" => "Application/Appreciation"]
];

$skillMaster skillName is the same as $skillInfo keys, so we can replace $skillInfo keys from $skillMaster skillID.

Expected output

Array
(
    [1] => Array
        (
            [0] => 100
            [1] => 400
        )

    [2] => Array
        (
            [0] => 200
        )

    [3] => Array
        (
            [0] => 300
        )

    )

my code

foreach($skillInfo as $key1 => $val1){
    $skillInfo[$key1][$skillMaster[$val1["skillID"]]] = $skillMaster[$val1["skillID"]] ?? [];
}

above code is not working as expected output, kindly anyone help me out.

3
  • $val1 is a $skillInfo element, it doesn't have skillID. Commented Dec 10, 2020 at 6:07
  • skill name present $skillInfo array, i want to replace instead of name skillID Commented Dec 10, 2020 at 6:09
  • I know that's what you want to do. Why did you think $val1['skillID'] would work? Commented Dec 10, 2020 at 6:11

1 Answer 1

2
foreach($skillMaster as $item){
  if(array_key_exists($item['skillName'], $skillInfo)){
        $skillInfo[$item['skillID']] = $skillInfo[$item['skillName']];
        unset($skillInfo[$item['skillName']]);
   }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Including an explanation of the changes you made would be helpful for both the OP and any future readers.

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.