About foreach and array_merge

Asked 2 years ago, Updated 2 years ago, 88 views

PHP (using EC-CUBE 3.0.10) is trying to retrieve the target (store name) data (item category) from DB and get more parent categories from it to be displayed.

The flow is
①—Retrieves the username (identical to the store name) from the user information.
②: Using the username obtained in で, obtain the category name of the same person and put it in an array.
③: Obtain the parent category that belongs to the category obtained in で.
④: For display, combine multiple array categories into one (like packing while looping with foreach)

I tried to do it with for, but I'm trying to use foreach because it's better to use foreach for arrays, but I don't really understand how to use array_merge to combine foreach with arrays...

// Since the store name is the same as the username, the username is obtained from the user information and stored in the $name below.
$name = $app['user']->getName();
// Obtain the category name that matches the username obtained.
$categories=$em->getRepository('\Eccube\Entity\Category') ->findBy(array('name'=>$name));
// TODO I want to get the parent category ID to which this acquired category name belongs.

Could you lend me some advice?
Thank you for your cooperation.

Dear kitar,

Thank you for your reply.

I think it was difficult to communicate because of my poor writing ability...
Sorry
As an example, the categories are registered as follows:

Tableware
 Store A
 Store C
Furniture
 Store B
 Store C

In this case, $categories stores information for Store A and Store B.

Example: Store B

0=>Category {#1724▼
    -id : 10
    -name: "Store B"
    -level:2
    -rank —6
    -create_date:DateTime {#1721▶}
    - update_date:DateTime {#1722▶}
    -del_flg:0
    - CategoryCount: null
    - CategoryTotalCount: null
    - ProductCategories: PersistentCollection {#1733▶}
    - Children: PersistentCollection {#1735▶}
    - Parent: Category {#1782▶}
    - Creator: Member {#1374▶}
  }

In the case of store C, there are store C belonging to tableware and store C belonging to furniture.

0=>Category {#1735▼
    -id : 11
    -name: "Store C"
    -level:2
    -rank: 7
    -create_date:DateTime {#1732▶}
    - update_date:DateTime {#1733▶}
    -del_flg:0
    - CategoryCount: null
    - CategoryTotalCount: null
    - ProductCategories: PersistentCollection {#1744▶}
    - Children: PersistentCollection {#1746▶}
    - Parent: Category {#1793▶}
    - Creator: Member {#1792▶}
  }
  1 = > Category {#1789 ▼
    - id : 13
    -name: "Store C"
    -level:2
    -rank:3
    -create_date:DateTime {#1791▶}
    - update_date: DateTime {#1790▶}
    -del_flg:0
    - CategoryCount: null
    - CategoryTotalCount: null
    - ProductCategories: PersistentCollection {#1788▶}
    - Children: PersistentCollection {#1786▶}
    - Parent: Category {#1784▶}
    - Creator: Member {#1792▶...2}
  }

Because of the array, it may be ideal to use foreach, but

for($i=0;$i<count($categories);$i++){
・・・
}

This for also works correctly, so I don't mind doing it in the for way...
There are times and times, so I'd like to pack them so that even if there are more than one, they work fine.

By default, EC-CUBE has a separate logic for retrieving the parent category, which can be retrieved in the getPath entity class of Category and will be used.
When I put the following in the for statement above, it was overwritten in more than one case later.

$category=$categories[$i]->getPath();

php ec-cube

2022-09-30 19:39

1 Answers

Tableware
 Store A
 Store C
Furniture
 Store B
 Store C

For store name A,

Tableware
 Store A

For store name C

Tableware
 Store C
Furniture
 Store C

Do you recognize that you want to display ?
If that's the case, then if it's to fit the question,

$parents=array();
foreach($categories as$categories){
    $parent = {Retrieve Parent Category};
    $set_parents[$parent["name"] = $parent;
    $parents = array_merge($parents, $set_parents);
}

above or
If you follow the rule that IDs are unique,

$parents=array();
foreach($categories as$categories){
    $parent = {Retrieve Parent Category};
    $parents[$parent["id"] = $parent;
}

I think I can do it here.


2022-09-30 19:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.