Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6908377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:33:21+00:00 2026-05-27T08:33:21+00:00

I am trying to figure out how to iterate over an array which is

  • 0

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 ) ) ) )
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:33:22+00:00Added an answer on May 27, 2026 at 8:33 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I’m trying to figure out how to iterate over an array of subroutine refs.
I've been trying to figure out how to iterate over the list of columns
I'm trying to figure out how to iterate through a JSON object in an
Trying to figure out an equation to get the current group a page would
Trying to figure out which to use.
I am trying to figure out how to iterate through a file line by
I am trying to figure out what the best method would be for me
I'm trying to figure out how to iterate through a document and pull all
I'm trying to figure out how I can iterate in reverse, and forward through
Trying to figure out the best way to set up collection lists for users

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.