I am trying to create an associative array with the keys being email addresses and the values being passwords. It is reading from an XML database to get the information. Here is my code:
$data = simplexml_load_file("Treasury.xml");
//Add in all passwords
for ($i = 0; $i < count($data->Member); $i++) {
$key = $data->Member[$i]->Email + '';
$USERS[$key] = $data->Member[$i]->Pin;
}
The problem comes in the for loop. It gets a correct count of the members (I had that print out) but the key is always being labeled as the number 0, resulting in only the last pin being stored in an array on length 1. Is there something syntactically that I am doing wrong?
Thanks in advance.
EDIT: I did a var_dump of the first user in the XML document. Here it is (Sorry for how long it is):
object(SimpleXMLElement)#4 (5) { ["Name"]=> string(19) "Mackenzie Daugherty" ["PC"]=> object(SimpleXMLElement)#2 (0) { } ["Email"]=> string(16) "dau53688#obu.edu" ["Pin"]=> string(4) "0000" ["Payments"]=> object(SimpleXMLElement)#3 (1) { ["Payment"]=> array(2) { [0]=> object(SimpleXMLElement)#5 (7) { ["Type"]=> string(4) "Dues" ["Description"]=> string(18) "Dues for Fall 2013" ["DateIssued"]=> string(7) "8/26/13" ["DateEnd"]=> string(6) "9/9/13" ["Owed"]=> string(2) "55" ["Paid"]=> string(2) "55" ["Plan"]=> object(SimpleXMLElement)#7 (5) { ["InPlan"]=> string(1) "0" ["PlanDescription"]=> object(SimpleXMLElement)#8 (0) { } ["Intervals"]=> string(1) "0" ["Completed"]=> string(1) "0" ["PerInterval"]=> string(1) "0" } } [1]=> object(SimpleXMLElement)#6 (7) { ["Type"]=> string(19) "Tiger Tunes Tickets" ["Description"]=> string(18) "Two Saturday Night" ["DateIssued"]=> string(7) "8/26/13" ["DateEnd"]=> string(7) "8/26/13" ["Owed"]=> string(2) "30" ["Paid"]=> string(2) "30" ["Plan"]=> object(SimpleXMLElement)#7 (5) { ["InPlan"]=> string(1) "0" ["PlanDescription"]=> object(SimpleXMLElement)#8 (0) { } ["Intervals"]=> string(1) "0" ["Completed"]=> string(1) "0" ["PerInterval"]=> string(1) "0" } } } } }
As clearly stated in the documentation that I'm sure you've been studying carefully, the PHP concatenation operator is ., not +.
Your code takes two operands, and attempts to perform arithmetic addition on them. Since they are not [meaningful] numbers, you end up with 0.
(I couldn't give a more detailed assessment without knowing the precise values of your operands, which you did not provide.)
Your code should read:
$key = $data->Member[$i]->Email . '';
// ^
// (is the concatenation necessary at all?
// isn't Email already a string?)
Make the same correction elsewhere.
Related
Okay so I have an array, laid out as follows (only the first item is shown):
array(600) {
[0]=>
array(12) {
["id"]=>
string(4) "1163"
["aliasID"]=>
string(1) "1"
["date"]=>
string(10) "2017-06-09"
["type"]=>
string(12) "DD"
["description"]=>
string(18) "GYM MEMBERSHIP"
["plusminus"]=>
string(1) "0"
["amount"]=>
string(2) "15"
["balance"]=>
string(6) "50.00"
["ts"]=>
string(19) "2019-01-27 22:32:29"
["alias"]=>
string(3) "Gym"
["categoryID"]=>
string(1) "1"
["category"]=>
string(10) "Recreation"
}
In this instance there are 600 transactions. I want to run through the list, find the 10 most popular (as in, the most frequent occuring) and display them. How can I achieve this? I'm not great at sorting - but I wrote the following code to add them up:
foreach($transactions as $t) {
if(isset($popular_dataset[$t['description']])) {
$popular_dataset[$t['description']]++;
} else {
$popular_dataset[$t['description']] = 1;
}
}
Which gives me an array that I can view the highest ones, but I'm unsure as to how to proceed from here. Any advice would be great - am I on the right path or is there a simpler way?
I would extract the description into an array and count the values, then sort descending and slice the first ten:
$popular_dataset = array_count_values(array_column($transactions, 'description'));
arsort($popular_dataset);
$top_ten = array_slice($popular_dataset, 0, 10);
I have two arrays that I want to merge. I created the first one through a for() loop so that it includes the last seven days (only three here to keep it short):
array(7) {
[0]=>
array(2) {
["created_at"]=>
string(10) "2017-08-15"
["errorCount"]=>
string(1) "0"
}
[1]=>
array(2) {
["created_at"]=>
string(10) "2017-08-16"
["errorCount"]=>
string(1) "0"
}
[2]=>
array(2) {
["created_at"]=>
string(10) "2017-08-17"
["errorCount"]=>
string(1) "0"
}
}
The other array includes data from a DB:
array(2) {
[0]=>
array(2) {
["created_at"]=>
string(10) "2017-08-15"
["errorCount"]=>
string(1) "4"
}
[1]=>
array(2) {
["created_at"]=>
string(10) "2017-08-16"
["errorCount"]=>
string(1) "12"
}
}
I want to merge these two together so that errorCount in the first array is overwritten whenever created_at is identical. I tried it with array_merge() directly but it only adds the rows of the second array to the end of the first one.
Any suggestions on how to solve this? Or is there a different way to approach it?
Filter result by following statement:
$finArray = array_values(array_combine(array_map(function ($value) {
return $value['created_at'];
}, $mergedArray), $mergedArray));
Working in CI, wanting to loop through a result_array and add values from an amount key value. The array is multidimensional
array(2)
{
[0]=> array(9)
{
["id"]=> string(1) "1"
["resident_id"]=> string(1) "1"
["charge_amt"]=> string(6) "250.00"
["charge_key"]=> string(3) "HOM"
["charge_desc"]=> string(25) "Homeowner Association Fee"
["charge_date"]=> string(19) "2014-03-04 03:08:08"
["active"]=> string(1) "1"
["created_at"]=> string(19) "2014-03-03 14:17:00"
["updated_at"]=> NULL
}
[1]=> array(9)
{
["id"]=> string(1) "2"
["resident_id"]=> string(1) "1"
["charge_amt"]=> string(5) "25.00"
["charge_key"]=> string(3) "LAT"
["charge_desc"]=> string(8) "Late Fee"
["charge_date"]=> string(19) "2014-03-04 04:11:10"
["active"]=> string(1) "1"
["created_at"]=> string(19) "2014-03-03 04:10:09"
["updated_at"]=> NULL
}
}
How do I get it to loop through each array and add up each ["charge_amt"] and then print the final calculation once?
Not sure if I am missing anything but it's just a foreach loop where $result represents the second layer of the array. So it's either
foreach($result as $item) or foreach($array['result'] as $item)
$total = 0;
foreach ($result as $item){
$total = $total + $item['charge_amt'];
}
echo $total;
If you don't need the loop for anything else, this might be faster/shorter (PHP 5 >= 5.5.0):
$charge_amts = array_column($result_array, 'charge_amt');
$total_charge_amt = array_sum($charge_amts);
print $total_charge_amt;
Anyway, since this looks like a result from a database query, why don't you let the database sum up the total amount directly?
This one's been bugging me for the past day now. Here's the code:
public function getUserCredits()
{
$dbl = new databaseManager();
$dbl->connect_simp();
$ret = $dbl->queryDB("SELECT * FROM users WHERE `USER_ID` = ".$this->userId);
$this->userCredits = $ret['USER_CREDITS'];
return $this->userCredits;
}
Now when I try to run this code, I get an undefined offset error. Nothing too strange about it when I first saw it, but now it's happening more and more and I can't figure out why.
I can use var_dump(); and var_export(); and it displays the contents of the returned array absolutely fine.
EDIT:
array(1) {
[0]=>
array(50) {
["USER_ID"]=>
string(10) "0000000001"
[0]=>
string(10) "0000000001"
["USER_USERNAME"]=>
string(8) "SampleUsername"
[1]=>
string(8) "SampleUsername"
["USER_PASSWORD"]=>
string(32) "5f4dcc3b5aa765d61d8327deb882cf99"
[2]=>
string(32) "5f4dcc3b5aa765d61d8327deb882cf99"
["USER_EMAIL"]=>
string(0) ""
[3]=>
string(0) ""
["USER_LEGION"]=>
string(1) "1"
[4]=>
string(1) "1"
["USER_ENERGY"]=>
string(4) "2812"
[5]=>
string(4) "2812"
["USER_MAX_ENERGY"]=>
string(4) "2812"
[6]=>
string(4) "2812"
["USER_SHIELD"]=>
string(2) "20"
[7]=>
string(2) "20"
["USER_MAX_SHIELD"]=>
string(2) "20"
[8]=>
string(2) "20"
["USER_HULL"]=>
string(2) "60"
[9]=>
string(2) "60"
["USER_MAX_HULL"]=>
string(2) "60"
[10]=>
string(2) "60"
["USER_CREDITS"]=>
string(19) "9223372036854775807"
try
$ret[0]['USER_CREDITS']
instead of
$ret['USER_CREDITS']
You gotta fetch a row first (I assume you extend mysqli).
$result = $dbl->queryDB("SELECT * FROM users WHERE `USER_ID` = ".$this->userId);
$row = $dbl->fetch_assoc($result) ;
$this->userCredits = $row['USER_CREDITS'];
At first you should watch the result record. As you are getting the details in an array of your database record fetched and it is in array format with numeric keys. Use what #AbuOmar said.
AS your var_dump($ret), Try this to assign user credit otherwise if it is not set, assign 0 value.
if(isset($ret[0]['USER_CREDITS'])){
$this->userCredits = $ret[0]['USER_CREDITS'];
}
else{
$this->userCredits = 0;
}
I have this array $node->taxonomy which puts out these data, if I var_dump() it:
array(1) {
[4]=> object(stdClass)#21 (5) {
["tid"]=> string(1) "4"
["vid"]=> string(1) "4"
["name"]=> string(9) "Marketing"
["description"]=> string(0) ""
["weight"]=> string(1) "1"
}
}
how can I extract the element "name" from it?
This way:
$node->taxonomy[4]->name
$node->taxonomy[4]->name
<?php
echo $node->taxonomy[4]->name;
or if you don't know the number (or the number changes) but its the first element in the array you could use:
<?php
$x = current($node->taxonomy);
echo $x->name;
As simple as:
$node->taxonomy[4]->name