Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.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,我的PHP数组如下所示: $countryList = array ( array( // Asia continent => 'Asia', country => array('Japan', 'China') ), array( // Europe continent => 'Europe', country => array('Spain', 'France', 'I

我的PHP数组如下所示:

$countryList = array (
    array( // Asia
        continent => 'Asia', 
        country   => array('Japan', 'China')
    ),
    array( // Europe
        continent => 'Europe', 
        country   => array('Spain', 'France', 'Italy')
    )
);
$countryList = array(
    'Asia'   => array('Japan, China'),
    'Europe' => array('Spain', 'France', 'Italy'),
);
我如何调用这个数组$countryList来询问如果大陆是“亚洲”,国家的价值是多少

我想要一些类似于:

$country = 'Japan, China';
非常感谢

$countryList = array (
    array( // Asia
        'continent' => 'Asia', 
        'country'   => array('Japan', 'China')
    ),
    array( // Europe
        'continent' => 'Europe', 
        'country'   => array('Spain', 'France', 'Italy')
    )
);

$continent = 'Asia';

foreach($countryList as $c)
  if ($c['continent'] == $continent)
  {
      echo join(', ', $c['country']);
      break;
  }
但是使用关联数组更好、更容易

$countryList = array (
    'Asia'   => array('Japan', 'China'),
    'Europe' => array('Spain', 'France', 'Italy')
);

$continent = 'Asia';

echo isset($countryList[$continent]) ?
         join(', ', $countryList[$continent]) :
         'No such continent';

最后一个回声与if等效。。然后结构并检查数组中是否存在具有相应键的元素。

您可以用这种方式内爆数组

$country = implode(', ', countryList['Asia']);

问候语

您应该将数据的结构更改为以下内容:

$countryList = array (
    array( // Asia
        continent => 'Asia', 
        country   => array('Japan', 'China')
    ),
    array( // Europe
        continent => 'Europe', 
        country   => array('Spain', 'France', 'Italy')
    )
);
$countryList = array(
    'Asia'   => array('Japan, China'),
    'Europe' => array('Spain', 'France', 'Italy'),
);
这样,您就可以直接访问阵列,而不必搜索阵列:

$region = 'Europe';
$countries = implode(', ', $countryList[$region]);
echo "Europe countries: {$countries}.";

可以使数组的基本索引为大陆名称。例如,`$countryList=数组'europe'=>数组'Spain'、'France'、'asia'=>数组;这样可以更容易地获取其他数据。我在手机上,但pop的数组搜索键功能可以工作吗?@Jhecht-nope,这些不是键。数组搜索返回相应的键,我的意思是。自动更正。同样,这也不起作用,因为他的数组的状态。@Jhecht isset比搜索键好得多,速度也快得多。考虑到最初的问题,这不起作用,因为Asia不是该数组声明的有效索引。不要将其与另一个答案中的数组解决方案混淆抱歉,我没有看到前面的答案,这是更明确的