读取多维数组只打印子数组的第一个字符

Reading Multi Dimentional array only print 1st Charcter of Child Array

我只想在结果中显示 "Order Type",但我得到了二维数组的第一个字符。 它还显示警告“非法字符串偏移量'Order Type'

我的代码

<?php
$data=Array ( 

                "Order Type" => Array ( 'delivery' ),
                "When" => Array (  'Later' ),
                "Time" => Array (  '12:30' ),
                "Date" => Array (  'Sat, 17-Jan' ) 
            );

foreach($data as $innerArray)
{
    foreach($innerArray as $result)
        {
            echo $result["Order Type"];
        }
}

使用这个

foreach($data as $innerArray)
{
    foreach($innerArray as $result)
        {
            echo $result['delivery'];
        }
}

试试这个

 <?php
    $data=Array (
        "Order Type" => Array ( 'delivery' ),
        "When" => Array (  'Later' ),
        "Time" => Array (  '12:30' ),
        "Date" => Array (  'Sat, 17-Jan' )
    );

    foreach($data as $index => $innerArray)
    {
        if($index == "Order Type")
         echo $innerArray[0];
    }

或者直接写

 if(!empty($data["Order Type"][0]))
        echo $data["Order Type"][0];

如果您写 echo $data['Order Type'],您将收到 array('delivery'),因此您可以使用 $data['Order Type'][0]

foreach($data as $key => $innerArray)
    {
        if($key == "Order Type") {
            // if order type array will hold always single index value then use it
            echo $innerArray[0];
            // if it will hold also multiple indexes value then use
            foreach( $innerArray as $innerArrayVal) {
                echo $innerArrayVal;
            }
        }
    }