title: Turning associative JSON array into nested SQL WHERE clause
body: <p>I'm new to backend programming and CodeIgniter is my first framework. This is new to me. Right now, the thing that I'm working is that I need to create a query based on the JSON encoded data saved at my table.</p>
  <p>I have here a segmentation form that should build a query to look/search for the customers profiles based on the condition set</p>
  <p><a href="https://i.stack.imgur.com/Kq7xK.png" rel="nofollow noreferrer">Segmentation form</a></p>
  <pre><code>    $segment = array(
         ...
         'filters' =&gt; json_encode($post['filters']), //to insert the filters in json
         ...
      );
      if ($this-&gt;model-&gt;addSegments($segment))
  </code></pre>
  <p>The filters saved in the database are like this :</p>
  <pre><code>{&quot;data&quot;:[{&quot;data&quot;:{&quot;condition&quot;:&quot;0&quot;,&quot;attribute&quot;:&quot;gender&quot;,&quot;sign&quot;:&quot;=&quot;,&quot;value&quot;:&quot;Male&quot;},&quot;type&quot;:&quot;condition&quot;,&quot;operator&quot;:&quot;0&quot;,&quot;filter&quot;:{&quot;operator&quot;:&quot;0&quot;}}],&quot;type&quot;:&quot;match&quot;,&quot;operator&quot;:&quot;0&quot;}
  </code></pre>
  <p>Looking for the answers, I found this forum and I relate it to the problem that I had: <a href="http://www.codingforums.com/php/202207-turning-json-object-into-nested-sql-where-clause.html" rel="nofollow noreferrer">http://www.codingforums.com/php/202207-turning-json-object-into-nested-sql-where-clause.html</a></p>
  <p>Calling the same like function (solution at the link) to the controller using this lines:</p>
  <pre><code>$filter = $this-&gt;model-&gt;getSegmentsFilters($id_segment);
  $filters = $this-&gt;model-&gt;parseFilterToQuery(json_decode($filter['filters'], true));
  </code></pre>
  <p>With the parameter <code>TRUE</code> from the <code>json_decode</code>, It throws the filters in array and using <code>print_r</code> shown like this:</p>
  <pre><code>Array ( 
      [0] =&gt; Array ( 
          [data] =&gt; Array ( 
              [condition] =&gt; 0 
              [attribute] =&gt; firstname 
              [sign] =&gt; = 
              [value] =&gt; John ) 
          [type] =&gt; condition 
          [operator] =&gt; 0 
          [filter] =&gt; Array ( 
              [operator] =&gt; 0 ) 
          ) 
      [1] =&gt; Array ( 
          [data] =&gt; Array ( 
              [condition] =&gt; 0 
              [attribute] =&gt; gender 
              [sign] =&gt; = 
              [value] =&gt; Male ) 
          [type] =&gt; condition 
          [operator] =&gt; condition 
          [filter] =&gt; Array ( 
              [operator] =&gt; 0 ) 
          ) 
      ) 
  </code></pre>
  <p>the <code>$sql</code> inside <code>if(is_array)</code> doesn't show any value to concatenate with. I want it to concatenate with another query as a WHERE clause like this idea.</p>
  <pre><code>$sql = &quot;SELECT * FROM customers WHERE&quot; . parseFilterToQuery($filters);
  </code></pre>
  <p>If you think that the solution/procedure that I came up is correct. My question is that inside the <code>if(is_array)</code> condition,
  how should I work on it with associative JSON array?</p>