0

I have array which contents are;

[@attributes] => Array
    (
        [type] => 1
    )
[shops] => Array
    (
        [basead] => Array
            (
                [shop] => Array
                    (
                        [0] => Array
                            (
                                [id] => KN0100060500211230
                                [entryname] => 陽陽ラーメン本店東栄店
                                [searchnum] => 7187766
                            )

                        [1] => Array
                            (
                                [id] => KN0114102400000170
                                [entryname] => 炭火焼肉たんたん
                                [searchnum] => 7187766
                            )
                    )

            )

    )

I have this code to get the values I wanted;

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

How can I do loop to get the values I want from the array List?

1
  • by doing a foreach on $company['shops']['basead']['shop'] I should think. Commented Aug 30, 2017 at 10:14

2 Answers 2

2

try like this

foreach ( $hascompany['shops']['basead']['shop'] as $company ) {
            $companyId = $company['id'];
            $entryName = $company['entryname'];
            $searchNum = $company['searchNum'];
}
Sign up to request clarification or add additional context in comments.

2 Comments

what if the data returned has only 1 <shop>? how should I do the loop? because this gives an error?
put condition before it like if(is_array($hascompany['shops']['basead']['shop'])){ loop here } else { single record code here }
0

Try this

    foreach ( $hascompany['shops']['basead']['shop'] as $shop ) {

         $companyId = $shop['id'];
         $entryName = $shop['entryname'];
         $priority = $shop['priority'];
         $searchNum = $shop['searchNum'];

    }

Comments

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.