如果键相同,如何将值追加到数组中

发布于 2024-11-09 10:53:08 字数 1254 浏览 0 评论 0原文

我有一个关联数组

[name]->user1,
[class_code]->bsc,
[name]->user2,
[class_code]->msc,
[name]->user1,
[class_code]->mca

,现在,我想要结果为

[name]->user1,
[class_code]->bsc,
mca
[name]->user2,
[class_code]->msc

“如果名称相同,则将 class_code 附加到第一个数组”。我该怎么做呢? 我的数组

 [1] =stdClass Object
        ([class_code] =Maths
            [userid] =365
            [avatar] =default.jpg
            [username] =user2
        )

    [2] =stdClass Object
        (
            [class_code] =Maths
            [userid] = 364
            [avatar] =default.jpg
            [username] =user1
        )

 [3] =stdClass Object
        (
            [class_code] =MCA
            [userid] =364
            [avatar] =default.jpg
            [username] =user1
        )

    [4] =stdClass Object
        (
            [class_code] =MCA
            [userid] =365
            [avatar] =default.jpg
            [username] =user2
        ) 

现在是,在显示结果时我想显示前的记录。 user2 as

username:user2
avatar
classcode :MCA,Maths 

而不是

username:user2
avatar
classcode :MCA
username:user2
avatar
classcode :Maths 

我该怎么做?

I have an associative array as

[name]->user1,
[class_code]->bsc,
[name]->user2,
[class_code]->msc,
[name]->user1,
[class_code]->mca

Now, i want result as

[name]->user1,
[class_code]->bsc,
mca
[name]->user2,
[class_code]->msc

Means if name is same then append the class_code to first one. How should I do that?
My array is

 [1] =stdClass Object
        ([class_code] =Maths
            [userid] =365
            [avatar] =default.jpg
            [username] =user2
        )

    [2] =stdClass Object
        (
            [class_code] =Maths
            [userid] = 364
            [avatar] =default.jpg
            [username] =user1
        )

 [3] =stdClass Object
        (
            [class_code] =MCA
            [userid] =364
            [avatar] =default.jpg
            [username] =user1
        )

    [4] =stdClass Object
        (
            [class_code] =MCA
            [userid] =365
            [avatar] =default.jpg
            [username] =user2
        ) 

Now, while displaying result i want to display records of for ex. user2 as

username:user2
avatar
classcode :MCA,Maths 

and not

username:user2
avatar
classcode :MCA
username:user2
avatar
classcode :Maths 

how can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

苍暮颜 2024-11-16 10:53:08

不能有两个值具有相同的键。否则,如果您要求 $myarray['name'],php 将不知道该给您什么。

您可以改用嵌套数组。

例如

[0]-> 
  [name]->user1,
  [class_code]->bsc,mca,
[1]->
  [name]->user2,
  [class_code]->msc

或者你可以做这样的事情

[user1]->bsc, mca
[user2]->msc

You can't have two values with the same key. Otherwise php wouldn't know what to give you if you asked for $myarray['name'].

You could use nested arrays instead.

eg

[0]-> 
  [name]->user1,
  [class_code]->bsc,mca,
[1]->
  [name]->user2,
  [class_code]->msc

or you could do something like this

[user1]->bsc, mca
[user2]->msc
月下客 2024-11-16 10:53:08

如果你想在那里有两个值,你应该考虑在那里使用一个数组

array(
   "name"=>"user1,
   "class_code"=>"array('mca','bsc'),
   "name"=>"user2,
   "class_code"=>"msc"
);

if you want to have both values in there, you should consider using an array in there

array(
   "name"=>"user1,
   "class_code"=>"array('mca','bsc'),
   "name"=>"user2,
   "class_code"=>"msc"
);
扬花落满肩 2024-11-16 10:53:08

如果[class_code]必须包含字符串,你可以尝试

$array[$key] .= $value;

if [class_code] must contain a string, you can try

$array[$key] .= $value;
提赋 2024-11-16 10:53:08

您可以使整个数组成为多维数组:

$users = array(
   0 = array(
       "name"=>"user1",
       "class_code"=>"bsc"
   ),
   1 = array(
       "name"=>"user2",
       "class_code"=>"msc"
   )
);

检索值:

$users[1][name]              // Returns 'user2'
$users[0][class_code]        // Returns 'bsc'

循环访问值:

foreach($users as $user => $attributes) {
    echo "$user: $attributes[name] ($attributes[class_code])<br />";
}

/*
Prints:
    0: user1 (bsc)
    1: user2 (msc)
*/

You can make the whole array multidimensional:

$users = array(
   0 = array(
       "name"=>"user1",
       "class_code"=>"bsc"
   ),
   1 = array(
       "name"=>"user2",
       "class_code"=>"msc"
   )
);

Retrieving a value:

$users[1][name]              // Returns 'user2'
$users[0][class_code]        // Returns 'bsc'

Cycling through values:

foreach($users as $user => $attributes) {
    echo "$user: $attributes[name] ($attributes[class_code])<br />";
}

/*
Prints:
    0: user1 (bsc)
    1: user2 (msc)
*/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文