Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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_Json - Fatal编程技术网

Php 从多维数组获取特定信息

Php 从多维数组获取特定信息,php,arrays,json,Php,Arrays,Json,我有这个多维数组,我从一个JSON字符串解码,现在我想输出一些数据,但我不能?如果是单个数组,这不是问题 我已经尝试了一些foreach循环,但是我无法让它输出我想要的数据。我一直得到一个非法的偏移错误 $someArray = array ( 'result' => 'success', 'response' => array ( 'server_info' => array ( 'status' =>

我有这个多维数组,我从一个JSON字符串解码,现在我想输出一些数据,但我不能?如果是单个数组,这不是问题

我已经尝试了一些foreach循环,但是我无法让它输出我想要的数据。我一直得到一个非法的偏移错误

$someArray = array (
    'result' => 'success',
    'response' => 
    array (
      'server_info' => 
      array (
        'status' => 'success',
        'ipaddress' => '123.123.123.123',
        'hostname' => 'hostname.example.com',
        'template' => 'linux-centos-6.5-x86_64-min-gen2-v1',
        'hdd' => '32212254720',
        'memory' => '1073741824',
        'country' => 'FR',
        'city' => 'Paris',
        'plan' => 'Plan 02',
        'rootpassword' => '1q2w3e4r5t',
      ),
      'server_state' => 
      array (
        'status' => 'success',
        'state' => 'online',
        'mainipaddress' => '123.123.123.123',
        'ipaddresses' => '123.123.123.123',
        'bandwidth' => '2199023255552,112133881,2198911121671,0',
        'hdd' => '32212254720,0,32212254720,0',
        'pae' => 'off',
        'bootorder' => 'cd',
        'iso' => '',
      ),
      'templates' => 
      array (
        'linux-centos-7.1503.01-x86_64-minimal-gen2-v1' => 
        array (
          'name' => 'centos',
          'display' => 'CentOS 7.1503.01 64bits minimal  ',
        ),
        'linux-clearos-6.3-x86_64-gen2-v1' => 
        array (
          'name' => 'clearos',
          'display' => 'ClearOS 6.3 64bits  ',
        ),
        'linux-debian-6.0.6-x86_64-min-gen2-v1' => 
        array (
          'name' => 'debian',
          'display' => 'Debian 6.0.6 64bits ',
        ),
      ),
      'one_click_apps' => 
      array (
        'linux-ubuntu-14.04-server-x86_64-min-gen2-v1-oneapp-webuzo' => 
        array (
          'name' => 'Webuzo',
          'display' => 'Webuzo',
          'time' => '2',
          'version' => '2.5',
          'desc' => 'Webuzo is a LAMP STACK and a Single User Control Panel, it simplifies the development and deployment of your project by supporting more than 400 applications and 1115 classes. ',
          'steps' => 'You will be able to access the Webuzo setup and dashboard by entering the following syntax in your web browser: http://IP:2004',
          'os' => 'Ubuntu 14.04  64bits ',
        ),
      ),
      'iso_images' => 
      array (
        0 => 'FreeBSD-9.1-RELEASE-amd64-disc1',
        1 => 'CentOS-7.0-1406-x86_64-Minimal',
        2 => 'FreeBSD-9.3-RELEASE-amd64-bootonly',
        3 => 'FreeBSD-10.1-RELEASE-amd64-disc1',
        4 => 'FreeBSD-8.4-RELEASE-amd64-disc1',
      ),
      'log' => 
      array (
        0 => 
        array (
          'log' => '[Reinstall] - Virtual server: 12345 - App: Drupal on ubuntu 14.04 64bits ',
          'time' => '2042-04-02 16:20:42',
        ),
        1 => 
        array (
          'log' => '[Reinstall] - Virtual server: 12345 - OS: Ubuntu 14.04 server 64bits ',
          'time' => '2042-04-20 04:20:42',
        ),
        2 => 
        array (
          'log' => '[Change hostname] - Virtual server: 12345 - Hostname: new.hostname.com',
          'time' => '2042-04-02 04:20:00',
        ),
      ),
      'server_billing' => 
      array (
        'reg_date' => '2015-03-12',
        'next_due_date' => '2015-04-12',
        'payment_method' => 'paypal',
        'first_payment_amount' => '0.00',
        'recurring_amount' => '0.00',
        'billing_cycle' => 'Monthly',
        'status' => 'Active',
      ),
      'server_install' => 0,
    ),
);


  $array = $someArray; 
  foreach($array as $key => $value) {
    echo $array[$key]["status"] .  "<br>";
  }

</code>
我想从以下方面进行输出:

Server_info
-status
-ipaddress
-and so on
server_state
-same here

但是我保留了一个非法的偏移量错误,或者它输出空值

您只需迭代
$array['response']
,而不是
$array
本身,因为它无法在$array['result']中找到“status”

即,代替

foreach($array as $key => $value) {
    echo $array[$key]["status"] .  "<br>";
  }
foreach($key=>$value的数组){
echo$array[$key][“status”]。“
”; }
你应该这样做

if(isset($array['response'])) {
    foreach($array['response'] as $key => $value) {
        echo $value[$key]["status"] .  "<br>";
    }
}
if(isset($array['response'])){
foreach($key=>$value的数组['response']as$key){
回显$value[$key][“状态”]。“
”; } }
我没有阅读全部代码,但您打开子阵列时如下所示:

$subarray_value = $array['subarray']['deeper']['even_more_below'];

$status=$someArray['response']['server_info']['status']好的,很简单。非常感谢。我甚至没想过。
$subarray_value = $array['subarray']['deeper']['even_more_below'];