简体   繁体   English

从2个数组创建新的多维关联数组

[英]Create new multidimensional Associative array from 2 arrays

I am looking for a solution to create a single multidimensional associate array in javascript. 我正在寻找一种在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. 我所拥有的:我有一个用php访问的mysql数据库,并且在一个记录中有一个包含所有字段(键,值对)的数组。 Their are upwards of 30 fields in each record so I am looking for a dynamic solution. 每个记录中都有30多个字段,因此我正在寻找动态解决方案。 In the html coding, there is a form that is used to update a specific record in the table. 在html编码中,有一种用于更新表中特定记录的表格。 I am using a function call on each input to fill a javascript array by key and value. 我在每个输入上使用函数调用,以按键和值填充javascript数组。 The keys are identical to the keys in the php array. 这些键与php数组中的键相同。 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. 在该函数中,我正在php数组上进行json_encode调用,以提取“旧”数据以使其可被javascript访问。

What works: I am able to create a dynamic javascript associate array from the new data coming from the input function calls. 可行的方法:我能够根据来自输入函数调用的新数据创建动态javascript关联数组。 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. 我需要的是一种将javascript数组更改为多维数组,提取旧值并将其添加到绑定到原始键的新数组中的方法。

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: 我的php数组$oldInfo保存了表中的旧信息,例如:

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

The javascript array hold new information: javascript数组包含新信息:

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

I would like a new multidimentional array (PHP) (or object in JavaScript) that would look something like this: 我想要一个新的多维数组(PHP)(或JavaScript中的对象),看起来像这样:

{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. lName和fName将是与PHP数组和JavaScript对象同义的关键字段。

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. 您不需要在这里eval The output of json_encode is JSON, which is a subset of JavaScript that can simply be executed. json_encode的输出是JSON,它是可以简单执行的JavaScript的子集。

  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. 假设key是一个数组,您要做的就是将key / values转换为一个新数组。 If key looks like this before... 如果之前的键看起来像这样...

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

    ... then changes will look like this after: ...之后changes将如下所示:

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

    For a multidimensional array, you need two keys. 对于多维数组,您需要两个键。 You'd write something like changes[key1][key2] = value . 您将编写诸如changes[key1][key2] = value

  4. Your variable naming is wrong. 您的变量命名错误。 You should never see a line that reads like this: key[value] . 永远不会看到这样的一行: 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 . 它应该读取类似array[key] = value

RE: Your clarification: RE:您的澄清:

This doesn't work: {fName=>{"charles","Charlie"},...} . 这不起作用: {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. 数组使用方括号和隐式数字键(例如["charles", "Charlie"] ),而对象可以使用{key1: "value1", key2: "value2"}语法像关联数组一样对待。

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. 我需要的是一种将javascript数组更改为多维数组,提取旧值并将其添加到绑定到原始键的新数组中的方法。

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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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