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

  • Home
  • SEARCH
  • 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 310071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:48:00+00:00 2026-05-12T07:48:00+00:00

I have a huge array coming back as search results and I want to

  • 0

I have a huge array coming back as search results and I want to do the following:

Walk through the array and for each record with the same "spubid" add the following keys/vals: "sfirst, smi, slast" to the parent array member in this case, $a[0]. So the result would be leave $a[0] in tact but add to it, the values from sfirst, smi and slast from the other members in the array (since they all have the same "spubid"). I think adding the key value (1, 2, 3) to the associate key (sfirst1=> "J.", smi1=>"F.", slast1=>"Kennedy") would be fine. I would then like to DROP (unset()) the rest of the members in the array with that "spubid". Here is a simplified example of the array I am getting back and in this example all records have the same "spubid":

Array ( 
  [0] => 
    Array ( 
      [spubid] => A00502 
      [sfirst] => J. 
      [smi] => A. 
      [slast] => Doe
  [1] => 
    Array ( 
      [spubid] => A00502 
      [sfirst] => J. 
      [smi] => F. 
      [slast] => Kennedy 
  [2] => 
    Array ( 
      [spubid] => A00502 
      [sfirst] => B. 
      [smi] => F. 
      [slast] => James 
  [3] => 
    Array ( 
      [spubid] => A00502
      [sfirst] => S. 
      [smi] => M. 
      [slast] => Williamson 
      )
    )

So in essence I want to KEEP $a[0] but add new keys=>values to it (sfirst$key, smi$key, slast$key) and append the values from "sfirst, smi, slast" from ALL the members with that same "spubid" then unset $a[1]-[3].

Just to clarify my IDEAL end result would be:

Array ( 
  [0] => 
    Array ( 
      [spubid] => A00502 
      [sfirst] => J. 
      [smi] => A. 
      [slast] => Doe
      [sfirst1] => J.
      [smi1] => F. 
      [slast1] => Kennedy
      [sfirst2] => B. 
      [smi2] => F. 
      [slast2] => James 
      [sfirst3] => S. 
      [smi3] => M. 
      [slast3] => Williamson
    )
  )

In most cases I will have a much bigger array to start with that includes a multitude of "spubid"’s but 99% of publications have more than one author so this routine would be very useful in cleaning up the results and making the parsing process for display much easier.

***UPDATE

I think by over simplifying my example I may have made things unclear. I like both Chacha102’s and zombat’s responses but my "parent array" contains A LOT more than just a pubid, that just happens to be the primary key. I need to retain a lot of other data from that record, a small portion of that is the following:

[spubid] => A00680 
[bactive] => t 
[bbatch_import] => t 
[bincomplete] => t 
[scitation_vis] => I,X 
[dentered] => 2009-08-03 12:34:14.82103 
[sentered_by] => pubs_batchadd.php 
[drev] => 2009-08-03 12:34:14.82103 
[srev_by] => pubs_batchadd.php 
[bpeer_reviewed] => t 
[sarticle] => A case study of bora-driven flow and density changes on the Adriatic shelf (January 1987)
.
.
.
.
.

There are roughly 40 columns that come back with each search query. Rather than hard coding them as these examples do with the pubid, how can I include them while still making the changes as you both suggested. Creating a multi-dimensional array (as both of you suggested) with the authors being part of the multi-dimension is perfectly fine, thank you both for the suggestion.

**** UPDATE:

Here is what I settled on for a solution, very simple and gets the job done nicely. I do end up creating a multi-dimensional array so the authors are broken out there.

Over simplified solution:

$apubs_final = array();
$spubid = NULL;
$ipub = 0;

foreach($apubs as $arec)
{
  if($spubid != $arec['spubid'])
  {
    $ipub++;
    $apubs_final[$ipub] = $arec;
    // insert UNSET statements here for author data
    $iauthor = 0;
    $spubid = $arec['spubid'];
  }
  $iauthor++;
  $apubs_final[$ipub]['authors'][$iauthor]['sauthor_first'] = $arec['sfirst'];
}
  • 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-12T07:48:00+00:00Added an answer on May 12, 2026 at 7:48 am
    // First, probably the more parsable way.
    foreach($array as $key => $values)
    {
        $end[$spuid] = $values;
        $spuid = $values['spuid']
        $end[$spuid]['authors'][] = array('sfirst' => $values['sfirst'],
                              'smi' => $values['smi'],
                               'slast' => $values['slast']);
    
    }
    

    Which will get an array like this

    Array(
        [A00502] =>
             Array(
               [supid] => A00502
                   .... other values .....
               [authors] =>
                     Array(
                    [0]=>
                          Array(
                        ['sfirst'] => '',
                        ['smi'] => '',
                        ['slast'] => '')
                    )
            )
    )
    

    I find this way to be much more parse-able if you plan on showing it on a page, because it uses arrays so you can foreach the authors, which is how I’ve seen many people do it for attributes like that.

    If you really do want your ideal format, use this afterwards

    $count = 0;
    foreach ($end as $supid => $values)
    {
        $other_end[$count] = $values;
        $other_end[$count]['spuid'] = $spuid;
        foreach($values['authors'] as $key => $author)
        {
            if($key == 0)
            {
                $suffix = '';
            }
            else
            {
                $suffix = $key;
            }
            $other_end[$count]['sfirst'.$suffix] = $author['sfirst'];
            $other_end[$count]['smi'.$suffix] = $author['smi'];
            $other_end[$count]['slast'.$suffix] = $author['slast'];
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a huge text file which I want to explode into an array.
Given I have a HUGE array, and a value from it. I want to
Hi I have a huge array of words and I want to check for
I have a huge array, which I loop with a foreach. foreach ($items as
So basically I have a huge array of arrays (only a 2-d array)... My
I have a huge global array of structures. Some regions of the array are
I have a huge array list which contains 1000 entries out of which one
I have an huge array which contains a struct Tile. The program im writing
I have a huge array that has to be read by different threads in
I have an config.php file where I simply make an huge array that contains

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.