-1

我在 PHP 中工作。

我有一个数组。我需要创建像([CN] => Chinese)这种格式的语言数组。

我的数组响应如下。

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
        )

)

我需要转换这种类型的数组

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

我怎样才能做到这一点?

4

4 回答 4

2

代码:

<?php

$newArray = array();

foreach($yourArray as $key => $items) {
    $newArray[$items->language_code] = $items->language_name;
}

die('<pre>' . print_r($newArray, true) . '</pre>');

而已。

于 2013-07-20T10:29:34.367 回答
0

此代码工作正常

foreach($objects as $object)
{
    $newArray[$object->language_code] = $object->language_name;
}
echo "<pre>";
print_r($newArray);
echo "</pre>";
于 2013-07-20T10:41:27.803 回答
0

你可以试试这个:

foreach($objects as $object)
{
    $newArray[$object->language_code] = $object->language_name;
}
print_r($newArray);
于 2013-07-20T10:32:37.383 回答
0

使用 PHP 的foreach

function convert($array){
    $ret = array();

    foreach($array as $obj){
        $ret[$obj->language_code] = $obj->language_name;
    }

    return $ret;
}
于 2013-07-20T10:31:33.203 回答