使用PHP将项目循环并计数到多维数组中

我有这个数组:


Array

(

    [USA] => Array

        (

            [0] => Array

                (

                    [Name] => Apple

                    [Item] => Phones and Computers

                )

            [1] => Array

                (

                    [Name] => McDonalds

                    [Item] => Food

                )


        )

    [China] => Array

        (

            [0] => Array

                (

                    [Name] => Lenovo

                    [Item] => Computers

                )

        )

)

我想介绍一下它,并回显count每个国家(美国和中国)的商品。


所以我的尝试是这样的:


foreach ($arr as $key => $value) {

    echo count($arr[$value]);

    echo '<br />';


    foreach ($value as $subkey => $subvalue) {

        echo $value." -> ".$subvalue['Name']." make ".$subvalue['Item']."<br />";    

    }

}


UYOU
浏览 157回答 1
1回答

ABOUTYOU

你可以这样做:foreach ($arr as $country => $companies) {&nbsp; &nbsp; echo $country . " has " . count($companies) . " item <br />";&nbsp; &nbsp; foreach ($companies as $k => $company) {&nbsp; &nbsp; &nbsp; &nbsp; echo $company['Name'] . " make " . $company['Item']."<br />";&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }}为变量保留有意义的名称可以使生活更加轻松
打开App,查看更多内容
随时随地看视频慕课网APP