简体   繁体   中英

Create new multidimensional Associative array from 2 arrays

I am looking for a solution to create a single multidimensional associate array in javascript.

What I have: I have a mysql database I am accessing with php and have an array containing all fields (key,value pairs) in a single record. Their are upwards of 30 fields in each record so I am looking for a dynamic solution. In the html coding, there is a form that is used to update a specific record in the table. I am using a function call on each input to fill a javascript array by key and value. The keys are identical to the keys in the php array. In the function I am doing a json_encode call on the php array to pull in the "old" data to make it accessible to javascript.

What works: I am able to create a dynamic javascript associate array from the new data coming from the input function calls. I have tested this out using an alert after each call to the function.

What I need: A method to change the javascript array to a multidimensional array, pulling in the old value and adding it to the new array tied to the original key.

This works:

var changes={}; 
function change(key,value) {
  changes[key[value]]=value;
  for (key in changes) {
    alert('key: '+key+'...  value: '+changes[key]);
  }
}

this is along the lines of what I am looking for:

var changes={}; 
function change(key,value) {
  var oldInfo = eval(<? echo json_encode($oldInfo); ?>); //this from the php array  
  changes[key[newValue]]=value; 
  changes[key[oldValue]]=oldInfo[key];
  for (key in changes) {
    alert('key: '+key+'...  value: '+changes[key[newValue]]);
  }
}

Can someone point me in the right direction?

To clarify:

My php array $oldInfo holds the old information from the table, for example:

{fName=>"charles",lName=>"madison", etc.}

The javascript array hold new information:

{fName=>"Charlie",lName=>"Madison", etc.}

I would like a new multidimentional array (PHP) (or object in JavaScript) that would look something like this:

{fName=>{"charles","Charlie"}, lName=>{"madison","Madison"}, etc.}

lName and fName would be the key fields that are synonymous to both the PHP array and the JavaScript object.

It's really unclear what you want, but there are a couple of serious flaws with your logic:

  1. var changes={}; ///this one way of declaring array in javascript

    No, it isn't. That's an Object, which is very different from an array.

  2. eval(<? echo json_encode($oldInfo); ?>);

    You don't need eval here. The output of json_encode is JSON, which is a subset of JavaScript that can simply be executed.

  3. changes[key[value]]=value;

    This is totally wrong, and still a single-dimensional array. Assuming key is an array, all you're doing is inverting the keys/values into a new array. If key looks like this before...

     'a' => 1 'b' => 2 'c' => 3 

    ... then changes will look like this after:

     1 => 'a' 2 => 'b' 3 => 'c' 

    For a multidimensional array, you need two keys. You'd write something like changes[key1][key2] = value .

  4. Your variable naming is wrong. You should never see a line that reads like this: key[value] . That's backwards. The key goes between the [] , the value goes on the other side of the = . It should read something like array[key] = value .

RE: Your clarification:

This doesn't work: {fName=>{"charles","Charlie"},...} . You're confusing arrays and objects; Arrays use square brackets and implicit numeric keys ( ["charles", "Charlie"] for example) while Objects can be treated like associative arrays with {key1: "value1", key2: "value2"} syntax.

You want an array, where each key is the name of a property and each value is an array containing the old and new values.

I think what you want is actually quite simple, assuming the "value" you're passing into the function is the new value.

var changes = {};
var oldInfo = <?= json_encode($oldInfo) ?>;

function change(key, value) {
  changes[key] = [ oldInfo[key], value ]  
}

This :

changes[key[newValue]]

Should be:

changes[key][newValue]

What I need: A method to change the javascript array to a multidimensional array, pulling in the old value and adding it to the new array tied to the original key.

Use aliases for the numeric indices to do this:

var foo = ["Joe","Blow"];
var bar = ["joe","blow"];
var names = {};

foo.fname = foo[0];
bar.fname = bar[0];
foo.lname = foo[1];
bar.lname = bar[1];

names.fname = [foo.fname,bar.fname];
names.lname = [foo.lname,bar.lname];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM