2

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 ) ) ) )
pgtips
  • 1,328
  • 6
  • 24
  • 43

1 Answers1

5

you can use foreach then just use variable variables:

foreach($array as $keyName => $value) {
    //in here you have the key in $keyName and the value in $value
    $$keyName = $value;
}

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:

// 1. flatten out the array to contain only single values (no arrays)

do {
    $containedArrayValue = false;
    foreach($array as $key => $value) {
        if(is_array($value)) {
            $array = array_merge($array,$value);
            $containedArrayValue = true;
        }
        unset($array[$key]);
    }

} while($containedArrayValue);

// 2. run the code above to get variables for each one
foreach($array as $keyName => $value) 
    $$keyName = $value;

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

$firstName = $array[results][person][0][firstName];
$lastName  = $array[results][person][0][lastName]; 
$email     = $array[results][person][0][email];

now what if there was more than one person? we can make arrays of these variables as such:

foreach($array[results][person] as $personNum => $personData) {
    $firstNames[] = $personData[firstName];
    $lastNames[]  = $personData[lastName];
    $emails[]     = $personData[email];
}

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.

hackartist
  • 5,172
  • 4
  • 33
  • 48
  • It only gives me one set of values - results and Array?? – pgtips Dec 09 '11 at 03:37
  • Thanks for your patience @hackartist - I am a newbie. My goal is to take out every firstName, lastName, address, etc for display in a table of records. The children within the arrays are throwing me. Should I put all the firstNames etc into an array and then create the table. If so, how do I take out all the firstName's and put them into an array? – pgtips Dec 09 '11 at 15:58
  • That's ok, everyone is a newbie at some point. The code I gave above will flatten everything into a single array and put all of those things into variables... but I don't think that is what you want at all so sorry I misunderstood. What are the columns that you want? This is only the record for one person and the table will have many people shown at the same time right? – hackartist Dec 09 '11 at 18:53
  • Yes - exactly, this is only one but most queries will have many records. I am looking to to display columns - firstName, lastName, Email. If I have those, I should be able to figure out the rest. Thanks so much! – pgtips Dec 09 '11 at 19:32
  • 1
    ok hope the edit above helps explain everything. If this still isn't what you were looking for give me an example of the exact output you want and I will show you how to get to it. Good luck. – hackartist Dec 09 '11 at 20:15
  • This is EXACTLY what I was looking for. Thank you so much for explaining it so well. It makes so much more sense now :) – pgtips Dec 09 '11 at 20:35