php中的基本关联数组。如何填充它们
首先,我很抱歉,如果这对你们很多人来说太容易了。。。我正在尽可能多地学习 我想与客户一起创建一个阵列,如:php中的基本关联数组。如何填充它们,php,arrays,Php,Arrays,首先,我很抱歉,如果这对你们很多人来说太容易了。。。我正在尽可能多地学习 我想与客户一起创建一个阵列,如: $customers=["customer A", "customer B", "customer C"]; 然后,我想为该阵列中的每个客户创建一个具有某些特征的阵列。这些特征是“城市”、“分数”和“结果”。每个客户都有一个名称(字符串),而不是数字 当需要使用PHP时,我需要更改(或更新)阵列中所有客户的特征(“点”)。比如: for each $customer in $custom
$customers=["customer A", "customer B", "customer C"];
然后,我想为该阵列中的每个客户创建一个具有某些特征的阵列。这些特征是“城市”、“分数”和“结果”。每个客户都有一个名称(字符串),而不是数字
当需要使用PHP时,我需要更改(或更新)阵列中所有客户的特征(“点”)。比如:
for each $customer in $customers {
$points[$customer]=$points[$customer]+2;
}
$points['Customer C']=$points['Customer B']+3;
我还想只更新一位客户的信息,例如:
for each $customer in $customers {
$points[$customer]=$points[$customer]+2;
}
$points['Customer C']=$points['Customer B']+3;
最后,我需要访问特定客户的特征,如:
$i=$points['Customer A']+$result['Customer A'];
我知道这是不正确的,但我应该如何继续在PHP
如何从阵列$Customers(及其所有特征)中消除客户?如何在$Customers中添加新客户
非常感谢你的帮助。我真的很感激 您与您的foreach关系密切:
foreach ($customers as $customer) {
$points[$customer] = $points[$customer] + 2;
}
可以像这样更新单个值:
$points['customer A'] = $points['customer A'] + 2;
unset($points['customer A']);
您可以取消设置/删除如下值:
$points['customer A'] = $points['customer A'] + 2;
unset($points['customer A']);
您与您的foreach关系密切:
foreach ($customers as $customer) {
$points[$customer] = $points[$customer] + 2;
}
可以像这样更新单个值:
$points['customer A'] = $points['customer A'] + 2;
unset($points['customer A']);
您可以取消设置/删除如下值:
$points['customer A'] = $points['customer A'] + 2;
unset($points['customer A']);
正如您的问题所述,assoc阵列:
$customers=[
'客户机'=>[
“点数”=>100,
“城市”=>“纽约”,
“结果”=>40,
],
“customerB'=>[
“点数”=>75,
“城市”=>“阿姆斯特丹”,
“结果”=>10,
],
“customerC'=>[
“点数”=>25,
“城市”=>“伦敦”,
“结果”=>5,
],
];
//删除客户C
未结算($customers['customerC']);
//添加客户D
$customers['customerD']=[
“点数”=>50,
“城市”=>“柏林”,
“结果”=>5,
];
//客户更新点
$customers['customerA']['points']+=2;//加2分
//由其他客户点更新客户点
$customers['customerB']['points']=$customers['customerA']['points']+3;
//为每位客户增加2分
foreach($name=>$properties的客户){
$properties['points']+=2;
$customers[$name]=$properties;
}
客户数组中的键是客户的名称,所有属性都在该键的值内。如您的问题所述,assoc数组:
$customers = array();
//Adding customers
$customers["Customer 1"]["City"] = "Houston";
$customers["Customer 1"]["points"] = 3;
$customers["Customer 1"]["Results"] = "";
$customers["Customer 2"]["City"] = "Paris";
$customers["Customer 2"]["points"] = 8;
$customers["Customer 2"]["Results"] = "";
//updating characteristics
foreach ($customers as $name=>$customer)
{
$customers[$name]["City"] = "Dallas";
$customers[$name]["points"] = $customers[$name]["points"] + 2;
$customers[$name]["Results"] = "";
}
//Removing customer
unset($customers["Customer 1"]);
$customers=[
'客户机'=>[
“点数”=>100,
“城市”=>“纽约”,
“结果”=>40,
],
“customerB'=>[
“点数”=>75,
“城市”=>“阿姆斯特丹”,
“结果”=>10,
],
“customerC'=>[
“点数”=>25,
“城市”=>“伦敦”,
“结果”=>5,
],
];
//删除客户C
未结算($customers['customerC']);
//添加客户D
$customers['customerD']=[
“点数”=>50,
“城市”=>“柏林”,
“结果”=>5,
];
//客户更新点
$customers['customerA']['points']+=2;//加2分
//由其他客户点更新客户点
$customers['customerB']['points']=$customers['customerA']['points']+3;
//为每位客户增加2分
foreach($name=>$properties的客户){
$properties['points']+=2;
$customers[$name]=$properties;
}
客户数组中的键是客户的名称,所有属性都在该键的值内。您可以使用多维数组保存数据。这类似于从数据库请求返回数据的方式,因此是一种很好的使用方法
$customers = array();
//Adding customers
$customers["Customer 1"]["City"] = "Houston";
$customers["Customer 1"]["points"] = 3;
$customers["Customer 1"]["Results"] = "";
$customers["Customer 2"]["City"] = "Paris";
$customers["Customer 2"]["points"] = 8;
$customers["Customer 2"]["Results"] = "";
//updating characteristics
foreach ($customers as $name=>$customer)
{
$customers[$name]["City"] = "Dallas";
$customers[$name]["points"] = $customers[$name]["points"] + 2;
$customers[$name]["Results"] = "";
}
//Removing customer
unset($customers["Customer 1"]);
// Customers array
$customers = [
[
'name' => 'Customer A',
'city' => 'Townville',
'points' => '3',
'results' => '2',
],
[
'name' => 'Customer B',
'city' => 'Blagstonberry',
'points' => '1',
'results' => '4',
],
[
'name' => 'Customer C',
'city' => 'Thorpington',
'points' => '6',
'results' => '3',
],
];
要访问或编辑特定的客户详细信息,您需要获取该客户的密钥。例如,获取客户A的密钥
$key = array_search('Customer A', array_column($customers, 'name'));
现在要访问该客户的城市,例如,您可以使用代码
$customer_a_city = $customers[$key]['city'];
要删除
客户B
// get the key
$key = array_search('Customer B', array_column($customers, 'name'));
// remove customer
unset($customers[$key]);
向所有客户添加2分
foreach ($customers as &$customer) {
$customer['points'] += 2;
}
unset($customer);
这里我们使用和通过引用传递。这意味着我们可以直接在foreach循环中更新该值。最好取消设置变量,在本例中是$customer
,这样以后就不会进行任何不必要的更改
特定客户的特征-客户C
// get the key
$key = array_search('Customer C', array_column($customers, 'name'));
$i = $customers[$key]['points'] + $customers[$key]['results'];
添加一个客户
$customers[] = [
'name' => 'Customer D',
'city' => 'Dongleville',
'points' => '7',
'results' => '1',
];
注释
如果按不存在的名称为客户搜索密钥,$key
将为false
。比如说,
// get the key
$key = array_search('MADE UP NAME', array_column($customers, 'name'));
if ($key === false) {
// customer name did not exist
} else {
// do your thing
}
此外,如果多个客户具有相同的名称,则将返回第一个客户密钥
参考
您可以使用多维数组保存数据。这类似于从数据库请求返回数据的方式,因此是一种很好的使用方法
// Customers array
$customers = [
[
'name' => 'Customer A',
'city' => 'Townville',
'points' => '3',
'results' => '2',
],
[
'name' => 'Customer B',
'city' => 'Blagstonberry',
'points' => '1',
'results' => '4',
],
[
'name' => 'Customer C',
'city' => 'Thorpington',
'points' => '6',
'results' => '3',
],
];
要访问或编辑特定的客户详细信息,您需要获取该客户的密钥。例如,获取客户A的密钥
$key = array_search('Customer A', array_column($customers, 'name'));
现在要访问该客户的城市,例如,您可以使用代码
$customer_a_city = $customers[$key]['city'];
要删除客户B
// get the key
$key = array_search('Customer B', array_column($customers, 'name'));
// remove customer
unset($customers[$key]);
向所有客户添加2分
foreach ($customers as &$customer) {
$customer['points'] += 2;
}
unset($customer);
这里我们使用和通过引用传递。这意味着我们可以直接在foreach循环中更新该值。最好取消设置变量,在本例中是$customer
,这样以后就不会进行任何不必要的更改
特定客户的特征-客户C
// get the key
$key = array_search('Customer C', array_column($customers, 'name'));
$i = $customers[$key]['points'] + $customers[$key]['results'];
添加一个客户
$customers[] = [
'name' => 'Customer D',
'city' => 'Dongleville',
'points' => '7',
'results' => '1',
];
注释
如果按不存在的名称为客户搜索密钥,$key
将为false
。比如说,
// get the key
$key = array_search('MADE UP NAME', array_column($customers, 'name'));
if ($key === false) {
// customer name did not exist
} else {
// do your thing
}
此外,如果多个客户具有相同的名称,则将返回第一个客户密钥
参考
使用unset
删除客户,即unset($points['customer a'])代码>并添加一个$points['Customer D']='vcvb'代码>使用unset
删除客户,即unset($points['customer a'])代码>和t