Php 显示依赖于差异数组名称的数组成员

Php 显示依赖于差异数组名称的数组成员,php,arrays,laravel,Php,Arrays,Laravel,我创建了一个网站的拉威尔,我想显示类别和相关的产品名称 我有一个数组,就像这样 $category = [ '1' => 'cat1', '2' => 'cat2' ]; $product = [ '0' => array( 'name' => 'product1', 'category_id' => '2' ), '1' => array( 'name' => 'product2',

我创建了一个网站的拉威尔,我想显示类别和相关的产品名称

我有一个数组,就像这样

$category  = [
    '1' => 'cat1',
    '2' => 'cat2'
];

$product = [
    '0' => array(
     'name' => 'product1',
     'category_id' => '2'
),
    '1' => array(
     'name' => 'product2',
     'category_id' => '2'
),
    '2' => array(
     'name' => 'product3',
     'category_id' => '2'
),
    '3' => array(
     'name' => 'product4',
     'category_id' => '1'
),
    '4' => array(
     'name' => 'product5',
     'category_id' => '1'
),
    '5' => array(
     'name' => 'product6',
     'category_id' => '2'
)]; 
<h3>cat1</h3>
<p>product4</p>
<p>product5</p> 
<hr>
<hr> 
<h3>cat2</h3>
<p>product1</p>
<p>product2</p>
<p>product3</p>
<p>product6</p> 
我想这样展示

$category  = [
    '1' => 'cat1',
    '2' => 'cat2'
];

$product = [
    '0' => array(
     'name' => 'product1',
     'category_id' => '2'
),
    '1' => array(
     'name' => 'product2',
     'category_id' => '2'
),
    '2' => array(
     'name' => 'product3',
     'category_id' => '2'
),
    '3' => array(
     'name' => 'product4',
     'category_id' => '1'
),
    '4' => array(
     'name' => 'product5',
     'category_id' => '1'
),
    '5' => array(
     'name' => 'product6',
     'category_id' => '2'
)]; 
<h3>cat1</h3>
<p>product4</p>
<p>product5</p> 
<hr>
<hr> 
<h3>cat2</h3>
<p>product1</p>
<p>product2</p>
<p>product3</p>
<p>product6</p> 
cat1
产品4

产品5



第二类 产品1

产品2

产品3

产品6


你必须知道我在使用laravel,我想用laravel做这项工作,但你也可以用PHP编码,我想我可以用laravel转换它,你可以使用simple
foreach
循环,如下所示:

foreach($category as $ind=>$cat){
    echo "<h3>".$cat."</h3>".PHP_EOL;
    foreach($product as $row){
        if($row['category_id'] == $ind) {
            echo "<p>".$row['name']."</p>".PHP_EOL;
        }
    }
    echo "<hr><hr>".PHP_EOL;
}
foreach($ind=>$cat类){
回声“$cat.”。PHP_EOL;
foreach($row形式的产品){
如果($row['category_id']==$ind){
echo“”$row['name']。“

”.PHP\u EOL; } } echo“

”.PHP\u EOL; }

产出:

<h3>cat1</h3>
<p>product4</p>
<p>product5</p>
<hr><hr>
<h3>cat2</h3>
<p>product1</p>
<p>product2</p>
<p>product3</p>
<p>product6</p>
<hr><hr>
cat1
产品4

产品5



第二类 产品1

产品2

产品3

产品6



试试看

@foreach($key=>$cat类)
{{$cat}
@foreach($pr形式的产品)
@如果($pr['category_id']==$key)
{{$pr['name']}


@恩迪夫 @endforeach
@endforeach
您可以在您的用例中使用Laravel collections

在控制器文件中,按
category\u id
键分组,并通过循环在刀片中渲染它们

控制器代码段:

public function someMethod(){
    $category  = [
        '1' => 'cat1',
        '2' => 'cat2'
    ];

    $product = [
        '0' => array(
         'name' => 'product1',
         'category_id' => '2'
    ),
        '1' => array(
         'name' => 'product2',
         'category_id' => '2'
    ),
        '2' => array(
         'name' => 'product3',
         'category_id' => '2'
    ),
        '3' => array(
         'name' => 'product4',
         'category_id' => '1'
    ),
        '4' => array(
         'name' => 'product5',
         'category_id' => '1'
    ),
        '5' => array(
         'name' => 'product6',
         'category_id' => '2'
    )]; 

    $category_collection = collect($product)->groupBy('category_id')->toArray();

    return view('your_blade_name',compact('category_collection','category'));
}
@foreach($category_collection as $category_id => $data)
    <h3>{{ $category[$category_id] }}</h3>
    @foreach($data as $current_data)
        <p>{{ $current_data['name'] }}</p>
    @endforeach
<hr>
<hr> 
@endforeach
刀片代码片段:

public function someMethod(){
    $category  = [
        '1' => 'cat1',
        '2' => 'cat2'
    ];

    $product = [
        '0' => array(
         'name' => 'product1',
         'category_id' => '2'
    ),
        '1' => array(
         'name' => 'product2',
         'category_id' => '2'
    ),
        '2' => array(
         'name' => 'product3',
         'category_id' => '2'
    ),
        '3' => array(
         'name' => 'product4',
         'category_id' => '1'
    ),
        '4' => array(
         'name' => 'product5',
         'category_id' => '1'
    ),
        '5' => array(
         'name' => 'product6',
         'category_id' => '2'
    )]; 

    $category_collection = collect($product)->groupBy('category_id')->toArray();

    return view('your_blade_name',compact('category_collection','category'));
}
@foreach($category_collection as $category_id => $data)
    <h3>{{ $category[$category_id] }}</h3>
    @foreach($data as $current_data)
        <p>{{ $current_data['name'] }}</p>
    @endforeach
<hr>
<hr> 
@endforeach
@foreach($category\u collection as$category\u id=>$data)
{{$category[$category_id]}
@foreach($data作为$current_数据)
{{$current_data['name']}

@endforeach

@endforeach
要在Laravel中执行此操作,应使用模型和关系。在普通php中,只需使用
$category
执行一个foreach循环,在该循环中,使用
$product
执行另一个循环。谢谢-如果我想按类别限制所有产品,使产品的计数都不超过4,那么我该怎么做,并按类别进行排序alphabet@aryatehran对不起,我没弄明白。你能不能把这个问题作为一个新问题加上所有必要的细节?