0

I am trying to remove Product element using unset from the array but it's not working. also, need to insert this array using MySql. How can I achieve this?

unset( $ar2['Product'] );
        print_r($ar2);

Result showing with 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
        )
)
Kane
  • 605
  • 2
  • 8
  • 22

2 Answers2

0

Your code should be

foreach ($ar2 as $array) {
    unset( $array['Product'] );
}

Since you need to unset the key product from each element of the first array.

And to insert it you can use insert query inside the same loop.

foreach ($ar2 as $array) {
    unset( $array['Product'] );
    $sql = "insert into table_name column1,column2 values ($array['Price'],$array['Img'];";
}

I'm Assuming that you know how to insert values into MySql.

Otherwise use the following tutorial.

https://www.w3schools.com/php/php_mysql_insert.asp

Edit

Use this code inside your loop to remove multiple keys.

$keys = array('key1', 'key2');///all the keys you need to remove

foreach($keys as $key) {
   unset($arr[$key]);
}
Yasii
  • 1,702
  • 1
  • 10
  • 15
0

@kane The error in your code is that you are not accessing the correct array index.

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
        )
)

In your array Product index occurs after a numeric index i.e. You can use the approach of Yasii or you can try array_map to iterate over the array of array and unset Product key.

<?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);

As for insert PHP array to Mysql database you need to create an insert statement from the array values and have to handle missing index values like 'Img' key value is missing your second sub-array array

Before working with arrays learn about the best way to use arrays in php here: https://code.tutsplus.com/tutorials/working-with-php-arrays-in-the-right-way--cms-28606

Using mysql ext to insert data into the db is no longer supported in PHP7. Please use PDO instead.

A similar query is answered here: Insert array into MySQL database with PHP

PDO Tutorial: https://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059

Susmita Mitra
  • 91
  • 1
  • 2