1

So I have this xml file;

<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>

I have a function that parses the xml file into an array;

<?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 />';
    }
}

?>

When there are two <shop> data, the script returns the details for both. But until I remove 1 <shop> the script gives error, Illegal string offset .. probably because the array is not correct.

I tried to display the array via echo so that you can see the results, you can also try to remove 1 .

This is the result for print_r($shops) if the xml file has 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
                                )

                        )

                )

        )

)

This is the result for print_r($shops) if the xml file has only one <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
                        (
                            [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
                        )

                )

        )

)

This is the full error if the xml file has only one <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
3
  • Please give us the full error message, hinting the line of code it's pointing to (we don't have access to the file names and line numbers as you do) Commented Sep 13, 2017 at 5:13
  • @AlivetoDie sorry $limit is just an excess parameter, it's useless, I edited the contents. I also added the full error. Commented Sep 13, 2017 at 5:18
  • @Calimero I added the results. check again Commented Sep 13, 2017 at 5:23

2 Answers 2

2

you need to check array is single dimension or multi-dimension your correct code is

<?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 />';
    }
}
?>
Sign up to request clarification or add additional context in comments.

4 Comments

escaping from errors is not correct at-all. don't escape, try to found-it and resolve it.
any error messages that might be generated by that expression will be ignored->why to ignore if you can resolved it? So did you always leave notice,warning as it is?
it give only warning error so i used only for reducing the complexity of code.
every-one know that warning doesn't stop execution of the script. But a good code resolve those too. Otherwise any-one can use ini_set('display_errors',0); to stop showing that warning, notices.I am not scolding you. Just suggesting you that write a code in a way that it doesn't show any error even when error_reporting is on for all types of error.
1

You need to check first that $shops['shops']['basead']['shop'] is multi-dimensional array or not? If yes then use foreach() to print otherwise use echo directly like below:-

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 />';
        }
    }
}

Reference:-Checking if array is multidimensional or not?

3 Comments

yes that is correct, I have to check if has 2 or more counts of shop.
thank you! I will now try to re-run my script with your corrections in it.
@JohnDaleOcayaAndilDale glad to help you.please check the edited answer (removed unnecessary variable creation)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.