Reputation: 1
I have a function that holds an array which is then json_encoded, it sends the json array to a database and pulls the results from there. What I need to do is have a recursive array. There are nested arrays inside of an array and I need to pull out some values. Here's my code:
function attDir($attId){
//$request = "{"Service":"webphone+","type":"attuid","criteria":"$attId","output":"partial","depth":"1"}";
$arrRe = array();
//Assoc array
$arrRe['Service'] = "webphone+";
$arrRe['type'] = "attuid";
$arrRe['criteria'] = "$attId";
$arrRe['output'] = "partial";
$arrRe['depth'] = "all"; //Shows everyone that reports under this ID. "1" will show direct reports.
$request = json_encode($arrRe);
//echo $request;
$fp = fsockopen( "loadbalancer.web.att.com", 5862 );
if ( ! $fp )
{
echo "Failed connection: \n";
}
else
{
fwrite($fp, "$request\n" );
fwrite($fp, "end\n"); // required so SJ knows this is the end of the request
echo "Sending $request <br />";
$result = array();
while ( ! feof( $fp)) //while NOT end of file
{
$line = rtrim(fgets( $fp, 1024000 )); //remove white space from right side of a string fgets(file, length) fgets gets line from file pointer
if ( $line == "end" ) //if line = 'end' stop the program
{
break;
}
$result[] = $line;
}
$decoded = json_decode( $result[1], true ); //json array decoded results
print "<pre>";
print_r( $decoded ); //print out results from json array
print "</pre>";
foreach ( $decoded as $attuid => $arrLvl1)
{
//print_r($attuid);
if ( $attuid != "sub" )
{
//echo "LV1: $attuid\n";
echo "Attuid: ". $arrLvl1['a1'] . "<br />";
echo "F_Name: ". $arrLvl1['givenName'] . "<br />";
echo "L_Name: ". $arrLvl1['sn'] . "<br />";
}
else
{
foreach ( $arrLvl1 as $key1 => $arrLvl2 )
{
//echo $attuid;
echo " Attuid: ". $arrLvl2['a1'] . "<br />";
echo " F_Name: ". $arrLvl2['givenName'] . "<br />";
echo " L_Name: ". $arrLvl2['sn'] . "<br />";
//echo " Sub: ". $arrLvl2['sub'] . "<br />";
}
}
}
}
This will print out the ID, First and last name for the keys inside the array but only two levels down. How would I make this recursive so that it looks for all the levels inside the nested arrays? This is what I get as an output:
Attuid: pr127r
F_Name: PATRICIA
L_Name: RILEY
Attuid: fb2747
F_Name: FRED
L_Name: BAUER
Attuid: hp8813
F_Name: HELEN
L_Name: PEPPER
Attuid: rg0137
F_Name: ROBERT
L_Name: GRIFFITH
Attuid: ss942h
F_Name: STACI
L_Name: SAITO
Upvotes: 0
Views: 78
Reputation: 86
For your data, for example, you can try this solution:
// example array
$decodedArray = array(
1 => array(
'a1' => 'pr127r',
'givenName' => 'PATRICIA',
'sn' => 'RILEY',
),
2 => array(
'a1' => 'hp8813',
'givenName' => 'HELEN',
'sn' => 'PEPPER',
),
'sub' => array(
1 => array(
'a1' => 'fb2747',
'givenName' => 'FRED',
'sn' => 'BAUER',
),
2 => array(
'a1' => '2fb2747',
'givenName' => '2FRED',
'sn' => '2BAUER',
),
'sub' => array(
1 => array(
'a1' => 'rg0137',
'givenName' => 'ROBERT',
'sn' => 'GRIFFITH',
'sub' => array(),
)
),
),
);
function yourFunction($decodedArray)
{
echo '<div style="padding-left:20px;">';
foreach ($decodedArray as $key => $value) {
if($key != 'sub') {
// var_dump($value);
echo "Attuid: ". $value['a1'] . "<br />";
echo "F_Name: ". $value['givenName'] . "<br />";
echo "L_Name: ". $value['sn'] . "<br /><br />";
} else {
yourFunction($value);
}
}
echo '<div>';
}
yourFunction($decodedArray)
Upvotes: 1