title: Trying to access array offset on value of type null & isset problem in API call loop - 504 Gateway Time-out server
body: <p>I am building cron job with API calls in loop for DB entries and Have performance issues.</p>
  <p>Particularly in this part:</p>
  <pre><code>    if (!empty($sudCode) &amp;&amp; !empty($sudBroj) &amp;&amp; isset($sudCode) &amp;&amp; isset($sudBroj)) {
          // echo $sudCode . &quot;&lt;br&gt;&quot;;
          // echo $sudBroj . &quot;&lt;br&gt;&quot;;
          $epredmet = ePredmeti($sudCode, $sudBroj);
  
          // print_r($epredmet);
          // echo &quot;&lt;br&gt;&quot;;
  
          if (isset($epredmet[&quot;data&quot;][&quot;prvi&quot;][&quot;lastUpdateTime&quot;])) {
              $lastUpdateTime = $epredmet[&quot;data&quot;][&quot;prvi&quot;][&quot;lastUpdateTime&quot;];
              $dateTime  = str_replace(&quot;T&quot;, &quot; &quot;, $lastUpdateTime);
              echo $nas . &quot; - &quot; . $dateTime . &quot;&lt;br&gt;&quot;;
          }
      }
  </code></pre>
  <p>on line:</p>
  <pre><code>if (isset($epredmet[&quot;data&quot;][&quot;prvi&quot;][&quot;lastUpdateTime&quot;])) {
  </code></pre>
  <p>I have few Databases and on one when this line is reached sever goes to <code>504 Gateway Time-out</code> after 2 minutes.</p>
  <p>Hosting company said  that it goes in timeout because Apache web server waits for PHP parser to process data, what ever that means.</p>
  <p><strong>What is strange, is if I leave out that if check i script finishes and I get results but with Notice</strong>: <code>Trying to access array offset on value of type null in</code></p>
  <p>because I expect that <code>$epredmet</code> after API call looks like this:</p>
  <pre><code>- array(1) { [&quot;data&quot;]=&gt; array(1) { [&quot;prvi&quot;]=&gt; NULL } } // case not found
  - array(1) { [&quot;data&quot;]=&gt; array(1) { [&quot;prvi&quot;]=&gt; array(1) { [&quot;lastUpdateTime&quot;]=&gt; NULL } } }  // case found but lastUpdateTime is not set, null
  - array(1) { [&quot;data&quot;]=&gt; array(1) { [&quot;prvi&quot;]=&gt; array(1) { [&quot;lastUpdateTime&quot;]=&gt; string(23) &quot;2021-06-14T22:51:22.171&quot; } } }   // case found and lastUpdateTime is set
  </code></pre>
  <p>So what I need to do is filter out just last case where <code>lastUpdateTime</code> is set, and all that I read is suggesting to solve it with <code>isset</code> but that breaks my script for some reason.</p>
  <p>PHP V 7.4</p>
  <p>Please advise.</p>
  <p>Im attaching full script in case someone notices problem somewhere else:</p>
  <pre><code>function eSudovi()
  {
      $endpoint = &quot;xxx&quot;;
      $qry = '{&quot;query&quot;:&quot;query{sudovi {id, sudNaziv}}&quot;}';
  
      $headers = array();
      $headers[] = 'Content-Type: application/json';
  
      $ch = curl_init();
  
      curl_setopt($ch, CURLOPT_URL, $endpoint);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $qry);
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  
      $result = curl_exec($ch);
  
      curl_close($ch);
      return json_decode($result, true);
  }
  
  
  $eSudovi = eSudovi()[&quot;data&quot;][&quot;sudovi&quot;];
  
  function findSudCode($val, $eSudovi)
  {
      foreach ($eSudovi  as $key =&gt; $value) {
          if ($value[&quot;sudNaziv&quot;] == $val) {
              return $value[&quot;id&quot;];
          }
      }
  }
  
  
  
  function ePredmeti($sud, $pred)
  {
      $endpoint = &quot;xxx&quot;;
      $qry = '{&quot;query&quot;:&quot;query{ prvi:predmet(sud: ' . $sud . ', oznakaBroj: \&quot;' . $pred . '\&quot;) {lastUpdateTime}}&quot;}';
  
      $headers = array();
      $headers[] = 'Content-Type: application/json';
  
      $ch = curl_init();
  
      curl_setopt($ch, CURLOPT_URL, $endpoint);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $qry);
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  
      $result = curl_exec($ch);
      curl_close($ch);
      return json_decode($result, true);
  }
  
  
  
  
  
  
  $results = mysqli_query($con, &quot;
                  SELECT DISTINCT predf_nas_br, predf_odv,  predf_SUD, predf_SUDBROJ
                  FROM PREDMETIFView 
                  WHERE predf_SUD &lt;&gt; '' AND predf_SUDBROJ &lt;&gt; '' AND predf_SUDBROJ NOT LIKE '% %'
                  UNION ALL   
                  SELECT DISTINCT predp_nas_br, predp_odv,  predp_SUD, predp_SUDBROJ 
                  FROM PREDMETIPView 
                  WHERE predp_SUD &lt;&gt; '' AND predp_SUDBROJ &lt;&gt; '' AND predp_SUDBROJ NOT LIKE '% %'
                  ;&quot;);
  while ($row = $results-&gt;fetch_assoc()) {
      foreach ($row as $key =&gt; $value) {
          if ($key == &quot;predf_nas_br&quot;) {
              $nas = $value;
          }
  
          if ($key == &quot;predf_SUD&quot;) {
              $sud = trim($value);
              if (!empty($sud) &amp;&amp; isset($sud)) {
                  $sudCode = findSudCode($sud, $eSudovi);
              }
          };
  
          if ($key == &quot;predf_SUDBROJ&quot;) {
              $sudBroj = trim($value);
          };
         
  
  
          if (!empty($sudCode) &amp;&amp; !empty($sudBroj) &amp;&amp; isset($sudCode) &amp;&amp; isset($sudBroj)) {
              // echo $sudCode . &quot;&lt;br&gt;&quot;;
              // echo $sudBroj . &quot;&lt;br&gt;&quot;;
              $epredmet = ePredmeti($sudCode, $sudBroj);
  
              print_r($epredmet);
              echo &quot;&lt;br&gt;&quot;;
  
              if (isset($epredmet[&quot;data&quot;][&quot;prvi&quot;][&quot;lastUpdateTime&quot;])) {
                  $lastUpdateTime = $epredmet[&quot;data&quot;][&quot;prvi&quot;][&quot;lastUpdateTime&quot;];
                  $dateTime  = str_replace(&quot;T&quot;, &quot; &quot;, $lastUpdateTime);
                  echo $nas . &quot; - &quot; . $dateTime . &quot;&lt;br&gt;&quot;;
              }
          }
      }
  };
  
  
  // preg_match('/\s/', $sudBroj)
  </code></pre>
  <p><strong>Edit:</strong></p>
  <p>I also tried this:</p>
  <pre><code>    if (isset($epredmet[&quot;data&quot;][&quot;prvi&quot;][&quot;lastUpdateTime&quot;]) &amp;&amp; !empty($epredmet[&quot;data&quot;][&quot;prvi&quot;][&quot;lastUpdateTime&quot;])) {
  </code></pre>
  <p>and this:</p>
  <pre><code>if (isset($epredmet[&quot;data&quot;][&quot;prvi&quot;]) &amp;&amp; !empty($epredmet[&quot;data&quot;][&quot;prvi&quot;])) {
                  if (isset($epredmet[&quot;data&quot;][&quot;prvi&quot;][&quot;lastUpdateTime&quot;]) &amp;&amp; !empty($epredmet[&quot;data&quot;][&quot;prvi&quot;][&quot;lastUpdateTime&quot;])) {
  </code></pre>
  <p>Same thing, it hangs, but without it all it work with erros.</p>