I Have a Json output like
{ ["id"]=> int(1) ["name"]=> string(7) "SH-Mini" ["currency"]=> int(1) ["monthly"]=> string(4) "4.99" } [79]=> object(stdClass)#301 (4)
{ ["id"]=> int(1) ["name"]=> string(7) "SH-Mini" ["currency"]=> int(2) ["monthly"]=> string(4) "345.23" } [80]=> object(stdClass)#300 (4)
{ ["id"]=> int(1) ["name"]=> string(7) "SH-Mini" ["currency"]=> int(6) ["monthly"]=> string(5) "4.01" } [81]=> object(stdClass)#299 (4)
{ ["id"]=> int(19) ["name"]=> string(8) "SH-Basic" ["currency"]=> int(1) ["monthly"]=> string(4) "5.99" } [82]=> object(stdClass)#298 (4)
{ ["id"]=> int(19) ["name"]=> string(8) "SH-Basic" ["currency"]=> int(2) ["monthly"]=> string(6) "443.44" } [83]=> object(stdClass)#297 (4)
{ ["id"]=> int(19) ["name"]=> string(8) "SH-Basic" ["currency"]=> int(6) ["monthly"]=> string(4) "5.03" } [84]=> object(stdClass)#296 (4)
I want to get the value with id as 19 and currency as 2. The output I require is 443.44
I was trying using PHP foreach for the above json decoded output. It always gives me the first value of 5.99
I am using the below code.
$pid = 19;
$cur = 2;
foreach($products as $product){
$getproductrrice[$product->id] = $product->monthly;
}
return $getproductrrice[$pid];
I need to add one more condition so that I can pass currency and get the output as 443.44.
AS you are looping, test each product to see if it is one you want
$pid = 19;
$cur = 2;
foreach($products as $product){
if ( $product->id == $pid && $product->currency == $cur ) {
$getproductrrice[$product->id] = $product->monthly;
}
}
Related
I have written below lines of code
public function shiftarray($cursor, $arg)
{
$keyarguments = array("first_name","roll_no");
$arrayStudents =array();
foreach ($cursor as $k => $row)
{
foreach ($keyarguments as $key)
{
if (strcasecmp($row[$key], $arg) == 0)
{
array_unshift($arrayStudents, $row);
}
else
{
}
}
}
return $arrayStudents;
}
I have array of students in $cursor like
{ [0]=> array(50) { ["_id"]=> object(MongoId)#23 (1) { ["$id"]=> string(24) "58131c7799fbad4c1d000202" } ["student_id"]=> float(2) ["registration_temp_perm_no"]=> string(1) "1" ["roll_no"]=> float(1) ["admission_date"]=> string(10) "01/07/2016" ["first_name"]=> string(7) "Neil" ["middle_name"]=> string(4) "David" ["last_name"]=> string(6) "Stephan" ["dob"]=> string(10) "12/03/1981" ["gender"]=> string(6) "Female" ["blood_group"]=> string(2) "A+" ["birth_place"]=> string(11) "Sadar Bazar" ["nationality"]=> string(6) "Indian" ["language"]=> string(7) "English" ["religion"]=> string(8) "Agnostic" ["address_line1"]=> string(20) "4148 Hazelcrest Hill" ["address_line2"]=> string(20) "22883 Memorial Place" ["city"]=> string(11) "Sadar Bazar" ["state"]=> string(13) "Uttar Pradesh" ["pincode"]=> string(6) "190010" ["country"]=> string(5) "India" ["phone1"]=> string(10) "9039180419" ["phone2"]=> string(10) "7681559402" ["email"]=> string(24) "educianstudent#gmail.com" ["is_sms_enabled"]=> string(3) "Yes" ["is_active"]=> int(1) ["has_finished"]=> int(0) ["student_category"]=> string(1) "5" ["course"]=> string(24) "58131c7099fbad4c1d0001c2" ["Biometric_ID"]=> string(1) "1" ["siblings"]=> string(14) "Cynthia Taylor" ["guardian_name"]=> string(14) "Cynthia Taylor" ["guardian_occupation"]=> string(13) "Senior Editor" ["guardian_qualification"]=> string(20) "Research Assistant I" ["guardian_email_id"]=> string(23) "educianparent#gmail.com" ["gaurdain_contact_details"]=> string(10) "9419513603" ["guardian_relationship"]=> string(6) "Father" ["height"]=> string(3) "4.9" ["weight"]=> string(4) "34.9" ["allergies"]=> string(0) "" ["batch"]=> int(2) ["academicyear"]=> string(4) "2015" ["batchhistory"]=> array(1) { [0]=> array(5) { ["batchid"]=> float(2) ["academic_year"]=> string(4) "2015" ["course"]=> string(24) "58131c7099fbad4c1d0001c2" ["sequenceno"]=> int(1) ["courseId"]=> object(MongoId)#24 (1) { ["$id"]=> string(24) "58131c7099fbad4c1d0001c2" } } } ["uploads"]=> array(1) { ["profile_pic"]=> string(39) "58131c7799fbad4c1d000202schoolgirl2.jpg" } ["created_at"]=> NULL ["updated_at"]=> string(0) "" ["routearray"]=> array(2) { [0]=> array(5) { ["routeid"]=> int(2) ["academicyear"]=> string(4) "2016" ["current"]=> int(0) ["vehicleno"]=> string(9) "JK01S8764" ["dateofassignment"]=> string(10) "09/28/2016" } [1]=> array(5) { ["routeid"]=> int(3) ["academicyear"]=> string(4) "2016" ["current"]=> int(1) ["vehicleno"]=> string(9) "JK01S8764" ["dateofassignment"]=> string(10) "11/17/2016" } } ["HostelAlloted"]=> array(7) { ["Food Preferences"]=> string(4) "Both" ["Hostel"]=> object(MongoId)#25 (1) { ["$id"]=> string(24) "58138aee99fbade41e000031" } ["Floor"]=> string(7) "Floor_1" ["RoomNumber"]=> int(11) ["Approved"]=> string(3) "yes" ["Approved On"]=> object(MongoDate)#26 (2) { ["sec"]=> int(1472322600) ["usec"]=> int(0) } ["Academic Year"]=> string(4) "2016" } ["HostelAllotmentHistory"]=> array(1) { [0]=> array(7) { ["Food Preferences"]=> string(4) "Both" ["Hostel"]=> object(MongoId)#27 (1) { ["$id"]=> string(24) "58138aee99fbade41e000031" } ["Floor"]=> string(7) "Floor_1" ["RoomNumber"]=> int(11) ["Approved"]=> string(3) "yes" ["Approved On"]=> object(MongoDate)#28 (2) { ["sec"]=> int(1472322600) ["usec"]=> int(0) } ["Academic Year"]=> string(4) "2016" } } ["courseId"]=> object(MongoId)#29 (1) { ["$id"]=> string(24) "58131c7099fbad4c1d0001c2" } }
...
...
I am trying to bring those student on the beginning of array whose first name is brian and rest of students should at the bottom.
Now the above code place the students on the top of the array but the rest of the students are ommited/removed. I don't want them to be removed.
I am trying to tweak what code to write in else condition.
Please help!!!
Fill $arrayStudents before the loop otherwise only matches will be collected and unshifted.
Extend example of php.net/unshift
$add = array('big');
$queue = array(
array("orange"),
array("banana"),
array("apple1"),
array("raspberry")
);
array_unshift($queue, $add);
print_r($queue);
In your case: search for keys you want to unshift, the rest goes to the standard $list = array(/list of studends/):
$list = array();
$bringToTop = array('brian', 'elvis');
foreach($data as $item) {
if (/* in in the list to bring on top*/) {
$memory[] = $item;
} else {
$list[] = $item;
}
}
foreach($memory as $item) {
array_unshift($list, $item);
}
print_r($list);
Kind regards
Add $arrayStudents[] = $row; to your else scope. Here is a simple demo to illustrate it.
When matches add the element to the result from the start of array, unmatches add them from the end of the array.
I am trying to parse a csv file in php to try and figure out how to get all data between some dates and the sort according to the date in ascending order but I am not able to sort the date column or I have no idea what format it is in please help
ID Reg Date FirstName LastName
1 1278336015 Sergio Roberto
2 1395656121 Ray Wilkins
3 1300276526 Trueman Ted
4 1374087492 Volt John
Please assist thanks
Okay, just for fun.
1. Parse the Data:
You could load the CSV data into an associative array like this (admittedly not very sophisticated, no error checking, validation etc):
$file = file_get_contents('CSV.csv'); // load csv into string
$rows = explode(PHP_EOL, $file); // break each line into array
$data = Array(); // where we'll store the data
foreach ($rows as $i => $row) {
$fields = explode(',', $row); // break each field into array
foreach ($fields as $col => $val) {
if ($i == 0) {
$names[] = $val; // headers in first row
} else {
$colName = $names[$col];
if ($colName == 'Reg Date') $val = date('Y-m-d H:i:s', $val);
$data[$i - 1][$colName] = $val;
}
}
}
So with your sample data above, you get:
array(4) {
[0]=>
array(4) {
["ID"]=>
string(1) "1"
["Reg Date"]=>
string(19) "2010-07-05 06:20:15"
["FirstName"]=>
string(6) "Sergio"
["LastName"]=>
string(7) "Roberto"
}
[1]=>
array(4) {
["ID"]=>
string(1) "2"
["Reg Date"]=>
string(19) "2014-03-24 03:15:21"
["FirstName"]=>
string(3) "Ray"
["LastName"]=>
string(7) "Wilkins"
}
[2]=>
array(4) {
["ID"]=>
string(1) "3"
["Reg Date"]=>
string(19) "2011-03-16 04:55:26"
["FirstName"]=>
string(7) "Trueman"
["LastName"]=>
string(3) "Ted"
}
[3]=>
array(4) {
["ID"]=>
string(1) "4"
["Reg Date"]=>
string(19) "2013-07-17 11:58:12"
["FirstName"]=>
string(4) "Volt"
["LastName"]=>
string(4) "John"
}
}
2. Sort the Data:
You can use usort() to sort by the timestamp column.
usort($data, function ($a, $b) {
if ($a['Reg Date'] == $b['Reg Date']) return 0;
return ($a['Reg Date'] < $b['Reg Date']) ? -1 : 1;
});
Results:
===After Sorting:===
array(4) {
[0]=>
array(4) {
["ID"]=>
string(1) "1"
["Reg Date"]=>
string(19) "2010-07-05 06:20:15"
["FirstName"]=>
string(6) "Sergio"
["LastName"]=>
string(7) "Roberto"
}
[1]=>
array(4) {
["ID"]=>
string(1) "3"
["Reg Date"]=>
string(19) "2011-03-16 04:55:26"
["FirstName"]=>
string(7) "Trueman"
["LastName"]=>
string(3) "Ted"
}
[2]=>
array(4) {
["ID"]=>
string(1) "4"
["Reg Date"]=>
string(19) "2013-07-17 11:58:12"
["FirstName"]=>
string(4) "Volt"
["LastName"]=>
string(4) "John"
}
[3]=>
array(4) {
["ID"]=>
string(1) "2"
["Reg Date"]=>
string(19) "2014-03-24 03:15:21"
["FirstName"]=>
string(3) "Ray"
["LastName"]=>
string(7) "Wilkins"
}
}
3. Filter the Data:
And you can use array_filter to filter the results down. I created a class to handle this (as described by #jensgram answer here)
$from = '2011-01-01';
$to = '2013-12-31';
$filtered = array_filter($data, Array(new compareDates($from, $to), 'isInRange'));
Results:
===After Filtering [2011-01-01 - 2013-12-31]:===
array(2) {
[1]=>
array(4) {
["ID"]=>
string(1) "3"
["Reg Date"]=>
string(19) "2011-03-16 04:55:26"
["FirstName"]=>
string(7) "Trueman"
["LastName"]=>
string(3) "Ted"
}
[2]=>
array(4) {
["ID"]=>
string(1) "4"
["Reg Date"]=>
string(19) "2013-07-17 11:58:12"
["FirstName"]=>
string(4) "Volt"
["LastName"]=>
string(4) "John"
}
}
Here's the simple class I used:
class compareDates
{
function __construct($from, $to)
{
$this->from = $from;
$this->to = $to;
}
function isInRange($ele)
{
if ($ele['Reg Date'] >= $this->from && $ele['Reg Date'] <= $this->to) {
return TRUE;
}
return FALSE;
}
}
NOTE: for the purpose of being able to see the date/time, I stored as
a string. But you would normally just use the numeric value to sort
by. I mention this to say that if you change the format of the date
string, it could break the sorting.
I got this array
array(5) {
[0]=>
array(5) {
["id"]=>
int(1411667077)
["nachricht"]=>
string(13) "iiiiiiiiiiiii"
["user"]=>
string(15) "334607943355808"
["datum"]=>
string(16) "25.09.2014 19:44"
["deleted"]=>
string(0) ""
}
[1]=>
array(5) {
["id"]=>
int(1411701734)
["nachricht"]=>
string(2) "dd"
["user"]=>
string(15) "334607943355808"
["datum"]=>
string(16) "26.09.2014 05:22"
["deleted"]=>
string(0) ""
}
[2]=>
array(5) {
["id"]=>
int(1411701737)
["nachricht"]=>
string(6) "swfsfs"
["user"]=>
string(15) "334607943355808"
["datum"]=>
string(16) "26.09.2014 05:22"
["deleted"]=>
string(0) ""
}
[3]=>
array(5) {
["id"]=>
int(1411701739)
["nachricht"]=>
string(7) "egwegeg"
["user"]=>
string(15) "334607943355808"
["datum"]=>
string(16) "26.09.2014 05:22"
["deleted"]=>
string(0) ""
}
[4]=>
array(5) {
["id"]=>
int(1411742201)
["nachricht"]=>
string(3) "sss"
["user"]=>
string(15) "334607943355808"
["datum"]=>
string(16) "26.09.2014 16:36"
["deleted"]=>
string(0) ""
}
}
I want to cut the where the id is 1411701737 so I tryed:
foreach($array as $arr => $sub_arr)
{
if $sub_arr['id'] == 1411701737
{
break;
}
}
I know I need to create a whole new array in the foreach, but isn't there maybe a build in function?
$arr_new = array(); //Define a new array
foreach($arr_old as $a) { //Loop through the old one
if($a['id'] === 1411701737) break; //If the value of id matches, leave the foreach loop
$arr_new[] = $a; //Else, copy this array to the new array
}
print_r($arr_new); //Print the new array
$new = array();
foreach($array as $k) {
if($k['id'] == 1411701737) break;
array_push($new, $k);
}
var_dump($new);
The answer by Mooseman is likely a faster way to do this since it does not rely on a PHP function call to place the array into the new array. However unless you are doing this on a set of arrays that is in the 100k+ it really won't matter.
i have the following array:
[2]=>
object(stdClass)#9 (4) {
["ID"]=>
string(32) "43c845f895a56fbe8aea9435ef8fa806"
["Type"]=>
string(8) "Campaign"
["Name"]=>
string(28) "An unmissable invitation for"
["Actions"]=>
array(5) {
[0]=>
object(stdClass)#10 (4) {
["Event"]=>
string(4) "Open"
["Date"]=>
string(19) "2013-05-07 17:00:00"
["IPAddress"]=>
string(12) "109.239.93.2"
["Detail"]=>
string(0) ""
}
[1]=>
object(stdClass)#11 (4) {
["Event"]=>
string(4) "Open"
["Date"]=>
string(19) "2013-05-07 09:01:00"
["IPAddress"]=>
string(12) "109.239.93.2"
["Detail"]=>
string(0) ""
}
[2]=>
object(stdClass)#12 (4) {
["Event"]=>
string(4) "Open"
["Date"]=>
string(19) "2013-04-30 22:29:00"
["IPAddress"]=>
string(14) "94.171.192.216"
["Detail"]=>
string(0) ""
}
[3]=>
object(stdClass)#13 (4) {
["Event"]=>
string(5) "Click"
["Date"]=>
string(19) "2013-04-30 17:43:00"
["IPAddress"]=>
string(12) "109.239.93.2"
["Detail"]=>
string(60) "http://www.rbh.co.uk/rbhevent/?name=[fullname]&email=[email]"
}
[4]=>
object(stdClass)#14 (4) {
["Event"]=>
string(4) "Open"
["Date"]=>
string(19) "2013-04-30 17:43:00"
["IPAddress"]=>
string(12) "109.239.93.2"
["Detail"]=>
string(0) ""
}
}
}
i am trying to count the events that are the same. So for example ["Event"] = Open =4 / ["Event"] = Click =1.
I am trying to achieve this via counting a foreach loop:
$i=0;
foreach($entry->Actions as $actions ) {
echo $i++;
}
Im not quite sure how to approach this? Can someone suggest a best practice?
$counts = array();
foreach($entry->Actions as $actions) {
if(!isset($counts[$actions->Event])) {
$counts[$actions->Event] = 0;
}
++$counts[$actions->Event];
}
print_r($counts);
<?php
$amounts = array(); // Events as key
foreach($entry->Actions as $actions)
{
if (isset($amounts[$actions["Event"]])) $amounts[$actions["Event"]]++;
else $amounts[$actions["Event"]] = 1;
}
print_r($amounts);
echo "<br>".$amounts["Open"];
?>
Hi I have the following array
array(4) {
[0]=>
array(3) {
["id_acreditado"]=>
string(3) "174"
["cantidad"]=>
string(7) "4008.00"
["acreditado"]=>
string(27) "Olga Olivia Lucio Hernandez"
}
[1]=>
array(3) {
["id_acreditado"]=>
string(3) "175"
["cantidad"]=>
string(7) "4008.00"
["acreditado"]=>
string(23) "Enrique Carranco Vences"
}
[2]=>
array(3) {
["id_acreditado"]=>
string(3) "176"
["cantidad"]=>
string(7) "4008.00"
["acreditado"]=>
string(32) "Juana Patricia Contreras Paredes"
}
[3]=>
array(3) {
["id_acreditado"]=>
string(3) "177"
["cantidad"]=>
string(7) "4008.00"
["acreditado"]=>
string(17) "Noemi Cruz Campos"
}
}
And I want to create a bidimensional array with some values of the above array and different indices. I'm using a foreach loop to achive that.
$j=1;
foreach($acreditados as $acreditado){
$tmp['oneCol'] = $j;
$tmp['twoCol'] = $acreditado['acreditado'];
$tmp['threeCol'] = $acreditado['cantidad'];
$info['fourCol'] =$acreditado['id_acreditado'];
$info[]=$tmp;
$j++;
}
$tmp is an auxiliar one dimension array that lately is added as a row for bidemensional $info array, however I'm not getting the output expected. I want something like the following as output:
array(4) {
[0]=>
array(3) {
["oneCol"]=>
int(1)
["twoCol"]=>
string(27) "Olga Olivia Lucio Hernandez"
["threeCol"]=>
string(7) "4008.00"
["fourCol"]=>
string(3) "174"
}
[1]=>
array(3) {
["oneCol"]=>
int(2)
["twoCol"]=>
string(23) "Enrique Carranco Vences"
["threeCol"]=>
string(7) "4008.00"
["fourCol"]=>
string(3) "175"
}
[2]=>
array(3) {
["oneCol"]=>
int(3)
["twoCol"]=>
string(32) "Juana Patricia Contreras Paredes"
["threeCol"]=>
string(7) "4008.00"
["fourCol"]=>
string(3) "176"
}
[3]=>
array(3) {
["oneCol"]=>
int(4)
["twoCol"]=>
string(17) "Noemi Cruz Campos"
["threeCol"]=>
string(7) "4008.00"
["fourcol"]=>
string(3) "177"
}
}
You have a code error
The line
$info['fourCol'] =$acreditado['id_acreditado'];
Should be
$tmp['fourCol'] =$acreditado['id_acreditado'];
So the code show be: (With the added $tmp array reset)
$j=1;
foreach($acreditados as $acreditado){
$tmp = array();
$tmp['oneCol'] = $j;
$tmp['twoCol'] = $acreditado['acreditado'];
$tmp['threeCol'] = $acreditado['cantidad'];
$tmp['fourCol'] =$acreditado['id_acreditado'];
$info[] = $tmp;
$j++;
}