使用foreach在Array中获取数据

时间:2017-09-13 05:09:57

标签: php arrays xml foreach

所以我有这个xml文件;

<Response type="1">
<ack>
    <ackstatus>OK</ackstatus>
    <ackreason></ackreason>
</ack>
<dataversion>1</dataversion>
<hitcount>13391</hitcount>
<shops>
    <spad>
                </spad>
    <basead>
        <shop>
            <id>KN0100060500216636</id>
            <priority>1</priority>
            <entryname>旭川職業能力開発促進センター</entryname>
            <telno>0166-48-2412</telno>
            <zipcode>079-8418</zipcode>
            <address>北海道旭川市永山8条20丁目3−1</address>
            <latitude>157707796</latitude>
            <longitude>512803967</longitude>
            <imageurl></imageurl>
            <promoword></promoword>
            <coupon>0</coupon>
            <group>0</group>
            <searchnum>7215270</searchnum>
        </shop>
        <shop>
            <id>KN0100060500202256</id>
            <priority>1</priority>
            <entryname>旭川市立/嵐山小中学校</entryname>
            <telno>0166-61-1199</telno>
            <zipcode>070-8051</zipcode>
            <address>北海道旭川市江丹別町嵐山143</address>
            <latitude>157704475</latitude>
            <longitude>512195888</longitude>
            <imageurl></imageurl>
            <promoword></promoword>
            <coupon>0</coupon>
            <group>0</group>
            <searchnum>7215270</searchnum>
        </shop>
    </basead>
</shops>
</Response>

我有一个将xml文件解析为数组的函数;

<?php

$xml = simplexml_load_file('hascompany.xml');
$json = json_encode($xml);
$shops = json_decode($json, true);

echo "<pre>";
print_r($shops);
echo "</br></br>";
print_r(count($shops['shops']['basead']['shop']));
echo "</br>";
print_r(count($shops['shops']['basead']));
echo "</br></br>";

logCompanyData( $shops );

function logCompanyData( $shops )
{
    foreach ( $shops['shops']['basead']['shop'] as $company )
    {
        $companyId = $company['id'];
        $entryName = $company['entryname'];
        $priority  = $company['priority'];
        $searchNum = $company['searchnum'];

        echo 'company_id = ' . $companyId . '<br />';
        echo 'company_name = ' . $entryName . '<br />';
        echo 'company_prio = ' . $priority . '<br />';
        echo 'company_searchno = ' . $searchNum . '<br /><br />';
    }
}

?>

当有两个<shop>数据时,脚本会返回两者的详细信息。但是在我删除1 <shop>之前,脚本会出错,非法字符串偏移..可能是因为数组不正确。

我尝试通过echo显示数组,以便您可以看到结果,也可以尝试删除1.

这是print_r($ shops)的结果,如果xml文件有2 <shop>;

Array
(
    [@attributes] => Array
        (
            [type] => 1
        )

    [ack] => Array
        (
            [ackstatus] => OK
            [ackreason] => Array
                (
                )

        )

    [dataversion] => 1
    [hitcount] => 13391
    [shops] => Array
        (
            [spad] => Array
                (
                    [0] => 

                )

            [basead] => Array
                (
                    [shop] => Array
                        (
                            [0] => Array
                                (
                                    [id] => KN0100060500206405
                                    [priority] => 1
                                    [entryname] => 旭川市立/台場小学校
                                    [telno] => Array
                                        (
                                        )

                                    [zipcode] => 070-8022
                                    [address] => 北海道旭川市神居町台場274
                                    [latitude] => 157575990
                                    [longitude] => 512224612
                                    [imageurl] => Array
                                        (
                                        )

                                    [promoword] => Array
                                        (
                                        )

                                    [coupon] => 0
                                    [group] => 1
                                    [searchnum] => 7211472
                                )

                            [1] => Array
                                (
                                    [id] => KN0100060500216326
                                    [priority] => 1
                                    [entryname] => 旭川市立/永山小学校
                                    [telno] => Array
                                        (
                                        )

                                    [zipcode] => 079-8415
                                    [address] => 北海道旭川市永山5条18丁目2−1
                                    [latitude] => 157707415
                                    [longitude] => 512772640
                                    [imageurl] => Array
                                        (
                                        )

                                    [promoword] => Array
                                        (
                                        )

                                    [coupon] => 0
                                    [group] => 1
                                    [searchnum] => 7211472
                                )

                        )

                )

        )

)

如果xml文件只有一个<shop>,则这是print_r($ shops)的结果;

Array
(
    [@attributes] => Array
        (
            [type] => 1
        )

    [ack] => Array
        (
            [ackstatus] => OK
            [ackreason] => Array
                (
                )

        )

    [dataversion] => 1
    [hitcount] => 13391
    [shops] => Array
        (
            [spad] => Array
                (
                    [0] => 

                )

            [basead] => Array
                (
                    [shop] => Array
                        (
                            [id] => KN0100060500206405
                            [priority] => 1
                            [entryname] => 旭川市立/台場小学校
                            [telno] => Array
                                (
                                )

                            [zipcode] => 070-8022
                            [address] => 北海道旭川市神居町台場274
                            [latitude] => 157575990
                            [longitude] => 512224612
                            [imageurl] => Array
                                (
                                )

                            [promoword] => Array
                                (
                                )

                            [coupon] => 0
                            [group] => 1
                            [searchnum] => 7211472
                        )

                )

        )

)

如果xml文件只有一个<shop>

,则为完整错误
Warning:  Illegal string offset 'id' in C:\xampp\htdocs\demo\demoout.php on line 21



Warning:  Illegal string offset 'entryname' in C:\xampp\htdocs\demo\demoout.php on line 22



Warning:  Illegal string offset 'priority' in C:\xampp\htdocs\demo\demoout.php on line 23



Warning:  Illegal string offset 'searchnum' in C:\xampp\htdocs\demo\demoout.php on line 24

company_id = K
company_name = K
company_prio = K
company_searchno = K



Warning:  Illegal string offset 'id' in C:\xampp\htdocs\demo\demoout.php on line 21



Warning:  Illegal string offset 'entryname' in C:\xampp\htdocs\demo\demoout.php on line 22



Warning:  Illegal string offset 'priority' in C:\xampp\htdocs\demo\demoout.php on line 23



Warning:  Illegal string offset 'searchnum' in C:\xampp\htdocs\demo\demoout.php on line 24

company_id = 1
company_name = 1
company_prio = 1
company_searchno = 1



Warning:  Illegal string offset 'id' in C:\xampp\htdocs\demo\demoout.php on line 21



Warning:  Illegal string offset 'entryname' in C:\xampp\htdocs\demo\demoout.php on line 22



Warning:  Illegal string offset 'priority' in C:\xampp\htdocs\demo\demoout.php on line 23



Warning:  Illegal string offset 'searchnum' in C:\xampp\htdocs\demo\demoout.php on line 24

company_id = �
company_name = �
company_prio = �
company_searchno = �



Notice:  Undefined index: id in C:\xampp\htdocs\demo\demoout.php on line 21



Notice:  Undefined index: entryname in C:\xampp\htdocs\demo\demoout.php on line 22



Notice:  Undefined index: priority in C:\xampp\htdocs\demo\demoout.php on line 23



Notice:  Undefined index: searchnum in C:\xampp\htdocs\demo\demoout.php on line 24

company_id = 
company_name = 
company_prio = 
company_searchno = 



Warning:  Illegal string offset 'id' in C:\xampp\htdocs\demo\demoout.php on line 21



Warning:  Illegal string offset 'entryname' in C:\xampp\htdocs\demo\demoout.php on line 22



Warning:  Illegal string offset 'priority' in C:\xampp\htdocs\demo\demoout.php on line 23



Warning:  Illegal string offset 'searchnum' in C:\xampp\htdocs\demo\demoout.php on line 24

company_id = 0
company_name = 0
company_prio = 0
company_searchno = 0



Warning:  Illegal string offset 'id' in C:\xampp\htdocs\demo\demoout.php on line 21



Warning:  Illegal string offset 'entryname' in C:\xampp\htdocs\demo\demoout.php on line 22



Warning:  Illegal string offset 'priority' in C:\xampp\htdocs\demo\demoout.php on line 23



Warning:  Illegal string offset 'searchnum' in C:\xampp\htdocs\demo\demoout.php on line 24

company_id = �
company_name = �
company_prio = �
company_searchno = �



Warning:  Illegal string offset 'id' in C:\xampp\htdocs\demo\demoout.php on line 21



Warning:  Illegal string offset 'entryname' in C:\xampp\htdocs\demo\demoout.php on line 22



Warning:  Illegal string offset 'priority' in C:\xampp\htdocs\demo\demoout.php on line 23



Warning:  Illegal string offset 'searchnum' in C:\xampp\htdocs\demo\demoout.php on line 24

company_id = 1
company_name = 1
company_prio = 1
company_searchno = 1



Warning:  Illegal string offset 'id' in C:\xampp\htdocs\demo\demoout.php on line 21



Warning:  Illegal string offset 'entryname' in C:\xampp\htdocs\demo\demoout.php on line 22



Warning:  Illegal string offset 'priority' in C:\xampp\htdocs\demo\demoout.php on line 23



Warning:  Illegal string offset 'searchnum' in C:\xampp\htdocs\demo\demoout.php on line 24

company_id = 5
company_name = 5
company_prio = 5
company_searchno = 5



Notice:  Undefined index: id in C:\xampp\htdocs\demo\demoout.php on line 21



Notice:  Undefined index: entryname in C:\xampp\htdocs\demo\demoout.php on line 22



Notice:  Undefined index: priority in C:\xampp\htdocs\demo\demoout.php on line 23



Notice:  Undefined index: searchnum in C:\xampp\htdocs\demo\demoout.php on line 24

company_id = 
company_name = 
company_prio = 
company_searchno = 



Notice:  Undefined index: id in C:\xampp\htdocs\demo\demoout.php on line 21



Notice:  Undefined index: entryname in C:\xampp\htdocs\demo\demoout.php on line 22



Notice:  Undefined index: priority in C:\xampp\htdocs\demo\demoout.php on line 23



Notice:  Undefined index: searchnum in C:\xampp\htdocs\demo\demoout.php on line 24

company_id = 
company_name = 
company_prio = 
company_searchno = 



Warning:  Illegal string offset 'id' in C:\xampp\htdocs\demo\demoout.php on line 21



Warning:  Illegal string offset 'entryname' in C:\xampp\htdocs\demo\demoout.php on line 22



Warning:  Illegal string offset 'priority' in C:\xampp\htdocs\demo\demoout.php on line 23



Warning:  Illegal string offset 'searchnum' in C:\xampp\htdocs\demo\demoout.php on line 24

company_id = 0
company_name = 0
company_prio = 0
company_searchno = 0



Warning:  Illegal string offset 'id' in C:\xampp\htdocs\demo\demoout.php on line 21



Warning:  Illegal string offset 'entryname' in C:\xampp\htdocs\demo\demoout.php on line 22



Warning:  Illegal string offset 'priority' in C:\xampp\htdocs\demo\demoout.php on line 23



Warning:  Illegal string offset 'searchnum' in C:\xampp\htdocs\demo\demoout.php on line 24

company_id = 1
company_name = 1
company_prio = 1
company_searchno = 1



Warning:  Illegal string offset 'id' in C:\xampp\htdocs\demo\demoout.php on line 21



Warning:  Illegal string offset 'entryname' in C:\xampp\htdocs\demo\demoout.php on line 22



Warning:  Illegal string offset 'priority' in C:\xampp\htdocs\demo\demoout.php on line 23



Warning:  Illegal string offset 'searchnum' in C:\xampp\htdocs\demo\demoout.php on line 24

company_id = 7
company_name = 7
company_prio = 7
company_searchno = 7

2 个答案:

答案 0 :(得分:2)

您需要检查数组是单维还是多维的正确代码

<?php
$xml = simplexml_load_file('shop.xml');
$json = json_encode($xml);
$shops = json_decode($json, true);

echo "<pre>";
print_r($shops);
echo "</br></br>";
print_r(count($shops['shops']['basead']['shop']));
echo "</br>";
print_r(count($shops['shops']['basead']));
echo "</br></br>";

logCompanyData( $shops, $limit=1);

function logCompanyData( $shops, $limit )
{
    $array_data = array();
    if(is_array(@$shops['shops']['basead']['shop'][0])){
        $array_data = $shops['shops']['basead']['shop']; 
    }else{
        $array_data = array($shops['shops']['basead']['shop']);
    }
    foreach ( $shops['shops']['basead']['shop'] as $company )
    {
         // /echo "<pre>"; print_r( $company); die();
        $companyId = $company['id'];
        $entryName = $company['entryname'];
        $priority  = $company['priority'];
        $searchNum = $company['searchnum'];

        echo 'company_id = ' . $companyId . '<br />';
        echo 'company_name = ' . $entryName . '<br />';
        echo 'company_prio = ' . $priority . '<br />';
        echo 'company_searchno = ' . $searchNum . '<br /><br />';
    }
}
?>

答案 1 :(得分:1)

您需要首先检查$shops['shops']['basead']['shop']是否为多维数组?如果是,则使用foreach()进行打印,否则直接使用echo,如下所示: -

function logCompanyData( $shops ){

    if(count($shops['shops']['basead']['shop']) == count($shops['shops']['basead']['shop'], COUNT_RECURSIVE)){
        echo 'company_id = ' . $shops['shops']['basead']['shop']['id'] . '<br />';
        echo 'company_name = ' . $shops['shops']['basead']['shop']['entryname'] . '<br />';
        echo 'company_prio = ' . $shops['shops']['basead']['shop']['priority'] . '<br />';
        echo 'company_searchno = ' . $shops['shops']['basead']['shop']['searchnum'] . '<br /><br />';
}else{

        foreach ( $shops['shops']['basead']['shop'] as $company )
        {
            echo 'company_id = ' . $company['id'] . '<br />';
            echo 'company_name = ' . $company['entryname'] . '<br />';
            echo 'company_prio = ' . $company['priority'] . '<br />';
            echo 'company_searchno = ' . $company['searchnum'] . '<br /><br />';
        }
    }
}

参考: - Checking if array is multidimensional or not?