从多维数组获取特定信息

时间:2019-04-03 21:43:54

标签: php arrays json

我有这个多维数组,可以从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>

这是我从print_r得到的东西:

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
        )

)

我想输出来自:

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

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

2 个答案:

答案 0 :(得分:1)

我没有阅读完整的代码,但是您打开了这样的子数组:

<table>
                <tbody>
                @foreach($displayTickets as $t)
                    @if(empty($t->section))
                    <tr class="hide" data-id="{{$t->id}}">
                        <td><i class="fa fa-ticket" style="font-size: 16px" aria-hidden="true"></i><a href="/ticket/{{ $t->slug }}" class="subject-link">{{ $t->title }}</a>
                        </td>
                        <td>{{ $t->author }} {{$t->id}}</td>
                        <td>{{ $t->subject_area }}</td>
                        <td>{{ $t->created_at }}</td>
                        <td>{{ $t->reply_count }}</td>
                        <td>
                        @if($t->status == 'Open')
                            <span class="status-green">{{ $t->status }}</span>
                        @elseif($t->status == 'Closed')
                            <span class="status-red">{{ $t->status }}</span>
                        @endif
                        </td>
                     </tr>
                     @endif
                @endforeach
                </tbody>
            </table>

答案 1 :(得分:0)

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

即,代替

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

你应该做,

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