简体   繁体   中英

Extracting data from array in php

I need to extract address from $mydata variable.

i have this code which shows me my data:

foreach($landerssss['domains'] as $mydata)
{
    echo '<pre>' . print_r($mydata, TRUE) . '</pre>';}

in the above code there is no error the lines bellow shows whats inside the $mydata variable.

This is whats inside $mydata variable:

Array
(
    [0] => Array
        (
            [address] => aloran
            [mainDomain] => 
        )

    [1] => Array
        (
            [address] => ozamiz
            [mainDomain] => 
        )

    [2] => Array
        (
            [address] => manila
            [mainDomain] => 
        )

    [3] => Array
        (
            [address] => cebu
            [mainDomain] => 
        )

)

Array
(
    [0] => Array
        (
            [address] => bacolod
            [mainDomain] => 
        )

    [1] => Array
        (
            [address] => bukidnon
            [mainDomain] => 1
        )

    [2] => Array
        (
            [address] => davao
            [mainDomain] => 
        )

    [3] => Array
        (
            [address] => boracay
            [mainDomain] => 
        )

    [4] => Array
        (
            [address] => palawan
            [mainDomain] => 
        )

    [5] => Array
        (
            [address] => bohol
            [mainDomain] => 
        )

    [6] => Array
        (
            [address] => calabarson
            [mainDomain] => 
        )

    [7] => Array
        (
            [address] => NCR
            [mainDomain] => 
        )

    [8] => Array
        (
            [address] => baguio
            [mainDomain] => 
        )

    [9] => Array
        (
            [address] => manila
            [mainDomain] => 
        )

    [10] => Array
        (
            [address] => cagayan
            [mainDomain] => 
        )

    [11] => Array
        (
            [address] => iligan
            [mainDomain] => 
        )

    [12] => Array
        (
            [address] => sindangan
            [mainDomain] => 
        )

    [13] => Array
        (
            [address] => dipolog
            [mainDomain] => 
        )

    [14] => Array
        (
            [address] => calamba
            [mainDomain] => 
        )

)

i need to extract all address so i tried using

foreach ($mydata[0]['sddress'] as $key) {

    }

but iam recieving errors:

Warning: Illegal string offset 'address' in /srv/users/serverpilot/apps/ruleswitcherandchecker/public/CTAchecker/index.php on line 131

Warning: Invalid argument supplied for foreach() in /srv/users/serverpilot/apps/ruleswitcherandchecker/public/CTAchecker/index.php on line 131

please help :(

Looking at that dump it seems you just need another set of foreach since its nested:

foreach($landerssss['domains'] as $mydata) {
    foreach ($mydata as $key) {
        echo $key['address']; // and so forth
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM