1

I'm trying to parse json string to mysql but I get an error saying array_keys() expects parameter 1 to be array, null given on line 58.

<?php
function array_value_recursive($key , $arr){
$val = array();
array_walk_recursive($arr, function ($v, $k) use($key, &$val){
if($k == $key) array_push($val, $v);
        });
return count($val) > 1 ? $val : array_pop($val);
    }
// get tag
$tag = $_POST['tag'];

// include db handler
require_once 'include/DB_Functions.php';
$db = new DB_Functions();

// response Array
$response = array("tag" => $tag, "error" => FALSE);

// check for tag type
if ($tag == 'register') {
    // Request type is Register new user

    $name = $_POST['UserName'];
    $phone = $_POST['Phone'];
    $date=$_POST['userAddedDate'];
    $userIsActive=$_POST['isUserActive'];


        // store user
        $user = $db->storeUser($name,$phone,$date,$userIsActive);
        if ($user!=null) {
            // user stored successfully
           $response["user"]["uid"] = $user["uid"];
            $response["user"]["UserName"] = $user["userName"];
            $response["user"]["Phone"] = $user["userPhone"];
            $response["user"]["isUserActive"] = $user["isUserActive"];
            $response["user"]["Register"] = true;


            echo json_encode($response);
        }
        else {

           $user1 = $db->UserExist($name,$phone);
            // user failed to store
            $response["user"]["uid"] = $user1["uid"];
            $response["user"]["Register"] = false;
            $response["error_msg"] = "user_exist";
            echo json_encode($response);
        }

}
else if($tag == 'CompleteTrip'){



    $trip= $_POST['Trip'];

    // $tripstartDate = $_POST['tripStartDate'];
    // $uid=$_POST['userID'];

    // $bumpGravity = $_POST['bumpGravity'];
    // $bumpLong=$_POST['bumpLong'];
    // $bumplat=$_POST['bumplat'];
    // $bumpTime=$_POST['bumpTime'];
    //$bump=$_POST['bump'];

    $arraykey=array_keys($trip);
    $tripcount=count($arraykey);

    for ($i=0; $i < $tripcount ; $i++) { 

        //  print_r($arr[$arraykey[$i]]);

        $tripDescription=array_value_recursive('tripDescription',    
 $trip[$arraykey[$i]]);
        $tripStartDate=array_value_recursive('tripStartDate', 
 $trip[$arraykey[$i]]);
        $userID=array_value_recursive('userID', $trip[$arraykey[$i]]);

        $bumpGravity=array_value_recursive('bumpGravity', 
 $trip[$arraykey[$i]]);
        $bumpLong=array_value_recursive('bumpLong', $trip[$arraykey[$i]]);
        $bumplat=array_value_recursive('bumplat', $trip[$arraykey[$i]]);
        $bumpTime=array_value_recursive('bumpTime', $trip[$arraykey[$i]]);

        $trip1 = $db-

  >addcompletetrip($tripDescription,$tripStartDate,$userID,$bumpGravity,
   $bumpLong,
  $bumplat,$bumpTime);


    }


    //$trip = $db->addcompletetrip($tripdescription,$tripstartDate,$uid,$bumpGravity,$bumpLong,$bumplat,$bumpTime);
    if ($trip1) {
        $response["trip"]["feedback"] = "true";
        echo json_encode($response);
    }
    else
    {
        $response["trip"]["feedback"] = "false";
        echo json_encode($response);

    }

}
else if ($tag == 'Trip') {

    $tripdescription = $_POST['tripDescription'];
    $tripstartDate = $_POST['tripStartDate'];
    $uid=$_POST['userID'];


        // store trip
        $trip = $db->tripstart($tripdescription,$tripstartDate,$uid);
        if ($trip) {
            // trip stored successfully
            $response["trip"]["Description"] = $trip["tripDescription"];
            $response["trip"]["startDate"] = $trip["tripStartDate"];
            $response["trip"]["userID"] = $trip["userID"];
            $response["trip"]["tripID"] = $trip["tripID"];



            echo json_encode($response);
        } else {
            // user failed to store
            $response["error"] = false;
            $response["error_msg"] = "Error occured in Registartion";
            echo json_encode($response);
        }

   }
   else if ($tag == 'Bump') {
    // Request type is Register new user
    $tripID = $_POST['tripId'];
    $bumpGravity = $_POST['bumpGravity'];
    $bumpLong=$_POST['bumpLong'];
    $bumplat=$_POST['bumplat'];
    $bumpTime=$_POST['bumpTime'];


        // store trip
        $trip = $db-
    >addbumps($tripID,$bumpGravity,$bumpLong,$bumplat,$bumpTime);
        if ($trip) {
            // trip stored successfully

            $response["bump"]["feedback"] = "true";



            echo json_encode($response);
        } else {
            // user failed to store
            $response["error"] = false;
            $response["error_msg"] = "Error occured in Registartion";
            echo json_encode($response);
        }

    }
     else if ($tag == 'LeaderBoard') {

        // store trip
        $LBresponse = $db-
  >addbumps($tripID,$bumpGravity,$bumpLong,$bumplat,$bumpTime);
        if ($LBresponse) {


            echo json_encode($LBresponse);
        } else {
            // user failed to store
            $response["error"] = false;
            $response["error_msg"] = "Error occured in during data 
       fetching";
            echo json_encode($response);
        }

      }
      else if ($tag == 'userHistory') {

       $uid=$_POST['userID'];


        // store trip
        $history = $db->showhistory($uid);

         if ($history) {


            echo json_encode($history);
        } else {
            // user failed to store
            $response["error"] = false;
            $response["error_msg"] = "Error occured in during data 
      fetching";
            echo json_encode($response);
        }



         }
       else {
       // user failed to store
    $response["error"] = false;
    $response["error_msg"] = "error in saving data";
    echo json_encode($response);
    }

   ?>

This is my json string

{
  "tag" : "CompleteTrip",
  "Trip" : [
    {
      "tripDescription" : "My Trip number 17",
      "userID" : "35",
      "bump" : [
        {
          "bumpLong" : "73.089104",
          "bumplat" : "33.732934",
          "bumpTime" : "20.05.2015",
          "bumpGravity" : 0.5719842483606042
        },
        {
          "bumpLong" : "73.089104",
          "bumplat" : "33.732934",
          "bumpTime" : "20.05.2015",
          "bumpGravity" : 0.4080177753918094
        },
        {
          "bumpLong" : "73.089104",
          "bumplat" : "33.732934",
          "bumpTime" : "20.05.2015",
          "bumpGravity" : 0.448878156374468
        }
      ],
      "tripStartDate" : "20.05.2015"
    },
    {
      "tripDescription" : "My Trip number 18",
      "userID" : "35",
      "bump" : [
        {
          "bumpLong" : "73.089105",
          "bumplat" : "33.732965",
          "bumpTime" : "20.05.2015",
          "bumpGravity" : 0.6059529999867896
        },
        {
          "bumpLong" : "73.089105",
          "bumplat" : "33.732965",
          "bumpTime" : "20.05.2015",
          "bumpGravity" : 0.4395928505184715
        },
        {
          "bumpLong" : "73.089105",
          "bumplat" : "33.732965",
          "bumpTime" : "20.05.2015",
          "bumpGravity" : 0.4675621285346855
        },
        {
          "bumpLong" : "73.089105",
          "bumplat" : "33.732965",
          "bumpTime" : "20.05.2015",
          "bumpGravity" : 0.6471158319159812
        }
      ],
      "tripStartDate" : "20.05.2015"
    },
    {
      "tripDescription" : "My Trip number 19",
      "userID" : "35",
      "bump" : [
        {
          "bumpLong" : "73.089216",
          "bumplat" : "33.733184",
          "bumpTime" : "21.05.2015",
          "bumpGravity" : 0.6152545665366322
        },
        {
          "bumpLong" : "73.089216",
          "bumplat" : "33.733184",
          "bumpTime" : "21.05.2015",
          "bumpGravity" : 0.5464117459805118
        },
        {
          "bumpLong" : "73.089216",
          "bumplat" : "33.733184",
          "bumpTime" : "21.05.2015",
          "bumpGravity" : 0.5087776494496044
        }
      ],
      "tripStartDate" : "21.05.2015"
    },
    {
      "tripDescription" : "My Trip number 20",
      "userID" : "35",
      "bump" : [
        {
          "bumpLong" : "73.089113",
          "bumplat" : "33.732939",
          "bumpTime" : "21.05.2015",
          "bumpGravity" : 0.7385709255522828
        },
        {
          "bumpLong" : "73.089113",
          "bumplat" : "33.732939",
          "bumpTime" : "21.05.2015",
          "bumpGravity" : 0.607726128634555
        },
        {
          "bumpLong" : "73.089113",
          "bumplat" : "33.732939",
          "bumpTime" : "21.05.2015",
          "bumpGravity" : 0.5887302539592243
        },
        {
          "bumpLong" : "73.089113",
          "bumplat" : "33.732939",
          "bumpTime" : "21.05.2015",
          "bumpGravity" : 0.4596315163659364
        }
      ],
      "tripStartDate" : "21.05.2015"
    },
    {
      "tripDescription" : "My Trip number 21",
      "userID" : "35",
      "bump" : [
        {
          "bumpLong" : "73.089105",
          "bumplat" : "33.732965",
          "bumpTime" : "21.05.2015",
          "bumpGravity" : 0.6060029844771418
        },
        {
          "bumpLong" : "73.089105",
          "bumplat" : "33.732965",
          "bumpTime" : "21.05.2015",
          "bumpGravity" : 0.4437488421895869
        },
        {
          "bumpLong" : "73.089105",
          "bumplat" : "33.732965",
          "bumpTime" : "21.05.2015",
          "bumpGravity" : 0.4253806333440103
        },
        {
          "bumpLong" : "73.089105",
          "bumplat" : "33.732965",
          "bumpTime" : "21.05.2015",
          "bumpGravity" : 0.7034483457347098
        }
      ],
      "tripStartDate" : "21.05.2015"
    },
    {
      "tripDescription" : "My Trip number 22",
      "userID" : "35",
      "bump" : [
        {
          "bumpLong" : "73.089067",
          "bumplat" : "33.732891",
          "bumpTime" : "21.05.2015",
          "bumpGravity" : 0.5309099143204541
        },
        {
          "bumpLong" : "73.089014",
          "bumplat" : "33.732961",
          "bumpTime" : "21.05.2015",
          "bumpGravity" : 0.4918705089435963
        },
        {
          "bumpLong" : "73.089067",
          "bumplat" : "33.732891",
          "bumpTime" : "21.05.2015",
          "bumpGravity" : 0.6217900667121954
        }
      ],
      "tripStartDate" : "21.05.2015"
    }
  ]
}
1
  • is your $_POST is in json as you dumped here, or an array ? Commented May 26, 2015 at 6:00

1 Answer 1

1

Because array_keys() wants an array and $trip is a (json-)string. you must call json_decode() before you are using the json-data.

Sign up to request clarification or add additional context in comments.

4 Comments

im still getting an error after decoding it. any idea why?
please post the error message, and update your question with your actual source code
this ihs what im getting, the end error, it's not reading the tag and i dont know why error = 0; "error_msg" = "error in saving data'"; tag = "<null>”;
@zavi ali Add $tag to your error handler, to see whats inside this variable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.