以大写字母显示第一个字母并忽略嵌套数组中的重复值?

时间:2012-12-13 05:33:00

标签: php

我试图在数组中显示值。第一个字母应该在CAPS中打印,并且应该跳过重复的值。这是我的代码。

$user =  array(array("name"=>"prusoth","language"=>array("english","tamil")),array("name"=>"venkat","    language"=>array("english","french")));
$names = array_keys($user);
$lang  = array_values($user);
foreach($lang as $value)
{
print_r (array_map('ucfirst', $value));
echo "<br>"."<br>";
print_r (array_unique($value));
}

上面的代码输出是这样的。

Warning: ucfirst() expects parameter 1 to be string, array given in /home/aspire/public_html/Array.php on line 8
Array ( [name] => Prusoth [language] => ) 

Array ( [name] => prusoth [language] => Array ( [0] => english [1] => tamil ) ) 
Warning: ucfirst() expects parameter 1 to be string, array given in /home/aspire/public_html/Array.php on line 8
Array ( [name] => Venkat [language] => ) 

Array ( [name] => venkat [language] => Array ( [0] => english [1] => french ) )

预期产出:

name = (Prusoth,Venkat)
Language = (english,tamil,french)

4 个答案:

答案 0 :(得分:1)

  

我认为你需要像这样的输出。试试这个..

$user =  array(
array("name"=>"prusoth","language"=>array("english","tamil")),
array("name"=>"venkat","language"=>array("english","french")));
$lang  = array_values($user);
$username=array();
$langname=array();
$arrUser=array();
foreach($lang as $value)
{
    foreach($value as $val)
    {
        if(!empty($val))
        {
            if(!is_array($val))
            {
                $username[]=ucfirst($val);
            }
            else
            {
                foreach($val as $langs)
                {
                    $langname[]=$langs; 
                }
            }
            $arrUser['name']=array_unique($username);
            $arrUser['language']=array_unique($langname);
        }
    }
}
print_r($arrUser);

答案 1 :(得分:0)

试试这个;)

<?php

$user =  array(
    array(
        "name"=>"prusoth",
        "language"=>array("english","tamil")
    ),
    array(
        "name"=>"venkat",
        "language"=>array("english","french")
    )
);

$result = array(
    'name' => array(),
    'language' => array()
);

foreach($user as $value){
    $result['name'][ $value['name'] ] = ucfirst($value['name']);

    $result['language'] = array_merge($result['language'], array_combine($value['language'], $value['language']));
}

print_r($result);
echo PHP_EOL;

echo 'Name = ('.implode(',', $result['name']).')';

echo PHP_EOL;

echo 'Language = ('.implode(',', $result['language']).')';

答案 2 :(得分:0)

Try This :)
$resultarray=array();
$user = array(array("name"=>"prusoth","language"=>array("english","tamil")),array("name"=>"venkat","language"=>array("english","french")));
foreach($user as $MainArray)
{
    $Lang_array[]=$MainArray['language'];
    $Name_array[]=$MainArray['name'];

}
foreach($Lang_array as $language)
{
    foreach($language as $values)
    {
      $languages[]=$values;
    }


}
$resultarray['name']=array_map('ucfirst', $Name_array);
$resultarray['language']=array_unique($languages);
die(print_r($resultarray));

答案 3 :(得分:-1)

$user =  array(array("name"=>"prusoth","language"=>array("english","tamil")),array("name"=>"venkat","language"=>array("english","french")));

for ($i = 0; $i < count($user); $i++){

foreach ($user[$i] as $key => $value){

if (!is_array($user[$i][$key])) $name[] .= ucfirst(strtolower($user[$i][$key]))." ";

else foreach ($user[$i][$key] as $val){ $array[] = $val;}}}

echo 'name = ('.implode(',', $name).')<br>';

$language = array_unique($array);

echo 'Language = ('.implode(',', $language).')';