title: Cannot able to access json array in php , it returns null and Warning: Invalid argument supplied for foreach()
body: <p>I am trying to send json array to php and insert multiple rows in to table</p>
  
  <p>Problem:</p>
  
  <p>Php:</p>
  
  <ol>
  <li>Not able to access the decode JSON array - array count returns nil </li>
  <li>Warning: Invalid argument supplied for foreach() </li>
  </ol>
  
  <p>When I print JSON string it gives me the below data</p>
  
  <pre><code>[
      {
          "email" : "",
          "Name" : "Ddd",
          "contact2" : "",
          "ontact1" : ""
      },
      {
          "email" : "",
          "Name" : "Ddd",
          "contact2" : "",
          "contact1" : ""
      },
      {
          "email" : "",
          "Name" : "Dddddr",
          "contact2" : "",
          "contact1" : ""
      }
  ]
  </code></pre>
  
  <p>But when I am trying to access that using php that shows me a error</p>
  
  <p><strong>PHP CODE:</strong></p>
  
  <pre><code>&lt;?php
  
      $inputJSON = file_get_contents('php://input');
      $array = json_decode($inputJSON, true);
  
      echo count($array);
  
      foreach($array as $item) {
          $uses = $item['Name'];
          echo $uses;
      }
  
  ?&gt;
  </code></pre>
  
  <p>Error: </p>
  
  <blockquote>
    <p>Warning: Invalid argument supplied for foreach()</p>
  </blockquote>
  
  <p>So I tested the array in php using ,</p>
  
  <pre><code>Isarray() || Isobject funtion but it print null
  Count() funcions provide 0 
  
   var_dump($array); result NULL
  </code></pre>
  
  <p>But I decode the array and check the result in iOS, it gives the array as response</p>
  
  <pre><code>echo json_encode($array);
  </code></pre>
  
  <p>But in php I am not able access the array.</p>
  
  <p>Not sure if there is any mistake in iOS code. In iOS I have <code>NSMutableDictionary</code> that is stored to <code>NSMutableArray</code> and then <code>NSArray</code>.
  When I parse the <code>NSMutableArray</code> I get the same problem.</p>
  
  <p><strong>iOS code:</strong></p>
  
  <pre><code>func saveToCloud(){
  
      var mutablearray = NSMutableArray()
  
      var dict = NSMutableDictionary()
  
      var eachRow = textfieldarry  as [UITextField]
      for eachField in eachRow {
          let index = eachRow.indexOf(eachField)
          let  data =  eachField.text!.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
  
  
          dict[keys[index!]] = data
      }
  
      mutablearray.addObject(dict)
  
      var TonNSarray = NSArray(array: mutablearray)
      var url = "myurl/file.php "
  
      JsonParseTOSaveCloud(TonNSarray, urlstring: url, successfullResponse: "Success", alertmessage: "not")
  }
  </code></pre>
  
  <p><strong>Saving to Server:</strong></p>
  
  <pre><code>func ok_JsonParseTOSaveCloud(dict:AnyObject,urlstring:String,successfullResponse:String,alertmessage:String){
  
      let json:NSData = try! NSJSONSerialization.dataWithJSONObject(dict, options: NSJSONWritingOptions.PrettyPrinted)
      var d = NSString(data: json, encoding: NSUTF8StringEncoding)
      print("jsonstr\(d!)")
      let urlString = urlstring
  
      urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLPathAllowedCharacterSet())!
  
      let httpRequest = NSMutableURLRequest(URL: NSURL(string: urlString)!)
  
      httpRequest.HTTPMethod = "POST"
  
      httpRequest.HTTPBody = json
  
      let sessionConfig = NSURLSessionConfiguration.defaultSessionConfiguration()
  
      sessionConfig.HTTPAdditionalHeaders = ["Accept" : "application/json", "api-key" : "API_KEY"]
  
      let session = NSURLSession(configuration: sessionConfig)
  
      let postDataTask = session.dataTaskWithRequest(httpRequest, completionHandler: {(data: NSData?, reponse: NSURLResponse?, error: NSError?) in
  
          if data == nil {
  
              dispatch_async(dispatch_get_main_queue(), {
  
                  let alert = UIAlertView(title: alertmessage, message: "you are no longer connected to the Internet", delegate: self, cancelButtonTitle: "Dismiss")
  
                  alert.show()
  
              })
          } else {
  
              print("data\(data)")
  
              //let jsonResult = (try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers))
          }
  
      })
  
      postDataTask.resume()
  }    
  </code></pre>
  
  <p>Note: When I check in php, like this it works. That is when I give the Jsonstring or jsonarray directly it works.</p>
  
  <pre><code>&lt;?php
  
      $json  = '[
      {
          "email" : "",
          "Name" : "Ddd",
          "contact2" : "",
          "contact1" : ""
      },
  {
      "email" : "",
      "Name" : "Ddd",
      "contact2" : "",
      "contact1" : ""
  },
  {
      "email" : "",
      "Name" : "Dddddr",
      "contact2" : "",
      "contact1" : ""
  }
  ]';
  
  $array = json_decode( $json, true );
  
  foreach($array as $item) {
      $uses = $item['Name'];
      echo $uses;
  }
  
      ?&gt;
  </code></pre>