Array ( [data] => Array ( [0] => Array ( [ucsfeduworkingdepartmentname] => Array ( [0] => ITS ) [telephonenumber] => Array ( [0] => +1 415 502-7575 ) [ucsfeduprofilenodeid] => Array ( [0] => 39487740 ) [displayname] => Array ( [0] => Kevin Dale ) [postaladdress] => Array ( [0] => Box 0272 1855 Folsom Street, MCB Room 401S San Francisco, CA 94143 ) [uid] => Array ( [0] => 88834 ) [ucsfeduprimarydepartmentnumber] => Array ( [0] => 411112 ) [ucsfeduworkingtitle] => Array ( [0] => Sr Manager, Identity Mgmt ) [mobile] => Array ( [0] => +1 415 806-8480 ) [roomnumber] => Array ( [0] => 401S ) [mail] => Array ( [0] => kevin.dale@ucsf.edu ) [box] => Array ( [0] => Box 0272 ) [baseaddress] => Array ( [0] => 1855 Folsom Street San Francisco, CA 94143 ) [primary] => Array ( [box] => Array ( [0] => Box 0272 ) [building] => Array ( [0] => MCB ) [baseaddress] => Array ( [0] => 1855 Folsom Street San Francisco, CA 94143 ) [postaladdress] => Array ( [0] => Box 0272 1855 Folsom Street, MCB Room 401S San Francisco, CA 94143 ) [cn] => Array ( [0] => Campus ) [ucsfeduaddressprimaryflag] => Array ( [0] => true ) [roomnumber] => Array ( [0] => 401S ) [telephonenumber] => Array ( [0] => +1 415 502-7575 ) [ucsfedusecondarytelephonenumber] => Array ( [0] => ) [ucsfedutelephonenumberreleasecode] => Array ( [0] => ) [ucsfedusecondarytelephonenumberreleasecode] => Array ( [0] => ) ) [ucsfeduprimarydepartmentname] => Array ( [0] => F_IT Identity and Access Mgt ) [departmentname] => Array ( [0] => F_IT Identity and Access Mgt ) ) ) ) ----

Recommended Answers

$int = 1;

foreach($json_info as $key => $value)
{

    echo "ARRAY " . $int;

    foreach ($value as $k => $v)
    {
        print_r($k . ": " . $v);
    }

    $int = $int + 1; // I couldn't be bothered making it simpler for me.

}

That should …

Jump to Post
Answered by jsuna 10 in a post from

For the array posted you would do...

foreach($json_info as $key=>$value){
    //use "\n" instead of "<br />" if printing in console
    echo $key."<br />"; //data
    /* each value is an array so need to use the index, 
    otherwise it'll just print 'Array' */
    foreach($value as $k=>$v)
        echo $k." …
Jump to Post

jsuna — I already posted basically the same thing as you did, no need to add to it. Mine should work fine, and is better in terms of syntax.

Jump to Post

All 11 Replies

foreach($json_info as $key => $value)
{

 print_r($value);

}

Gives me back same assoc array??

$int = 1;

foreach($json_info as $key => $value)
{

    echo "ARRAY " . $int;

    foreach ($value as $k => $v)
    {
        print_r($k . ": " . $v);
    }

    $int = $int + 1; // I couldn't be bothered making it simpler for me.

}

That should do it.

For the array posted you would do...

foreach($json_info as $key=>$value){
    //use "\n" instead of "<br />" if printing in console
    echo $key."<br />"; //data
    /* each value is an array so need to use the index, 
    otherwise it'll just print 'Array' */
    foreach($value as $k=>$v)
        echo $k." :: ".$v[0]."<br />";
}

if you just need to see what the array contains or how it's structured via a browser...

echo '<pre>';
print_r($json_info);
echo '</pre>';

Hope this helps!

jsuna — I already posted basically the same thing as you did, no need to add to it. Mine should work fine, and is better in terms of syntax.

@raminshahab, here is the array equivalent and you can try either one of the proposed solutions above. I know which one will work and which one will not. Your job now to test each.

$json_info = array('data'=> array(
            'ucsfeduworkingdepartmentname'=>array('ITS'),
            'telephonenumber'=>array('+1 415 502-7575'),
            'ucsfeduprofilenodeid' => Array ('39487740' ),
            'displayname' => Array ('Kevin Dale' ),
            'postaladdress'=>array('Box 0272 1855 Folsom Street, MCB Room 401S San Francisco, CA 94143'), 
            'uid' => Array ('88834' ) ,
            'ucsfeduprimarydepartmentnumber' => Array ('411112' ),
            'ucsfeduworkingtitle' => Array ( 'Sr Manager, Identity Mgmt' ) ,
            'mobile' => Array ( '1 415 806-8480' ), 
            'roomnumber' => Array ('401S' ) ,   
            'mail' => Array ('kevin.dale@ucsf.edu' ), 
            'box' => Array ( 'Box 0272' ), 
            'baseaddress' => Array ('1855 Folsom Street San Francisco, CA 94143' ), 
            'primary' => Array ( 'box' => 'Box 0272' ), 
            'building' => Array ( 'MCB' ), 
            'baseaddress' => Array ( '1855 Folsom Street San Francisco, CA 94143' ), 
            'postaladdress' => Array ( 'Box 0272 1855 Folsom Street, MCB Room 401S San Francisco, CA 94143' ),
            'cn' => Array ( 'Campus' ), 
            'ucsfeduaddressprimaryflag' => Array ('true' ),
            'roomnumber' => Array ( '401S' ), 
            'telephonenumber' => Array ( '+1 415 502-7575' ), 
            'ucsfedusecondarytelephonenumber' => Array ('') ,
            'ucsfedutelephonenumberreleasecode' => Array ( ''), 
            'ucsfedusecondarytelephonenumberreleasecode' => Array ( '')  ,
            'ucsfeduprimarydepartmentname' => Array ('F_IT Identity and Access Mgt' ), 
            'departmentname' => Array ('F_IT Identity and Access Mgt' )



));

good luck to you.

matrixdevuk - yeah it 'would' work if the inner array didn't have arrays for values... as it stands your code would print 'Array' where $v occurs...and as far as syntax...print_r($k.':'.$v); ??

Oh, right! I didn't realise it was Array -> Array -> Array.

I will update my code.

Code update:

$int = 1;

foreach($json_info as $key => $value)
{

    echo "ARRAY " . $int;

    foreach ($value as $k => $v)
    {
        if (count($v[0])>1) // check if $v has more than 1 item.
        {
            foreach ($v as $index => $content)
            {
                $k . " -> " . $index . ": " . $content;
            }
        }else{
            print_r($k . ": " . $v[0]); // we get the first item in the child array
        }
    }

    $int = $int + 1; // I couldn't be bothered making it simpler for me.

}

@matrixdevuk Gives me a NOTICE Undefined offset 0:

To me it looks like it has more than 2 levels once rendering with the brower as @jsuna mentioned.

@raminshahab: This update might solve the issue, though I cannot directly tell.

$int = 1;

foreach($json_info as $key => $value)
{

    echo "ARRAY " . $int;

    foreach ($value as $k => $v)
    {
        if (is_array($v))
        {
            if (count($v[0])>1) // check if $v has more than 1 item.
            {
                foreach ($v as $index => $content)
                {
                    $k . " -> " . $index . ": " . $content;
                }
            }else{
                print_r($k . ": " . $v[0], true); // we get the first item in the child array
            }
        }else{
            echo $k . ": " . $v;
        }
    }

    $int = $int + 1; // I couldn't be bothered making it simpler for me.

}

@matrixdevuk @jsuna @veedeoo Thanks guys! Big help. After fiddling with the nested data I was able to extract it all.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.