PHP 删除元素表单数组无法正常工作
PHP remove element form array is not working properly
我正在尝试使用 unset 从数组中删除 Product
元素,但它不起作用。此外,需要使用 MySql 插入此数组。我怎样才能做到这一点?
unset( $ar2['Product'] );
print_r($ar2);
结果显示 Product
Array
(
[0] => Array
(
[Product] => Resume, CV, vCard & Portfolio HTML5 Template
[Price] => 10
[Img] => http://localhost/envy-theme-new/wp-content/uploads/2019/01/cv.jpg
)
[1] => Array
(
[Product] => Runhill – Multipurpose Construction WP Theme
[Price] => 39
)
)
你的代码应该是
foreach ($ar2 as $array) {
unset( $array['Product'] );
}
因为您需要取消设置第一个数组的每个元素的键 product
。
要插入它,您可以在同一循环内使用插入查询。
foreach ($ar2 as $array) {
unset( $array['Product'] );
$sql = "insert into table_name column1,column2 values ($array['Price'],$array['Img'];";
}
我假设您知道如何将值插入 MySql。
否则使用下面的教程。
https://www.w3schools.com/php/php_mysql_insert.asp
编辑
在循环中使用此代码删除多个键。
$keys = array('key1', 'key2');///all the keys you need to remove
foreach($keys as $key) {
unset($arr[$key]);
}
@凯恩
您的代码中的错误是您没有访问正确的数组索引。
Array
(
[0] => Array
(
[Product] => Resume, CV, vCard & Portfolio HTML5 Template
[Price] => 10
[Img] => http://localhost/envy-theme-new/wp-content/uploads/2019/01/cv.jpg
)
[1] => Array
(
[Product] => Runhill – Multipurpose Construction WP Theme
[Price] => 39
)
)
在您的数组中,产品索引出现在数字索引之后,即
您可以使用 Yasii 的方法,也可以尝试 array_map 遍历数组数组并取消设置产品密钥。
<?php
$data=array(
array(
'Product' => 'Resume, CV, vCard & Portfolio HTML5 Template',
'Price' => 10,
'Img' => 'http://localhost/envy-theme-new/wp-content/uploads/2019/01/cv.jpg'
),
array(
'Product' => 'Runhill – Multipurpose Construction WP Theme',
'Price' => 39
)
);
$data = array_map(function($arr){
unset($arr['Product']);
return $arr;
}, $data);
echo '<pre>';
var_dump($data);
至于将 PHP 数组插入 Mysql 数据库,您需要从数组值创建一个插入语句,并且必须处理缺少的索引值,例如 'Img' 键值丢失第二个子数组array
在使用数组之前,请先了解 php 中使用数组的最佳方式:
https://code.tutsplus.com/tutorials/working-with-php-arrays-in-the-right-way--cms-28606
PHP7 不再支持使用 mysql ext 将数据插入数据库。
请改用 PDO。
此处回答了类似的查询:
Insert array into MySQL database with PHP
PDO教程:
https://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059
我正在尝试使用 unset 从数组中删除 Product
元素,但它不起作用。此外,需要使用 MySql 插入此数组。我怎样才能做到这一点?
unset( $ar2['Product'] );
print_r($ar2);
结果显示 Product
Array
(
[0] => Array
(
[Product] => Resume, CV, vCard & Portfolio HTML5 Template
[Price] => 10
[Img] => http://localhost/envy-theme-new/wp-content/uploads/2019/01/cv.jpg
)
[1] => Array
(
[Product] => Runhill – Multipurpose Construction WP Theme
[Price] => 39
)
)
你的代码应该是
foreach ($ar2 as $array) {
unset( $array['Product'] );
}
因为您需要取消设置第一个数组的每个元素的键 product
。
要插入它,您可以在同一循环内使用插入查询。
foreach ($ar2 as $array) {
unset( $array['Product'] );
$sql = "insert into table_name column1,column2 values ($array['Price'],$array['Img'];";
}
我假设您知道如何将值插入 MySql。
否则使用下面的教程。
https://www.w3schools.com/php/php_mysql_insert.asp
编辑
在循环中使用此代码删除多个键。
$keys = array('key1', 'key2');///all the keys you need to remove
foreach($keys as $key) {
unset($arr[$key]);
}
@凯恩 您的代码中的错误是您没有访问正确的数组索引。
Array
(
[0] => Array
(
[Product] => Resume, CV, vCard & Portfolio HTML5 Template
[Price] => 10
[Img] => http://localhost/envy-theme-new/wp-content/uploads/2019/01/cv.jpg
)
[1] => Array
(
[Product] => Runhill – Multipurpose Construction WP Theme
[Price] => 39
)
)
在您的数组中,产品索引出现在数字索引之后,即 您可以使用 Yasii 的方法,也可以尝试 array_map 遍历数组数组并取消设置产品密钥。
<?php
$data=array(
array(
'Product' => 'Resume, CV, vCard & Portfolio HTML5 Template',
'Price' => 10,
'Img' => 'http://localhost/envy-theme-new/wp-content/uploads/2019/01/cv.jpg'
),
array(
'Product' => 'Runhill – Multipurpose Construction WP Theme',
'Price' => 39
)
);
$data = array_map(function($arr){
unset($arr['Product']);
return $arr;
}, $data);
echo '<pre>';
var_dump($data);
至于将 PHP 数组插入 Mysql 数据库,您需要从数组值创建一个插入语句,并且必须处理缺少的索引值,例如 'Img' 键值丢失第二个子数组array
在使用数组之前,请先了解 php 中使用数组的最佳方式: https://code.tutsplus.com/tutorials/working-with-php-arrays-in-the-right-way--cms-28606
PHP7 不再支持使用 mysql ext 将数据插入数据库。 请改用 PDO。
此处回答了类似的查询: Insert array into MySQL database with PHP
PDO教程: https://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059