简体   繁体   中英

Need help regarding multi dimensional array formatting

I have a multidimensional array in which i want to group category information of categories in to one array and other array data to respective arrays.

I am trying to get data from a database via some sql queries and then merge whole data to a single array.

$out = array();

foreach ($arr as $key => $value){
//$out[] = (object)array_merge((array)$ar[$key], (array)$value);
$out[] = array_merge((array)$ar[$key], (array)$value);
}


$tmp = array();

foreach($out as $arg)
{
foreach($arg as $arg2 )
{
   $tmp[$arg2['product_id']][] = @$arg2['category_id'];
}


}


echo "<pre>";
print_r($tmp);

I want to group all category related to a specific product to a signle array like category id, category name, category description etc and product data to other respective array.

Here below is my result array containing category information and product data.

 Array
 (
 [0] => Array
    (
        [product_id] => 84
        [category_id] => 1060
        [category_name] => ACMITT
        [category_descr] => 

        010010                                                                                                        

        Syn: Aegerin                                                                                              

       Skaper mot og styrke når retrett er umulig. Gir klart syn 
       og overblikk.          

       Et mineral som inneholder natrium og jern. Hardhet på 6,o 
       og egenvekt på 3,5

    )

[1] => Array
    (
        [product_id] => 84
        [category_id] => 1252
        [category_name] => A
        [category_descr] => 
    )

[2] => Array
    (
        [product_attr_id] => 1827
        [product_id] => 84
        [attribute_id] => 13
        [product_attr_value] => 

    Skaper mot og styrke når retrett er umulig. Gir klart syn og 
    overblikk. Syn: Aegerin

    )

[3] => Array
    (
        [product_attr_id] => 1819
        [product_id] => 84
        [attribute_id] => 4
        [product_attr_value] => 010010
    )

[4] => Array
    (
        [product_attr_id] => 1823
        [product_id] => 84
        [attribute_id] => 9
        [product_attr_value] => 
        http://www.steinhaugenmoss.no/avactis- 
        images/acmittkrysttynne.jpg
    )

[5] => Array
    (
        [product_attr_id] => 1816
        [product_id] => 84
        [attribute_id] => 1
        [product_attr_value] => 50.00
    )

   )

     Array
     (
        [0] => Array
     (
        [product_id] => 5583
        [category_id] => 1057
        [category_name] => Gaveartikkler
        [category_descr] => 

        Mange av våre artikkler egner seg som gaver til jubilerer 
        og annet. Her har vi plasert steinklokker, telysholdere og 
        annet som egner seg som gaver. 

    )

[1] => Array
    (
        [product_id] => 5583
        [category_id] => 1068
        [category_name] => AGAT
        [category_descr] => 

          010030                                                                                                  

         Syn: Innvielsesstein                                                                                    

         Gir deg selvrespekt mot og handlekraft. Hjelper for 
         hjertet.                    

        Er mineralet kvarts og består av silicium oksyd. Har en 
        hardhet på 7.0 og egenvekt på 2.7 Det har felt seg ut 
        kryptokrystallinsk og danner knoller av mikro kvarts 
        krystaller. Fargen kommer av metaliske forurensinger. De 
        fleste agater i handel er farget.

    )

[2] => Array
    (
        [product_attr_id] => 117306
        [product_id] => 5583
        [attribute_id] => 13
        [product_attr_value] => Gir deg selvrespekt mot og 
         handlekraft. Hjelper for hjertet. Syn: innvielsesstein
    )

[3] => Array
    (
        [product_attr_id] => 117302
        [product_id] => 5583
        [attribute_id] => 9
        [product_attr_value] => Boksttte700.jpg
    )

[4] => Array
    (
        [product_attr_id] => 117298
        [product_id] => 5583
        [attribute_id] => 4
        [product_attr_value] => 010030
    )

[5] => Array
    (
        [product_attr_id] => 117295
        [product_id] => 5583
        [attribute_id] => 1
        [product_attr_value] => 700.00
    )

)

I want to format this array so that i can generate a CSV file of this data in order to import to woocommerce.

correct me if im wrong . you want to create a multidimensional array that group by category. if that the case try this code:

$newArray = [];
foreach ($arrs as $key => $value) {

   $newArray[$value['category_name']][] = $value;
}

Here below is the code about how i fixed my issue.

$final_arr = [];

foreach($out as $v) { $cat_name = array_column($v,'category_name');

$p_info = array_column($v,'product_attr_value');

$attrb_id = array_column($v,'attribute_id');

$p_id = array_column($v,'product_id');

$p_name = array_column($v,'product_name');

$c = array_combine($attrb_id, $p_info);

$final_arr[] = array(
    'pid' =>  $p_id[0],
    'product_title' => $p_name[0],
    'product_name' => $p_name[0],
    'product_desc' => $c[13],
    'post_status'  => 'publish',
    'post_author'  => 1,
    'product_type' => 'simple',
    'price' => $c[1],
    'sku' => $c[4],
    'image' => $c[9]
);
}

I create an array and using array_column function i get the values which i want to create a final array to perform a group array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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