我在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
)
我该怎么做?
答案 0 :(得分:2)
代码:
<?php
$newArray = array();
foreach($yourArray as $key => $items) {
$newArray[$items->language_code] = $items->language_name;
}
die('<pre>' . print_r($newArray, true) . '</pre>');
就是这样。
答案 1 :(得分:0)
使用PHP的foreach
:
function convert($array){
$ret = array();
foreach($array as $obj){
$ret[$obj->language_code] = $obj->language_name;
}
return $ret;
}
答案 2 :(得分:0)
foreach($objects as $object)
{
$newArray[$object->language_code] = $object->language_name;
}
print_r($newArray);
答案 3 :(得分:0)
此代码工作正常
foreach($objects as $object)
{
$newArray[$object->language_code] = $object->language_name;
}
echo "<pre>";
print_r($newArray);
echo "</pre>";