0

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

4
Contributors
11
Replies
39
Views
3 Years
Discussion Span
Last Post by raminshahab
Featured Replies
  • $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. Read More

  • 2

    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 />"; … Read More

  • 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. Read More

  • 1

    @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 … Read More

  • 1

    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); ?? Read More

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

 print_r($value);

}

Gives me back same assoc array??

1
$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.

2

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!

1

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.

1

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

1

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); ??

Edited by jsuna

1

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.

}
0

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

0

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

}
0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.