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 7678135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:34:33+00:00 2026-05-31T17:34:33+00:00

So I have this awesome MySQL query that’s returning me an awesome array >

  • 0

So I have this awesome MySQL query that’s returning me an awesome array >

$join_sql = "SELECT $detail_table.`lead_id`, $detail_table.`field_number`, $detail_table.`value` FROM $lead_table JOIN $detail_table ON $lead_table.`id` = $detail_table.`lead_id` WHERE $lead_table.`date_created` BETWEEN '2012-03-18 00:00:00' AND '2012-03-19 23:59:59'";
$join_query = $wpdb->get_results($join_sql, ARRAY_A);

This is returning a (in my opinion) complex associate array that looks like this – http://pastebin.com/kpXrK9Ra

(I don’t understand data storage methodology but this seems overly complicated to me to have each bit of data in it’s own dimensional array rather than just a row of data for each record. But anyway, this is what I have to deal with.)

So here’s a small sample of what I need to get to >

[23] => Array ( [lead_id] => 3 [field_number] => 6.3 [value] => Jane )

So the key that represents a “record” in my mind is the [lead_id]. I need to be able to create a var $first_name. My resultset may have 50 unique [lead_id} but for the first name, the [field_number] will always be ‘6.3’.

So how do I loop through the pastebin and create vars for each of the elements so that all the [lead_id] with matching numbers outputs something like-

$first_name, $last_name, $whatev, $foo, $bar, $etc

I’m not even sure if this makes sense… :\

EDIT: What I’m trying to do is access the ‘field_number’ which corresponds to what the field is, ie: a first name, second name, etc. and the corresponding ‘value’ and output all of those in a particular order that matches the ‘lead_id’. So all lead_id’s of [2] would be output into a line of the CSV, and then the next lead_id, etc.

Thanks in advance!

  • 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-31T17:34:35+00:00Added an answer on May 31, 2026 at 5:34 pm

    Given your input in $xs, the following:

    $ret = array_reduce($xs, function($acc,$x) {
        $acc[$x['lead_id']][$x['field_number']] = $x['value'];
        return $acc;
    }, array());
    

    produces the output of:

    array (
      2 => 
      array (
        1 => '37',
        2 => '051000549',
        3 => '1050000605321',
        4 => '1050000605321',
        15 => '250.00',
        '6.3' => 'John',
        '6.6' => 'Doe',
        7 => 'Microscope',
        '8.1' => '1222 McDowell Ave NE',
        '8.3' => 'Roanoke',
        '8.4' => 'Virginia',
        '8.5' => '24012',
        '8.6' => 'United States',
        9 => '(540)265-3216',
        10 => 'marty@microscope.com',
        11 => 'John Doe',
        16 => '987654321',
        '14.1' => 'Authorized',
      ),
      3 => 
      array (
        1 => '27',
        2 => '987654321',
        3 => '1050000123456',
        4 => '1050000123456',
        15 => '350.00',
        '6.3' => 'Jane',
        '6.6' => 'Doe',
        7 => 'Acme Inc',
        '8.1' => '6724 Laban Rd',
        '8.3' => 'Roanoke',
        '8.4' => 'Virginia',
        '8.5' => '24019',
        '8.6' => 'United States',
        9 => '(540)362-2773',
        10 => 'm@seoserpent.com',
        11 => 'Acme Inc.',
        16 => '987654321',
        '14.1' => 'Authorized',
      ),
    )
    

    Update: The input I used is:

    $xs = array ( 
     0 => array ( 'lead_id' => '2', 'field_number' => '1', 'value' => '37', ),
     1 => array ( 'lead_id' => '2', 'field_number' => '2', 'value' => '051000549', ),
     2 => array ( 'lead_id' => '2', 'field_number' => '3', 'value' => '1050000605321', ),
     3 => array ( 'lead_id' => '2', 'field_number' => '4', 'value' => '1050000605321', ),
     4 => array ( 'lead_id' => '2', 'field_number' => '15', 'value' => '250.00', ),
     5 => array ( 'lead_id' => '2', 'field_number' => '6.3', 'value' => 'John', ),
     6 => array ( 'lead_id' => '2', 'field_number' => '6.6', 'value' => 'Doe', ),
     7 => array ( 'lead_id' => '2', 'field_number' => '7', 'value' => 'Microscope', ),
     8 => array ( 'lead_id' => '2', 'field_number' => '8.1', 'value' => '1222 McDowell Ave NE', ),
     9 => array ( 'lead_id' => '2', 'field_number' => '8.3', 'value' => 'Roanoke', ),
     10 => array ( 'lead_id' => '2', 'field_number' => '8.4', 'value' => 'Virginia', ),
     11 => array ( 'lead_id' => '2', 'field_number' => '8.5', 'value' => '24012', ),
     12 => array ( 'lead_id' => '2', 'field_number' => '8.6', 'value' => 'United States', ),
     13 => array ( 'lead_id' => '2', 'field_number' => '9', 'value' => '(540)265-3216', ),
     14 => array ( 'lead_id' => '2', 'field_number' => '10', 'value' => 'marty@microscope.com', ),
     15 => array ( 'lead_id' => '2', 'field_number' => '11', 'value' => 'John Doe', ),
     16 => array ( 'lead_id' => '2', 'field_number' => '16', 'value' => '987654321', ),
     17 => array ( 'lead_id' => '2', 'field_number' => '14.1', 'value' => 'Authorized', ),
     18 => array ( 'lead_id' => '3', 'field_number' => '1', 'value' => '27', ),
     19 => array ( 'lead_id' => '3', 'field_number' => '2', 'value' => '987654321', ),
     20 => array ( 'lead_id' => '3', 'field_number' => '3', 'value' => '1050000123456', ),
     21 => array ( 'lead_id' => '3', 'field_number' => '4', 'value' => '1050000123456', ),
     22 => array ( 'lead_id' => '3', 'field_number' => '15', 'value' => '350.00', ),
     23 => array ( 'lead_id' => '3', 'field_number' => '6.3', 'value' => 'Jane', ),
     24 => array ( 'lead_id' => '3', 'field_number' => '6.6', 'value' => 'Doe', ),
     25 => array ( 'lead_id' => '3', 'field_number' => '7', 'value' => 'Acme Inc', ),
     26 => array ( 'lead_id' => '3', 'field_number' => '8.1', 'value' => '6724 Laban Rd', ),
     27 => array ( 'lead_id' => '3', 'field_number' => '8.3', 'value' => 'Roanoke', ),
     28 => array ( 'lead_id' => '3', 'field_number' => '8.4', 'value' => 'Virginia', ),
     29 => array ( 'lead_id' => '3', 'field_number' => '8.5', 'value' => '24019', ),
     30 => array ( 'lead_id' => '3', 'field_number' => '8.6', 'value' => 'United States', ),
     31 => array ( 'lead_id' => '3', 'field_number' => '9', 'value' => '(540)362-2773', ),
     32 => array ( 'lead_id' => '3', 'field_number' => '10', 'value' => 'm@seoserpent.com', ),
     33 => array ( 'lead_id' => '3', 'field_number' => '11', 'value' => 'Acme Inc.', ),
     34 => array ( 'lead_id' => '3', 'field_number' => '16', 'value' => '987654321', ),
     35 => array ( 'lead_id' => '3', 'field_number' => '14.1', 'value' => 'Authorized', ),
     );
    

    Also, you may find a foreach or while loop more readable than array_reduce:

    $ret = array();
    foreach ($xs as $x) {
        $ret[$x['lead_id']][$x['field_number']] = $x['value'];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web form that is using this awesome plugin and I'm trying
What are my options for printing in Silverlight 3? Assume I have this awesome
I have an awesome trouble with Gem. After executing this command: rm -f /usr/local/lib/ruby/gems/1.9.1/cache/*
I have this code in jQuery, that I want to reimplement with the prototype
I have this RewriteRule that works too well :-) RewriteRule ^([^/]*)/$ /script.html?id=$1 [L] The
Hey, first time poster on this awesome community. I have a regular expression in
I have this statement in T-SQL. SELECT Bay From TABLE where uid in (
So jQuery provides this awesome pseudo to query in DOM on ':visible', unfortunately, its
I have a query that attempts to find things within a certain geolocation, but
I have two MySQL tables: house and features . This is for a real

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.