[关闭]
@fzbing 2015-06-08T05:15:19.000000Z 字数 8690 阅读 815

php 引用和for

php



下面两个函数有什么区别 ?

  1. private function tree($list, $p_key, &$re)
  2. {
  3. foreach ($list as $key => $val) {
  4. if ($val['parent_id'] == $p_key) {
  5. $re[] = $val;
  6. unset($list[$key]);
  7. $this->tree($list, $val['type_id'], $re);
  8. }
  9. }
  10. }
  1. private function tree(&$list, $p_key, &$re)
  2. {
  3. foreach ($list as $key => $val) {
  4. if ($val['parent_id'] == $p_key) {
  5. $re[] = $val;
  6. unset($list[$key]);
  7. $this->tree($list, $val['type_id'], $re);
  8. }
  9. }
  10. }

测试用例 1

  1. array (size=5)
  2. 0 =>
  3. array (size=5)
  4. 'type_id' => string '1' (length=1)
  5. 'parent_id' => null
  6. 'name' => string '每日一文' (length=12)
  7. 'create_time' => string '2015-06-07 08:12:39' (length=19)
  8. 'user_id' => string '1' (length=1)
  9. 1 =>
  10. array (size=5)
  11. 'type_id' => string '30' (length=2)
  12. 'parent_id' => string '31' (length=2)
  13. 'name' => string '生活' (length=6)
  14. 'create_time' => string '2015-06-07 10:02:38' (length=19)
  15. 'user_id' => string '1' (length=1)
  16. 2 =>
  17. array (size=5)
  18. 'type_id' => string '31' (length=2)
  19. 'parent_id' => string '1' (length=1)
  20. 'name' => string '在身边' (length=9)
  21. 'create_time' => string '2015-06-07 09:43:53' (length=19)
  22. 'user_id' => string '1' (length=1)
  23. 3 =>
  24. array (size=5)
  25. 'type_id' => string '32' (length=2)
  26. 'parent_id' => string '31' (length=2)
  27. 'name' => string '陪伴' (length=6)
  28. 'create_time' => string '2015-06-07 09:44:22' (length=19)
  29. 'user_id' => string '1' (length=1)
  30. 4 =>
  31. array (size=5)
  32. 'type_id' => string '34' (length=2)
  33. 'parent_id' => string '1' (length=1)
  34. 'name' => string '刻舟求剑' (length=12)
  35. 'create_time' => string '2015-06-07 22:54:00' (length=19)
  36. 'user_id' => string '1' (length=1)

1.非引用 结果

  1. array (size=5)
  2. 0 =>
  3. array (size=5)
  4. 'type_id' => string '1' (length=1)
  5. 'parent_id' => null
  6. 'name' => string '每日一文' (length=12)
  7. 'create_time' => string '2015-06-07 08:12:39' (length=19)
  8. 'user_id' => string '1' (length=1)
  9. 1 =>
  10. array (size=5)
  11. 'type_id' => string '31' (length=2)
  12. 'parent_id' => string '1' (length=1)
  13. 'name' => string '在身边' (length=9)
  14. 'create_time' => string '2015-06-08 00:04:31' (length=19)
  15. 'user_id' => string '1' (length=1)
  16. 2 =>
  17. array (size=5)
  18. 'type_id' => string '30' (length=2)
  19. 'parent_id' => string '31' (length=2)
  20. 'name' => string '生活' (length=6)
  21. 'create_time' => string '2015-06-08 00:04:19' (length=19)
  22. 'user_id' => string '1' (length=1)
  23. 3 =>
  24. array (size=5)
  25. 'type_id' => string '32' (length=2)
  26. 'parent_id' => string '31' (length=2)
  27. 'name' => string '陪伴' (length=6)
  28. 'create_time' => string '2015-06-07 09:44:22' (length=19)
  29. 'user_id' => string '1' (length=1)
  30. 4 =>
  31. array (size=5)
  32. 'type_id' => string '34' (length=2)
  33. 'parent_id' => string '1' (length=1)
  34. 'name' => string '刻舟求剑' (length=12)
  35. 'create_time' => string '2015-06-07 22:54:00' (length=19)
  36. 'user_id' => string '1' (length=1)

正确结果

  1. 1
  2. 31
  3. 30
  4. 32
  5. 34

2. 引用 结果

  1. array (size=4)
  2. 0 =>
  3. array (size=5)
  4. 'type_id' => string '1' (length=1)
  5. 'parent_id' => null
  6. 'name' => string '每日一文' (length=12)
  7. 'create_time' => string '2015-06-07 08:12:39' (length=19)
  8. 'user_id' => string '1' (length=1)
  9. 1 =>
  10. array (size=5)
  11. 'type_id' => string '31' (length=2)
  12. 'parent_id' => string '1' (length=1)
  13. 'name' => string '在身边' (length=9)
  14. 'create_time' => string '2015-06-08 00:04:31' (length=19)
  15. 'user_id' => string '1' (length=1)
  16. 2 =>
  17. array (size=5)
  18. 'type_id' => string '30' (length=2)
  19. 'parent_id' => string '31' (length=2)
  20. 'name' => string '生活' (length=6)
  21. 'create_time' => string '2015-06-08 00:04:19' (length=19)
  22. 'user_id' => string '1' (length=1)
  23. 3 =>
  24. array (size=5)
  25. 'type_id' => string '32' (length=2)
  26. 'parent_id' => string '31' (length=2)
  27. 'name' => string '陪伴' (length=6)
  28. 'create_time' => string '2015-06-07 09:44:22' (length=19)
  29. 'user_id' => string '1' (length=1)

测试用例2

  1. array (size=5)
  2. 0 =>
  3. array (size=5)
  4. 'type_id' => string '1' (length=1)
  5. 'parent_id' => null
  6. 'name' => string '每日一文' (length=12)
  7. 'create_time' => string '2015-06-07 08:12:39' (length=19)
  8. 'user_id' => string '1' (length=1)
  9. 1 =>
  10. array (size=5)
  11. 'type_id' => string '30' (length=2)
  12. 'parent_id' => string '1' (length=1)
  13. 'name' => string '生活' (length=6)
  14. 'create_time' => string '2015-06-08 00:21:35' (length=19)
  15. 'user_id' => string '1' (length=1)
  16. 2 =>
  17. array (size=5)
  18. 'type_id' => string '31' (length=2)
  19. 'parent_id' => string '30' (length=2)
  20. 'name' => string '在身边' (length=9)
  21. 'create_time' => string '2015-06-08 00:21:38' (length=19)
  22. 'user_id' => string '1' (length=1)
  23. 3 =>
  24. array (size=5)
  25. 'type_id' => string '32' (length=2)
  26. 'parent_id' => string '31' (length=2)
  27. 'name' => string '陪伴' (length=6)
  28. 'create_time' => string '2015-06-07 09:44:22' (length=19)
  29. 'user_id' => string '1' (length=1)
  30. 4 =>
  31. array (size=5)
  32. 'type_id' => string '34' (length=2)
  33. 'parent_id' => string '1' (length=1)
  34. 'name' => string '刻舟求剑' (length=12)
  35. 'create_time' => string '2015-06-07 22:54:00' (length=19)
  36. 'user_id' => string '1' (length=1)

1.非引用 结果

  1. array (size=5)
  2. 0 =>
  3. array (size=5)
  4. 'type_id' => string '1' (length=1)
  5. 'parent_id' => null
  6. 'name' => string '每日一文' (length=12)
  7. 'create_time' => string '2015-06-07 08:12:39' (length=19)
  8. 'user_id' => string '1' (length=1)
  9. 1 =>
  10. array (size=5)
  11. 'type_id' => string '30' (length=2)
  12. 'parent_id' => string '1' (length=1)
  13. 'name' => string '生活' (length=6)
  14. 'create_time' => string '2015-06-08 00:21:35' (length=19)
  15. 'user_id' => string '1' (length=1)
  16. 2 =>
  17. array (size=5)
  18. 'type_id' => string '31' (length=2)
  19. 'parent_id' => string '30' (length=2)
  20. 'name' => string '在身边' (length=9)
  21. 'create_time' => string '2015-06-08 00:21:38' (length=19)
  22. 'user_id' => string '1' (length=1)
  23. 3 =>
  24. array (size=5)
  25. 'type_id' => string '32' (length=2)
  26. 'parent_id' => string '31' (length=2)
  27. 'name' => string '陪伴' (length=6)
  28. 'create_time' => string '2015-06-07 09:44:22' (length=19)
  29. 'user_id' => string '1' (length=1)
  30. 4 =>
  31. array (size=5)
  32. 'type_id' => string '34' (length=2)
  33. 'parent_id' => string '1' (length=1)
  34. 'name' => string '刻舟求剑' (length=12)
  35. 'create_time' => string '2015-06-07 22:54:00' (length=19)
  36. 'user_id' => string '1' (length=1)
  1. 1
  2. 30
  3. 31
  4. 32
  5. 34

2.引用 结果

  1. array (size=4)
  2. 0 =>
  3. array (size=5)
  4. 'type_id' => string '1' (length=1)
  5. 'parent_id' => null
  6. 'name' => string '每日一文' (length=12)
  7. 'create_time' => string '2015-06-07 08:12:39' (length=19)
  8. 'user_id' => string '1' (length=1)
  9. 1 =>
  10. array (size=5)
  11. 'type_id' => string '30' (length=2)
  12. 'parent_id' => string '1' (length=1)
  13. 'name' => string '生活' (length=6)
  14. 'create_time' => string '2015-06-08 00:21:35' (length=19)
  15. 'user_id' => string '1' (length=1)
  16. 2 =>
  17. array (size=5)
  18. 'type_id' => string '31' (length=2)
  19. 'parent_id' => string '30' (length=2)
  20. 'name' => string '在身边' (length=9)
  21. 'create_time' => string '2015-06-08 00:21:38' (length=19)
  22. 'user_id' => string '1' (length=1)
  23. 3 =>
  24. array (size=5)
  25. 'type_id' => string '32' (length=2)
  26. 'parent_id' => string '31' (length=2)
  27. 'name' => string '陪伴' (length=6)
  28. 'create_time' => string '2015-06-07 09:44:22' (length=19)
  29. 'user_id' => string '1' (length=1)

测试用例3

  1. array (size=5)
  2. 0 =>
  3. array (size=5)
  4. 'type_id' => string '1' (length=1)
  5. 'parent_id' => string '34' (length=2)
  6. 'name' => string '每日一文' (length=12)
  7. 'create_time' => string '2015-06-08 00:27:55' (length=19)
  8. 'user_id' => string '1' (length=1)
  9. 1 =>
  10. array (size=5)
  11. 'type_id' => string '30' (length=2)
  12. 'parent_id' => string '1' (length=1)
  13. 'name' => string '生活' (length=6)
  14. 'create_time' => string '2015-06-08 00:21:35' (length=19)
  15. 'user_id' => string '1' (length=1)
  16. 2 =>
  17. array (size=5)
  18. 'type_id' => string '31' (length=2)
  19. 'parent_id' => string '30' (length=2)
  20. 'name' => string '在身边' (length=9)
  21. 'create_time' => string '2015-06-08 00:21:38' (length=19)
  22. 'user_id' => string '1' (length=1)
  23. 3 =>
  24. array (size=5)
  25. 'type_id' => string '32' (length=2)
  26. 'parent_id' => string '31' (length=2)
  27. 'name' => string '陪伴' (length=6)
  28. 'create_time' => string '2015-06-07 09:44:22' (length=19)
  29. 'user_id' => string '1' (length=1)
  30. 4 =>
  31. array (size=5)
  32. 'type_id' => string '34' (length=2)
  33. 'parent_id' => null
  34. 'name' => string '刻舟求剑' (length=12)
  35. 'create_time' => string '2015-06-08 00:27:17' (length=19)
  36. 'user_id' => string '1' (length=1)

1.非引用 结果

  1. array (size=5)
  2. 0 =>
  3. array (size=5)
  4. 'type_id' => string '34' (length=2)
  5. 'parent_id' => null
  6. 'name' => string '刻舟求剑' (length=12)
  7. 'create_time' => string '2015-06-08 00:27:17' (length=19)
  8. 'user_id' => string '1' (length=1)
  9. 1 =>
  10. array (size=5)
  11. 'type_id' => string '1' (length=1)
  12. 'parent_id' => string '34' (length=2)
  13. 'name' => string '每日一文' (length=12)
  14. 'create_time' => string '2015-06-08 00:27:55' (length=19)
  15. 'user_id' => string '1' (length=1)
  16. 2 =>
  17. array (size=5)
  18. 'type_id' => string '30' (length=2)
  19. 'parent_id' => string '1' (length=1)
  20. 'name' => string '生活' (length=6)
  21. 'create_time' => string '2015-06-08 00:21:35' (length=19)
  22. 'user_id' => string '1' (length=1)
  23. 3 =>
  24. array (size=5)
  25. 'type_id' => string '31' (length=2)
  26. 'parent_id' => string '30' (length=2)
  27. 'name' => string '在身边' (length=9)
  28. 'create_time' => string '2015-06-08 00:21:38' (length=19)
  29. 'user_id' => string '1' (length=1)
  30. 4 =>
  31. array (size=5)
  32. 'type_id' => string '32' (length=2)
  33. 'parent_id' => string '31' (length=2)
  34. 'name' => string '陪伴' (length=6)
  35. 'create_time' => string '2015-06-07 09:44:22' (length=19)
  36. 'user_id' => string '1' (length=1)

2.引用 结果

  1. array (size=5)
  2. 0 =>
  3. array (size=5)
  4. 'type_id' => string '34' (length=2)
  5. 'parent_id' => null
  6. 'name' => string '刻舟求剑' (length=12)
  7. 'create_time' => string '2015-06-08 00:27:17' (length=19)
  8. 'user_id' => string '1' (length=1)
  9. 1 =>
  10. array (size=5)
  11. 'type_id' => string '1' (length=1)
  12. 'parent_id' => string '34' (length=2)
  13. 'name' => string '每日一文' (length=12)
  14. 'create_time' => string '2015-06-08 00:27:55' (length=19)
  15. 'user_id' => string '1' (length=1)
  16. 2 =>
  17. array (size=5)
  18. 'type_id' => string '30' (length=2)
  19. 'parent_id' => string '1' (length=1)
  20. 'name' => string '生活' (length=6)
  21. 'create_time' => string '2015-06-08 00:21:35' (length=19)
  22. 'user_id' => string '1' (length=1)
  23. 3 =>
  24. array (size=5)
  25. 'type_id' => string '31' (length=2)
  26. 'parent_id' => string '30' (length=2)
  27. 'name' => string '在身边' (length=9)
  28. 'create_time' => string '2015-06-08 00:21:38' (length=19)
  29. 'user_id' => string '1' (length=1)
  30. 4 =>
  31. array (size=5)
  32. 'type_id' => string '32' (length=2)
  33. 'parent_id' => string '31' (length=2)
  34. 'name' => string '陪伴' (length=6)
  35. 'create_time' => string '2015-06-07 09:44:22' (length=19)
  36. 'user_id' => string '1' (length=1)
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注