【问题标题】:PHP remove element form array is not working properlyPHP删除元素表单数组无法正常工作
【发布时间】:2019-06-27 05:40:05
【问题描述】:

我正在尝试使用 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
        )
)

【问题讨论】:

    标签: php mysql arrays associative-array


    【解决方案1】:

    你的代码应该是

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

    【讨论】:

      【解决方案2】:

      @凯恩 您的代码中的错误是您没有访问正确的数组索引。

      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 来遍历数组并取消设置 Product 键。

      <?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”键值缺少您的第二个子数组数组

      在使用数组之前,请在此处了解在 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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-07
        • 1970-01-01
        • 2012-01-17
        • 2017-05-23
        • 2011-06-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多