user4422481
user4422481

Reputation:

fetch values from different index value in array

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.

Upvotes: 1

Views: 113

Answers (2)

msfoster
msfoster

Reputation: 2572

An alternative solution:

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

This way you don't modify the array.

Upvotes: 0

Kevin
Kevin

Reputation: 41885

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

Upvotes: 1

Related Questions