PHP_multi_dimensional_arrays

آرایه ها در پی اچ پی

در این پست با فرمت های مختلفی آرایه در پی اچ پی به ویژه آرایه چند بعدی بحث خواهم کرد.

آرایه معمولی در PHP:

  1. ";
  2. }
  3.  
  4. /* Second method to create array. */
  5. $numbers[0] = "one";
  6. $numbers[1] = "two";
  7. $numbers[2] = "three";
  8. $numbers[3] = "four";
  9. $numbers[4] = "five";
  10.  
  11. foreach( $numbers as $value ) {
  12. echo "Value is $value
  13. ";
  14. }
  15. ?>

آرایه های انجمنی(Associative Arrays):

  1. <html>
  2. <body>
  3.  
  4. <?php
  5. /* First method to associate create array. */
  6. $salaries = array("mohammad" => 2000, "qadir" => 1000, "zara" => 500);
  7.  
  8. echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />";
  9. echo "Salary of qadir is ". $salaries['qadir']. "<br />";
  10. echo "Salary of zara is ". $salaries['zara']. "<br />";
  11.  
  12. /* Second method to create array. */
  13. $salaries['mohammad'] = "high";
  14. $salaries['qadir'] = "medium";
  15. $salaries['zara'] = "low";
  16.  
  17. echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />";
  18. echo "Salary of qadir is ". $salaries['qadir']. "<br />";
  19. echo "Salary of zara is ". $salaries['zara']. "<br />";
  20. ?>
  21.  
  22. </body>
  23. </html>

آرایه های چند بعدی:

  1. <html>
  2. <body>
  3.  
  4. <?php
  5. $marks = array(
  6. "mohammad" => array (
  7. "physics" => 35,
  8. "maths" => 30,
  9. "chemistry" => 39
  10. ),
  11.  
  12. "qadir" => array (
  13. "physics" => 30,
  14. "maths" => 32,
  15. "chemistry" => 29
  16. ),
  17.  
  18. "zara" => array (
  19. "physics" => 31,
  20. "maths" => 22,
  21. "chemistry" => 39
  22. )
  23. );
  24.  
  25. /* Accessing multi-dimensional array values */
  26. echo "Marks for mohammad in physics : " ;
  27. echo $marks['mohammad']['physics'] . "<br />";
  28.  
  29. echo "Marks for qadir in maths : ";
  30. echo $marks['qadir']['maths'] . "<br />";
  31.  
  32. echo "Marks for zara in chemistry : " ;
  33. echo $marks['zara']['chemistry'] . "<br />";
  34. ?>
  35.  
  36. </body>
  37. </html>

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *