-1

配列に値を表示しようとしました。最初の文字は 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

4 に答える 4

1

このような出力が必要だと思います.Try this.

$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);
于 2012-12-13T06:52:03.210 に答える
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));
于 2012-12-13T07:44:51.803 に答える
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']).')';
于 2012-12-13T05:47:58.503 に答える
-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).')';
于 2012-12-13T06:54:39.470 に答える