php中array嵌套,PHP中嵌套多維數組的多維數組 - Multidimensional array to a nested multidimensional array in PHP - 开发...

I have an array like so:

我有一個像這樣的數組:

sid sname did dname

1 Basketball 1 Mini

1 Basketball 3 Cadet

2 Baseball 8 Little League

2 Baseball 6 Junior League

1 Basketball 5 Masters

I was trying to get this and transform it to a nested array like so:

我試圖得到它並將其轉換為嵌套數組,如下所示:

array('Basketball' => array(

'id' => 1,

'divisions' => array(

1 => 'Mini',

3 => 'Cadet',

5 => 'Masters'

)

),

'Baseball' => array(

'id' => 2,

'divisions' => array(

8 => 'Little League',

6 => 'Junior League'

)

)

);

And I am using this foreach loop which is not working, it replaces each division entry so I'm left with only one division entry which is the last entry.

我正在使用這個不起作用的foreach循環,它取代了每個分區條目,所以我只剩下一個分區條目,這是最后一個條目。

$result = '';

foreach($row as $r)

{

$result[$r['sname']] = array('id' => $r['sid'], 'divisions' => array($r['did'] => $r['dname']));

}

This foreach loop gives me this result:

這個foreach循環給了我這個結果:

array('Basketball' => array(

'id' => 1,

'divisions' => array(

5 => 'Masters'

)

),

'Baseball' => array(

'id' => 2,

'divisions' => array(

6 => 'Junior League'

)

)

);

I don't understand what is wrong here.. can anybody help me out here?

我不明白這里有什么問題..有人可以幫幫我嗎?

5 个解决方案

#1

3

The problem is that you redefine $result[$r['sname']] each time. You only need to define it if it is not already defined.

問題是你每次都重新定義$ result [$ r ['sname']]。如果尚未定義,則只需定義它。

$result = array(); // initialize this to an appropriate type!

foreach($row as $r)

{

if(!isset($result[$r['sname']]))

{

$result[$r['sname']] = array('id' => $r['sid'], 'divisions' => array($r['did'] => $r['dname']));

continue;

}

$result[$r['sname']]['divisions'][$r['did']] = $r['dname'];

}

#2

1

$result = array();

foreach($row as $r)

{

$result[$r['sname']]['id'] = $r['sid'];

$result[$r['sname']]['divisions'][$r['did']] = $r['dname'];

}

#3

0

Check for existence of the key with isset() or array_key_exists() beforehand so that it doesn't get rewritten.

使用isset()或array_key_exists()檢查是否存在密鑰,以便不會重寫。

#4

0

i think you want:

我想你想要:

$result[$r['sname']][] = array('id' => $r['sid'], 'divisions' => array($r['did'] => $r['dname']));

#5

0

$result = array();

foreach($row as $r)

{

if(isset($result[$r['sname']]))

{

array_push($result[$r['sname']], array('id' => $r['sid'], 'divisions' => array($r['did'] => $r['dname'])));

}

else

{

$result[$r['sname']] = array('id' => $r['sid'], 'divisions' => array($r['did'] => $r['dname']))

}

}

What you are currently doing is overwriting the entry for $result[$r['sname']], so it will only ever have one array in it.

你目前正在做的是覆蓋$ result [$ r ['sname']]的條目,所以它只會有一個數組。

I use the if because $array[] = is quicker than array_push see php docs

我使用if因為$ array [] =比array_push更快看到php文檔

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值