简体   繁体   English

从数组中的不同索引值获取值

[英]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. 在此数组中,值15表示product_catid,28表示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 ie [1], the value of product_catid has shifted to [0] index value and product_subcatid has shifted to [1] index value. 但问题在于第二个数组,即[1],product_catid的值已转移到[0]索引值,而product_subcatid已转移到[1]索引值。 Now i am stuck and am not able to understand how should i fetch and store values. 现在我陷入困境,无法理解我应该如何获取和存储值。 would appreciate some help. 会很感激一些帮助。

If your subcat will always fall last, and the cat will follow that previously, just use array_pop instead of pointing to it explicitly. 如果您的subcat总是落在最后,并且cat将遵循之前的那个,那么只需使用array_pop而不是明确地指向它。 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];

An alternative solution: 替代解决方案:

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

This way you don't modify the array. 这样您就不会修改数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM