phparraysobjectarray-column

Convert two columns in an array of objects into an associative array


I am working in PHP.

I have one array. i need to create language array like ([CN] => Chinese) this format.

My array response is given below.

Array
(
    [0] => stdClass Object
        (
            [language_name] => Chinese
            [language_code] => CN
        )

    [1] => stdClass Object
        (
            [language_name] => English
            [language_code] => EN
        )

    [2] => stdClass Object
        (
            [language_name] => Korea
            [language_code] => KO
        )

    [3] => stdClass Object
        (
            [language_name] => Vietnamese
            [language_code] => VN
        )

)

I need to convert this type of array

Array(
      [CN]=>Chinese
      [EN]=>English
      [KO]=>Korea
      [VN]=>Vietnamese
)

How can I do this?


Solution

  • Code:

    <?php
    
    $newArray = array();
    
    foreach($yourArray as $key => $items) {
        $newArray[$items->language_code] = $items->language_name;
    }
    
    die('<pre>' . print_r($newArray, true) . '</pre>');
    

    That's it.