0

I'm developing a page that takes the info of a json, print a form with the json's values, the user can change it and now I'm trying to upload this data to override the original json with the new data. At this point, I have two arrays, one with the "keys" of the json, and another with the new data that must contain. I can't build the json in the correct way. The problem is to match the two propierties (key and data) to make an array for json_decode after.

I have:

$arrayClave with the keys of the json. and $guardar with the new data.

I have to build a new json with the keys plus the data, like this:

{"ID":"12450","MARCA":"Roly" .... }

I've tried to build the array like array($arrayClave[1] => $guardar[1]);

but I need to build it for every number of inputs, that change in different cases, so I need to use a loop, but it's not possible to do something like:

for($i=0; $i<99; $i++){
    $var = $arrayClave[$i] => $guardar[$i],;
}

$result = array($var);

I know this is not correct, but I don't know the way to do it. If someone can help me I'll be gratefull, I hope I've explained myself in the best way :) Thanks in advance!

2 Answers 2

2

where $arrayClave and $guardar should be arrays

 for($i=0; $i<99; $i++){
  $arrayClave[$i] = $guardar[$i];
  }

 print_r(json_encode($arrayClave));

try this

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

11 Comments

It returns syntax error, that's the problem I have with this solution. Thanks!
did you remember to remove the "," you have after $guardar[$i] in your original post?
You shouldn't have any arrows, use @KarthickKumarGanesh's solution, not your $var = $arrayClave[$i] => $guardar[$i],; solution
@ManelMusti can you give the full code ,so that i can check through
Why do you need to use the => at all?
|
0

Array 1: The Array that containd the Keys "tags" of the json, it comes from the previous page, by a form input:

$arrayClave

Array2: The Array that contains the values of the json, it gets the value of each input that the user has modified in the previous page:

foreach ($arrayClave as $value){
    $valorA = ($_GET[$value]);
    $guardar[] = $valorA;
}

Comments

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.