base arr - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of base arr.php

  1. <?php
  2. $users = [
  3.     [
  4.         "id" => 1,
  5.         "name" => "Leanne Graham",
  6.         "username" => "Bret",
  7.         "email" => "[email protected]",
  8.         "address" => [
  9.             "street" => "Kulas Light",
  10.             "suite" => "Apt. 556",
  11.             "city" => "Gwenborough",
  12.             "zipcode" => "92998-3874",
  13.             "geo" => [
  14.                 "lat" => "-37.3159",
  15.                 "lng" => "81.1496"
  16.             ]
  17.         ],
  18.         "phone" => "1-770-736-8031 x56442",
  19.         "website" => "hildegard.org",
  20.         "company" => [
  21.             "name" => "Romaguera-Crona",
  22.             "catchPhrase" => "Multi-layered client-server neural-net",
  23.             "bs" => "harness real-time e-markets"
  24.         ]
  25.     ],
  26.     [
  27.         "id" => 2,
  28.         "name" => "Ervin Howell",
  29.         "username" => "Antonette",
  30.         "email" => "[email protected]",
  31.         "address" => [
  32.             "street" => "Victor Plains",
  33.             "suite" => "Suite 879",
  34.             "city" => "Wisokyburgh",
  35.             "zipcode" => "90566-7771",
  36.             "geo" => [
  37.                 "lat" => "-43.9509",
  38.                 "lng" => "-34.4616"
  39.             ]
  40.         ],
  41.         "phone" => "010-692-6593 x09125",
  42.         "website" => "anastasia.net",
  43.         "company" => [
  44.             "name" => "eckow-Crist",
  45.             "catchPhrase" => "Proactive didactic contingency",
  46.             "bs" => "synergize scalable supply-chains"
  47.         ]
  48.     ],
  49.  
  50. ];
  51. $posts = [
  52.     [
  53.         "user_id" => 1,
  54.         "id" => 1,
  55.         "title" => "delectus aut autem",
  56.         "completed" => false
  57.     ],
  58.     [
  59.         "user_id" => 2,
  60.         "id" => 21,
  61.         "title" => "suscipit repellat esse quibusdam voluptatem incidunt",
  62.         "completed" => true
  63.     ],
  64.     [
  65.         "user_id" => 2,
  66.         "id" => 22,
  67.         "title" => "voluptas quo tenetur perspiciatis explicabo natus",
  68.         "completed" => false
  69.     ]
  70.  
  71. ];
  72.  
  73. foreach ($users as $key => $user) {
  74.     foreach ($posts as $post) {
  75.         if ($user['id'] == $post['user_id']) {
  76.             $users[$key]['posts'][] = $post;
  77.         }
  78.  
  79.     }
  80. }
  81.  
  82.  
  83. ?>
  84.  
  85.  
  86. <?php foreach ($users as $user): ?>
  87.     <div style="border: 1px solid;margin-bottom: 1em">
  88.         <h1><?= $user['username'] ?>  <?= $user['email'] ?>   <?= $user['phone'] ?></h1>
  89.         <h5>posts</h5>
  90.         <?php foreach ($user['posts'] as $post): ?>
  91.             <ul>
  92.                 <li style="margin-bottom: 1em;box-shadow: 1px 1px 5px;padding: 0.4em;">
  93.                     <input type="checkbox" <?= $post['completed'] ? 'checked' : '' ?>>
  94.                     <h3 class="<?= $post['completed'] ? 'completed' : '' ?>"><?= $post['title'] ?></h3>
  95.                 </li>
  96.             </ul>
  97.         <?php endforeach; ?>
  98.     </div>
  99. <?php endforeach; ?>
  100.  
File Description
  • base arr
  • PHP Code
  • 11 Sep-2019
  • 2.67 Kb
You can Share it: