I am trying to figure out how to iterate over an array which is a decoded json string. I would like to create variables for firstname, lastname, etc and then output them to separate rows in a table. Here is an example array. Most results will have multiple records. Any suggestions would be appreciated:
Array ( [results] => Array ( [@count] => 1 [@pageNumber] => 1 [@totalRecords] => 1 [@additionalPages] => 0 [person] => Array ( [0] => Array ( [@array] => true [@id] => 38903211 [@uri] => https://api-name-removed.com/People/38903211 [@imageURI] => [@oldID] => 38614423 [@iCode] => fwVVyUOWEg3DOjIfg== [@householdID] => 23902641 [@oldHouseholdID] => 23740508 [title] => [salutation] => [prefix] => [firstName] => Mandy [lastName] => Ford [suffix] => [middleName] => [goesByName] => [formerName] => [gender] => Female [dateOfBirth] => 1965-02-11T00:00:00 [maritalStatus] => [householdMemberType] => Array ( [@id] => 1 [@uri] => https://api-name-removed.com/People/HouseholdMemberTypes/1 [name] => Head ) [isAuthorized] => true [status] => Array ( [@id] => 26523 [@uri] => https://api-name-removed.com/People/Statuses/26523 [name] => Prospect [comment] => [date] => 2010-09-07T00:00:00 [subStatus] => Array ( [@id] => [@uri] => [name] => ) ) [occupation] => Array ( [@id] => [@uri] => [name] => [description] => ) [employer] => [school] => Array ( [@id] => [@uri] => [name] => ) [denomination] => Array ( [@id] => [@uri] => [name] => ) [formerChurch] => [barCode] => 20001881 [memberEnvelopeCode] => [defaultTagComment] => [weblink] => Array ( [userID] => [passwordHint] => [passwordAnswer] => ) [solicit] => [thank] => true [firstRecord] => 2008-11-24T14:52:23 [attributes] => [addresses] => [communications] => Array ( [communication] => Array ( [0] => Array ( [@array] => true [@id] => 40826651 [@uri] => https://api-name-removed.com/Communications/40826651 [household] => Array ( [@id] => 23902641 [@uri] => https://api-name-removed.com/Households/23902641 ) [person] => Array ( [@id] => [@uri] => ) [communicationType] => Array ( [@id] => 4 [@uri] => https://api-name-removed.com/Communications/CommunicationTypes/4 [name] => Email ) [communicationGeneralType] => Email [communicationValue] => mford@email.com [searchCommunicationValue] => mford@adventurechristian.org [listed] => true [communicationComment] => [createdDate] => 2011-11-14T17:10:13 [lastUpdatedDate] => 2008-11-24T14:52:23 ) ) ) [lastMatchDate] => [createdDate] => 2011-11-14T17:10:03 [lastUpdatedDate] => 2011-05-09T11:43:59 ) ) ) )
you can use foreach then just use variable variables:
EDIT: not 100% sure what you want with the ‘array’ and ‘results’ but I think the problem there is that the arrays are children of the other arrays you can modify the code to check if the value is an array like so:
is this what you want to happen? otherwise please give the outcome you want and I will fix it to be like that…
SECOND EDIT: I will leave the code above because even though it wasn’t what you were looking for it may help someone else later. The data structure you have is an array where each value is either a string, number, or another array. Each of those arrays is the same way so you can have many layers deep (7 in this example). The layer you care about would be in $array[results][person] if this whole variable was stored in $array; This is an array of ‘people’ arrays so the first person will be at $array[results][person][0] the second will be at $array[results][person][1] etc. inside of each person you can get the data you want as
now what if there was more than one person? we can make arrays of these variables as such:
and now you have the data you want in these three arrays. The empty brackets is just shorthand for the next empty element of the array so in the loop I am just building up these arrays member by member. If you wanted to get something more complex like their password hint you could get at it by doing $personData[weblink][passwordHint] inside the loop. For more info check out the foreach syntax and multidimensional arrays.