I am having trouble with trying to merge mulitple arrays into one array I also cant seem to find a decent solution to this problem since using merge_array doesnt work. I made an array in my foreach loop but it keeps coming out as multiple arrays for one string. How can I convert this.
Array
(
[images] => /wp-content/uploads/2015/03/223-HfpWJ4h.jpg
[userid] => 15
)
Array
(
[images] => /wp-content/uploads/2015/03/074-XlqzqVn.jpg
[userid] => 1
)
Array
(
[images] => /wp-content/uploads/2014/09/cj-spiller.jpg
[userid] => 1
)
Array
(
[images] => /wp-content/uploads/2015/02/183-cZRn9rz.jpg
[userid] => 1
)
Array
(
[images] => /wp-content/uploads/2015/02/001-gSxGEWD5.jpg
[userid] => 1
)
To this?
Array
(
[0] => array(
[images] => /wp-content/uploads/2015/03/223-HfpWJ4h.jpg
[userid] => 15
)
[1] => array(
[images] => /wp-content/uploads/2015/03/074-XlqzqVn.jpg
[userid] => 1
)
[2]=> array(
[images] => /wp-content/uploads/2014/09/cj-spiller.jpg
[userid] => 1
)
)
This is my code
global $wpdb, $bp;
$activity_table = bp_core_get_table_prefix() . 'bp_activity';
$activity_meta_table = bp_core_get_table_prefix() . 'bp_activity_meta';
// that have pictures associated with them
$sql = "SELECT a.*, am.meta_value, am2.meta_value as privacy FROM $activity_table a
INNER JOIN $activity_meta_table am ON a.id = am.activity_id
LEFT JOIN (SELECT activity_id, meta_key, meta_value FROM $activity_meta_table
WHERE meta_key = 'activityprivacy') am2 ON a.id = am2.activity_id
AND (am.meta_key = 'buddyboss_pics_aid' OR am.meta_key = 'bboss_pics_aid')
ORDER BY a.date_recorded DESC";
$pics = $wpdb->get_results($sql,ARRAY_A);
$i = 0;
foreach($pics as $pic){
$attachment_id = isset($pic['meta_value']) ? (int)$pic['meta_value'] : 0;
$tn = wp_get_attachment_image_src( $attachment_id, 'buddyboss_pic_tn' );
$userid = $pic['user_id'];
if($tn[0] != ''){
$profile_image = array('images' => $tn[0], 'userid' => $pic['user_id']);
print_r($profile_image);
}
}
It seems like:
$result = array();
//...
foreach($pics as $pic){
//...
$userid = $pic['user_id'];
if($tn[0] != ''){
$profile_image = array('images' => $tn[0], 'userid' => $pic['user_id']);
$result[] = $profile_image;
}
}
Isn't it? Or I've mistaken your purpose.
Related
I'm using PHP in doing our exercise which is to get the necessary details of a user and the quiz he/she took.
This is my code so far.
<?php
require("dbOption/Db.class.php");
$db = new Db();
$data = array();
$quiz = array();
$easy = "";
$normal = "";
$hard = "";
// SELECT id, course FROM quizz WHERE course = 2
$res_quizz = $db->query("SELECT id, course FROM quizz WHERE course = :course", array("course"=>2));
foreach ($res_quizz as $key => $value) {
$quizzid = $value['id'];
// SELECT easy, normal, hard, userid FROM quizzscore WHERE quizid = $quizid
$res_quizzscore = $db->query("SELECT easy, normal, hard, userid, quizzid FROM quizzscore WHERE quizzid = :quizzid", array("quizzid"=>$quizzid));
foreach ($res_quizzscore as $key => $value2) {
$easy = $value2['easy'];
$normal = $value2['normal'];
$hard = $value2['hard'];
$quizzid = $value2['quizzid'];
$userid = $value2['userid'];
$q = array(
'Qz'.$quizzid => array(
'easy' =>$easy,
'normal' =>$normal,
'hard' =>$hard,
)
);
$quiz['quizzes'] = $q;
// SELECT name FROM USER WHERE id = $userid
$res_user = $db->query("SELECT name FROM user WHERE id = :id", array("id"=>$userid));
foreach ($res_user as $key => $value3) {
$name = $value3['name'];
}
}
$data[$userid] = array();
$data[$userid]['name'] = $name;
$data[$userid]['quizzes'] = $quiz;
echo "<pre>";
print_r($data);
echo "<pre/>";
}
The array is poorly manipulated and that's why I'm getting this result.
Array
(
[1] => Array
(
[name] => John Smith
[quizzes] => Array
(
[quizzes] => Array
(
[Qz1] => Array
(
[easy] => 5
[normal] => 6
[hard] => 7
)
)
)
)
)
Array
(
[1] => Array
(
[name] => John Smith
[quizzes] => Array
(
[quizzes] => Array
(
[Qz2] => Array
(
[easy] => 3
[normal] => 4
[hard] => 5
)
)
)
)
)
What I want to have is this kind of result.
Array
(
[1] => Array
(
[name] => John Smith
[quizzes] => Array
(
[quizzes] => Array
(
[Qz1] => Array
(
[easy] => 5
[normal] => 6
[hard] => 7
)
[Qz2] => Array
(
[easy] => 3
[normal] => 4
[hard] => 5
)
)
)
)
)
Any ideas would be most appreciated.
I Have Two Array's One That is a 'App' Array and one is a 'Table' Array. Each Row in 'Table' has an 'AppID' As Shown Below. How can I combine the array to where the App_ID in the Tables Array is under the Same App_ID on the App Array? What I Currently Have is First and Want I Want is after that. I am using PHP. Thank Your for all of the Help!
App Array:
Array
(
[1] => Array
(
[App_ID] => 1
[App_Name] => Project Manager
[App_Created] => 2014-12-17 16:31:57
)
[2] => Array
(
[App_ID] => 2
[App_Name] => Estimating
[App_Created] => 2014-12-17 23:49:40
)
)
Tables Array:
Array
(
[1] => Array
(
[Table_ID] => 1
[App_ID] => 1
[Table_Name] => Customers
)
[2] => Array
(
[Table_ID] => 2
[App_ID] => 1
[Table_Name] => Jobs
)
)
I Want to Go to:
Array
(
[1] => Array
(
[App_ID] => 1
[App_Name] => Kennedy_Fabricating_Project_Manager
[App_Created] => 2014-12-17 16:31:57
[Tables] = > Array
(
[Table_ID] => 1
[App_ID] => 1
[Table_Name] => Customers
),
Array
(
[Table_ID] => 2
[App_ID] => 1
[Table_Name] => Jobs
)
)
[2] => Array
(
[App_ID] => 2
[App_Name] => Estimating
[App_Created] => 2014-12-17 23:49:40
)
)
My Current PHP Code:
//Get App List
$apps_sql = $conn->query("SELECT * FROM `Apps`") or die("Conn Query Apps Failed");
$apps = array();
while($apps = $apps_sql->fetch_array(MYSQLI_ASSOC)) {
$app_id = $apps['App_ID'];
$table_sql = $conn->query("SELECT * FROM `Tables` WHERE `App_ID` = $app_id") or die("Conn Query Tables in Apps Failed");
// My Guess is Something Here?
$apps_a[$app_id] = $apps;
}
// Table List
$table_sql = $conn->query("SELECT * FROM `Tables`") or die("Conn Query Tables Failed");
$tables = array();
while($tables = $table_sql->fetch_array(MYSQLI_ASSOC)) {
$table_id = $tables['Table_ID'];
$tables_a[$table_id] = $tables;
}
It looks cleaner, and faster as you make only 2 queries instead of zillions
$apps_sql = $conn->query("SELECT * FROM `Apps`");
$apps_ids = array();
while ($app = $apps_sql->fetch_array(MYSQLI_ASSOC))
{
$apps_ids[] = $app['App_ID'];
$apps[$app['App_ID']] = $app;
}
$apps_ids = implode(',', $apps_ids);
$table_sql = $conn->query("SELECT * FROM `Tables` WHERE IN ({$apps_ids})");
while($table = $table_sql->fetch_array(MYSQLI_ASSOC))
{
$apps[$table['App_ID']]['tables'][] = $table;
}
just change:
$table_id = $tables['Table_ID'];
$tables_a[$table_id] = $tables;
into:
$apps_a[$tables['App_ID']]['Tables'][$tables['Table_ID']] = $tables;
i have been troubling to format array correctly
i have this code:
require('simple_html_dom.php');
$table = array();
$html = file_get_html('apc.html');
foreach($html->find('tr') as $row) {
$rack = ltrim($row->find('td',0)->plaintext);
$location = ltrim($row->find('td',1)->plaintext);
$usage = ltrim($row->find('td',2)->plaintext);
$auk = ltrim($row->find('td',3)->plaintext);
$cost = ltrim($row->find('td',4)->plaintext);
$rack = rtrim($rack);
$location = rtrim($location);
$usage = rtrim($usage);
$auk = rtrim($auk);
$cost = rtrim($cost);
$table[$rack][$usage][$auk][$cost] = true;
}
echo '<pre>';
print_r($table);
echo '</pre>';
using simple_html_dom above i can convert html table to an array as follow:
[Rack01] => Array
(
[741,60] => Array
(
[1.409,04] => Array
(
[267,72] => 1
)
)
[110,88] => Array
(
[210,67] => Array
(
[40,03] => 1
)
)
)
[Rack 09] => Array
(
[843,84] => Array
(
[1.603,30] => Array
(
[304,63] => 1
)
)
)
I would like to have result as below:
array(
[0] => array (
[usage] => 'Rack 01',
[usage] => '741,60',
[auk] => '1.409.04',
[cost] => '267,72')
[1] => array (
[usage] => 'Rack 02',
[usage] => 'value???',
[auk] => 'value???',
[cost] => 'value???')
any help would be apreaciate
Something like this. Also note that trim will do both left and right:
foreach($html->find('tr') as $row) {
$rack = trim($row->find('td',0)->plaintext);
$location = trim($row->find('td',1)->plaintext);
$usage = trim($row->find('td',2)->plaintext);
$auk = trim($row->find('td',3)->plaintext);
$cost = trim($row->find('td',4)->plaintext);
$table[] = array('rack' => $rack,
'usage' => $usage,
'auk' => $auk,
'cost' => $cost);
}
So far I have accomplished this array
[1] => Array
(
[name] => Test
[stations] => Array
(
[0] => Array
(
[name] => Name
[price] => 50.00
[description] => Description
)
[1] => Array
(
[name] => Name Test
[price] => 135.00
[description] => Test
)
)
)
[2]=> Array
(
[name] => Test Name
[stations] => Array
(
[0] => Array
(
[name] => Name
[price] => 50.00
[description] => Description
)
)
I do this by using two SQL queries in a nested foreach loop. ID is a POST value :system_
$systems = "SELECT * FROM `systems` AS sys WHERE `sys`.`p_id`=:id";
$systemNumber = 0;
$stationNumber = 0;
foreach ($systems as $system) {
$return[$systemNumber] = array(
'name' => $system['system_name']
);
$stations = "SELECT * FROM `stations` WHERE `system_id`=$system[system_id] AND `p_id` = :id";
foreach($stations as $station){
$stationsArray[$stationNumber] = array(
'name'=>$station['station_name'],
'price'=>$station['price'],
'description'=>$station['description'],
);
$return[$systemNumber]['stations'] = $stationsArray;
$stationNumber++;
}
$systemNumber++;
}
The problem I have with this is that it is building stations on top of previous array, meaning array becomes something like this
...
[stations]=> Array(
[0]=>...
[1]=>...
[2]=>...
[3]=>...
[4]=>...
)
How can I make that stations will be attached to the corresponding system?
The problem lies in the loop.
You are setting variables to 0 outside of the loop
$systemNumber = 0;
$stationNumber = 0;
however, within the loop, you need to restart both the $stationsArray to nothing and the $stationNumber to 0 aswell, otherwise, when you do
foreach($stations as $station){
you will keep just increasing the value of the $stationNumber on each loop.
$systems = "SELECT * FROM `systems` AS sys WHERE `sys`.`p_id`=:id";
$systemNumber = 0;
$stationNumber = 0;
foreach ($systems as $system) {
$return[$systemNumber] = array(
'name' => $system['system_name']
);
$stations = "SELECT * FROM `stations` WHERE `system_id`=$system[system_id] AND `p_id` = :id";
$stationNumber = 0;
$stationsArray = array();
foreach($stations as $station){
$stationsArray[$stationNumber] = array(
'name'=>$station['station_name'],
'price'=>$station['price'],
'description'=>$station['description'],
);
$return[$systemNumber]['stations'] = $stationsArray;
$stationNumber++;
}
$systemNumber++;
}
I'm using Wordpress for this, but it is not a Wordpress-centric issue, so I am asking here.
You can see a snippet of my code here: http://pastebin.com/Cbc8wKvB
<?php
function getFormIds()
{
global $wpdb;
$sql = "
SELECT a.id
FROM wp_rg_lead a
WHERE a.form_id = 10 AND a.payment_status = 'Approved'
";
$query = $wpdb->get_results($sql);
if($query)
return $query;
return false;
}
function getFormInfo($form_id)
{
global $wpdb;
$sql = "
SELECT *
FROM wp_rg_lead_detail
WHERE lead_id = $form_id
";
$query = $wpdb->get_results($sql);
if($query)
return $query;
return false;
}
$credit_card_form_ids = getFormIds();
if ( $credit_card_form_ids ) {
$entries = array();
foreach( $credit_card_form_ids as $entry) {
$single_entry = getFormInfo($entry->id);
if ( $single_entry ) {
$entry_array = array();
$full_array = array();
foreach( $single_entry as $entry ) {
$curr_array = array( $entry->field_number => strip_tags($entry->value));
$entry_array[$entry->field_number] = strip_tags($entry->value);
array_push($full_array, $entry_array);
}
}
}
#var_dump('full_array', $full_array);
}
Basically I'm running a DB query to grab all entry ID's that match a certain criterion. This returns an array of objects of single values. I then pass this array through a foreach() and try to extract/combine data to make it easier to work with.
Here is a print_r() of a $single_entry from the code above: http://pastebin.com/RZmfD2EU
single entry:
Array
(
[0] => stdClass Object
(
[id] => 1983
[lead_id] => 86
[form_id] => 10
[field_number] => 34
[value] => 695
)
[1] => stdClass Object
(
[id] => 1982
[lead_id] => 86
[form_id] => 10
[field_number] => 39
[value] => 0
)
[2] => stdClass Object
(
[id] => 1981
[lead_id] => 86
[form_id] => 10
[field_number] => 40.1
[value] => Yes
)
... etc etc.
With the code I provided, the final array seems to collapse somehow -- meaning I lose entries somewhere along the way.
Ideally I'd love the data to associate id's to their key => value pairs. Namely:
[id] => [lead_id]
[field_number#] => [field_number_value]
For a actual representation of the first few lines of data provided:
Array (
[0] =>
(
[id] => 86
[34] => 695
[39] => 0
[40.1] => Yes
...etc
)
)
Is there a better way to loop through and associate data than what I'm doing? I'm relatively new/bad at PHP and MySQL and would love some guidance.
I'm not sure if I'm understanding it right but if you want to assign values to specified keys…
if ( $single_entry ) {
$entry_array = array();
$full_array = array();
foreach( $single_entry as $entry ) {
$curr_array = array( $entry->field_number => strip_tags($entry->value));
$entry_array[$entry->field_number] = strip_tags($entry->value);
$lead_id = $entry['lead_id'];
$full_array[$lead_id] = $entry_array;
}
}