我只想展示"订单类型"在我的结果中,但我得到了我的第二维数组的第一个字符。 它还显示" 非法字符串偏移'订单类型' "
的警告我的代码
<?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"];
}
}
答案 0 :(得分:1)
试试这个
<?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];
答案 1 :(得分:1)
如果您写echo $data['Order Type']
,我们会收到array('delivery')
,因此您可以使用$data['Order Type'][0]
答案 2 :(得分:0)
使用此
foreach($data as $innerArray)
{
foreach($innerArray as $result)
{
echo $result['delivery'];
}
}
答案 3 :(得分: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;
}
}
}