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!
Given your input in
$xs, the following:produces the output of:
Update: The input I used is:
Also, you may find a
foreachorwhileloop more readable thanarray_reduce: