在PHP中将嵌套(多维)数组更改为key =>值对

时间:2022-02-15 22:09:32

I have an multidimensional array that looks like this:

我有一个多维数组,它是这样的:

Array
(
    [0] => Array
        (
            [ClientID] => ec2173de2134fdsfg4fsdffcb4b5205
            [Name] => ABC Widgets
        )

    [1] => Array
        (
            [ClientID] => e5dfgfdg2d760f640aadfgdfgdfg47b
            [Name] => Acme Co
        )

    [2] => Array
        (
            [ClientID] => b9dfgsdfg419085c3sdgffdsgfdg313
            [Name] => 4321 Corp
        )

)

I would like to change to the following:

我想改为:

Array
(
  ec2173de2134fdsfg4fsdffcb4b5205 => ABC Widgets
  e5dfgfdg2d760f640aadfgdfgdfg47b => Acme Co
  b9dfgsdfg419085c3sdgffdsgfdg313 => 4321 Corp
)

What is the best way to do so? Also, would I be better off storing the array in its original format and converting to other formats as needed? Thank you for any assistance

最好的办法是什么?此外,我是否更好地将数组存储为原始格式,并根据需要转换为其他格式?谢谢你的帮助

2 个解决方案

#1


5  

$flat = array();
foreach($multidim as $item)
    $flat[$item['ClientID']] = $item['Name'];

Whether you'd be better off storing the original form cannot be answered generally. You should store it if you need it.

你最好还是保存原始表格,这个问题通常无法回答。如果你需要的话,你应该把它存起来。

#2


0  

It looks like you are creating a hash table with the original data. Hash tables are very fast for accessing and inserting single data elements. However, you can't run queries against the data contained in the records--you can only retrieve based on the unique key or insert based on a generated key.

看起来您正在创建一个带有原始数据的散列表。哈希表用于访问和插入单个数据元素非常快。但是,您不能对记录中包含的数据执行查询——您只能根据惟一键进行检索,或者根据生成的键进行插入。

You might use the original format with all its fields as the "back-end" and generate a hash table like the one you demonstrated. The draw-back is that every time this table is generated, it costs CPU cycles.

您可以使用原始格式及其所有字段作为“后端”,并生成一个散列表,如您所演示的那样。回调是每次生成这个表时,它都会消耗CPU周期。

If you throw this data into a database, the DB engine will handle creating regular data tables (like your first one) and hash tables (like your second) as needed for the particular query that you are using. You can also force it to create hash tables based on a certain database column.

如果将这些数据放入数据库中,DB引擎将处理创建常规数据表(如第一个数据表)和哈希表(如第二个),以满足您所使用的特定查询的需要。您还可以强制它基于某个数据库列创建哈希表。

#1


5  

$flat = array();
foreach($multidim as $item)
    $flat[$item['ClientID']] = $item['Name'];

Whether you'd be better off storing the original form cannot be answered generally. You should store it if you need it.

你最好还是保存原始表格,这个问题通常无法回答。如果你需要的话,你应该把它存起来。

#2


0  

It looks like you are creating a hash table with the original data. Hash tables are very fast for accessing and inserting single data elements. However, you can't run queries against the data contained in the records--you can only retrieve based on the unique key or insert based on a generated key.

看起来您正在创建一个带有原始数据的散列表。哈希表用于访问和插入单个数据元素非常快。但是,您不能对记录中包含的数据执行查询——您只能根据惟一键进行检索,或者根据生成的键进行插入。

You might use the original format with all its fields as the "back-end" and generate a hash table like the one you demonstrated. The draw-back is that every time this table is generated, it costs CPU cycles.

您可以使用原始格式及其所有字段作为“后端”,并生成一个散列表,如您所演示的那样。回调是每次生成这个表时,它都会消耗CPU周期。

If you throw this data into a database, the DB engine will handle creating regular data tables (like your first one) and hash tables (like your second) as needed for the particular query that you are using. You can also force it to create hash tables based on a certain database column.

如果将这些数据放入数据库中,DB引擎将处理创建常规数据表(如第一个数据表)和哈希表(如第二个),以满足您所使用的特定查询的需要。您还可以强制它基于某个数据库列创建哈希表。