Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从PHP数组中获取子数组_Php_Arrays - Fatal编程技术网

如何从PHP数组中获取子数组

如何从PHP数组中获取子数组,php,arrays,Php,Arrays,我得到的数组是: Array ( [country_location] => Array ( [0] => Array ( [location] => India ) [1] => Array ( [location] => United States of America

我得到的数组是:

Array
(
  [country_location] => Array
    (
        [0] => Array
            (
                [location] => India
            )

        [1] => Array
            (
                [location] => United States of America
            )

        [2] => Array
            (
                [location] => South Africa
            )

    )

  [region_location] => Array
    (
        [0] => Array
            (
                [regionname] => Africa
            )

        [1] => Array
            (
                [regionname] => Antarctica
            )

        [2] => Array
            (
                [regionname] => Asia
            )

        [3] => Array
            (
                [regionname] => Europe
            )

        [4] => Array
            (
                [regionname] => North america
            )

        [5] => Array
            (
                [regionname] => Oceania (Australia)
            )

        [6] => Array
            (
                [regionname] => South america
            )

    )

)
我尝试使用$arrayname['country\u location']获取值,但它显示的消息是:非法字符串偏移量'country\u location'。

尝试以下操作:

$country_location = [];
$region = [];
foreach($b as $key => $val) {
    foreach($val as $key2 => $val2) {
        $country_location[] = $val2['location'];
        $region_location[] = $val2['regionname'];
    }
}

print_r($country_location);
print_r($region);

你的问题是。。。?如何访问内部数组?您希望得到什么结果?我们可以有一些代码吗?您看到的错误是因为您试图访问字符串上的数组键。。。因此,要么您使用了错误的变量,要么它没有预期的内容……请使用以下命令:
highlight_string(print_r($arrayname,TRUE))
并查看键中是否有空格或隐藏的html标记。我需要子数组country_location和region_location@Wojciech Jasiński。我尝试使用foreach,但为foreach()提供的参数无效错误消息。@Yash这表示您没有数组或正在对我们隐藏某些内容。@Rizier123我是从文件_get_contents(“);@Yash得到这个数组的:
var\u dump($arrayname);
@Rizier123我得到了数组结果的“string(1211)”。当我使用var\u dump($myarrayname)时;