1

I have an array and through it i wish to fetch values and store them in database.

Array
(
    [success] => 1
    [products] => Array
        (
            [0] => Array
                (
                    [id] => 373
                    [categories] => Array
                        (
                            [0] => 1
                            [1] => 15
                            [2] => 28
                        )
                )

            [1] => Array
                (
                    [id] => 210
                    [categories] => Array
                        (
                            [0] => 15
                            [1] => 28
                        )
                )

            [3] => Array
                (
                    [id] => 209
                    [categories] => Array
                        (
                            [0] => 15
                            [1] => 28
                            [2] => 15
                        )
                )


                )

        )
)

I have fetched all the data, but now i am facing problem in fetching category. i have table who's view is like this

id  prod_id  prod_name  product_catid  product_subcatid

In this array the value 15 is for product_catid and 28 stands for product_subcatid.

part of code through which i was fetching values is,

if (!empty($array)) 
    {
        foreach ($array['products'] as $product) 
            {
                    if(isset($product['categories']))
                        {
                            $product_catid =  $product['categories'][1];
                            $product_subcatid =  $product['categories'][2];

                            $inserts1 = mysql_query("insert into product_category(prod_id,prod_name,product_catid,product_subcatid,product_subsubcatid) values ('".$idd."','".$product_name."','".$product_catid."','".$product_subcatid."','".$product_subsubcatid."')");

                            $posts[0]['message'] = 'Registered';
                        }

            } 
    } 

But the issue is in the second array i.e [1], the value of product_catid has shifted to [0] index value and product_subcatid has shifted to [1] index value. Now i am stuck and am not able to understand how should i fetch and store values. would appreciate some help.

0

2 Answers 2

1

If your subcat will always fall last, and the cat will follow that previously, just use array_pop instead of pointing to it explicitly. Rough example:

if (!empty($array)) {
    foreach ($array['products'] as $k => $product) {
        if(isset($product['categories'])) {

            $product_subcatid =  array_pop($product['categories']);
            $product_catid =  array_pop($product['categories']);

            $db = new PDO('mysql:host=localhost;dbname=database_name', 'username', 'password');
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $insert = $db->prepare('INSERT INTO product_category (prod_id,prod_name,product_catid,product_subcatid,product_subsubcatid) VALUES (?, ?, ?, ?, ?)');
            $insert->execute(array($idd, $product_name, $product_catid, $product_subcatid, $product_subsubcatid));
        }
    }
}

Sample Output

Another way would be to just reverse it:

$product['categories'] = array_reverse($product['categories']);
$product_subcatid =  $product['categories'][0];
$product_catid =  $product['categories'][1];
Sign up to request clarification or add additional context in comments.

2 Comments

can u plz help me once more??? i have a situation where i have one array where i have 3 index value in first array [0][1][2] where [0]=subcat, [1] =cat and [2] = subcat and in next array [ 0] = cat and [1] = subcat, how can i insert 2 subcat within a table in different rows
@kavi it depends on how you would want to approach it, get the cat first, then, loop it by subcats since you'll have two subcats now, and how do you determine which is which anyway?
0

An alternative solution:

$product_subcatid = end($product['categories']);
$product_catid = prev($product['categories']);

This way you don't modify the array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.