This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed last year.
I know this question is very basic to you all, and I'm truly sorry for asking, but I'm studying arrays right now and I've been asked to do the following learning from textbook, which I hate.
I need to loop through the first level and get the FIELD_NAME and FIELD_LABEL.
Then to loop through again and get all CUSTOM_FIELD_OPTIONS > OPTION_VALUE for each first level array.
My problem is that it fails at the first loop. And I can't get my head around second loops.
Can you provide some guidance please? I need some help on this :(
$getAllCustomFields = $api->getAllCustomFields();
foreach ($getAllCustomFields as $item) {
echo addslashes($item->FIELD_NAME) . ": '" . addslashes($item->FIELD_LABEL) . "',";
}
In the array I need to be able to loop and get
array(46) {
["Secondary_Volunteering__c"]=>
array(12) {
["FIELD_NAME"]=>
string(25) "Secondary_Volunteering__c"
["FIELD_ORDER"]=>
int(2)
["FIELD_FOR"]=>
string(7) "CONTACT"
["FIELD_LABEL"]=>
string(22) "Secondary Volunteering"
["FIELD_TYPE"]=>
string(11) "MULTISELECT"
["FIELD_HELP_TEXT"]=>
NULL
["DEFAULT_VALUE"]=>
NULL
["EDITABLE"]=>
bool(true)
["VISIBLE"]=>
bool(true)
["CUSTOM_FIELD_OPTIONS"]=>
array(9) {
[0]=>
array(3) {
["OPTION_ID"]=>
int(3)
["OPTION_VALUE"]=>
string(6) "Events"
["OPTION_DEFAULT"]=>
bool(false)
}
[1]=>
array(3) {
["OPTION_ID"]=>
int(14)
["OPTION_VALUE"]=>
string(28) "Flagship Programme"
["OPTION_DEFAULT"]=>
bool(false)
}
}
["DEPENDENCY"]=>
NULL
["JOIN_OBJECT"]=>
NULL
}
["Primary_Volunteering__c"]=>
array(12) {
["FIELD_NAME"]=>
string(23) "Primary_Volunteering__c"
["FIELD_ORDER"]=>
int(3)
["FIELD_FOR"]=>
string(7) "CONTACT"
["FIELD_LABEL"]=>
string(20) "Primary Volunteering"
["FIELD_TYPE"]=>
string(11) "MULTISELECT"
["FIELD_HELP_TEXT"]=>
NULL
["DEFAULT_VALUE"]=>
NULL
["EDITABLE"]=>
bool(true)
["VISIBLE"]=>
bool(true)
["CUSTOM_FIELD_OPTIONS"]=>
array(27) {
[0]=>
array(3) {
["OPTION_ID"]=>
int(87)
["OPTION_VALUE"]=>
string(9) "Animal Me"
["OPTION_DEFAULT"]=>
bool(false)
}
[1]=>
array(3) {
["OPTION_ID"]=>
int(51)
["OPTION_VALUE"]=>
string(18) "Animals inc Plants"
["OPTION_DEFAULT"]=>
bool(false)
}
[2]=>
array(3) {
["OPTION_ID"]=>
int(13)
["OPTION_VALUE"]=>
string(3) "Art"
["OPTION_DEFAULT"]=>
bool(false)
}
[3]=>
array(3) {
["OPTION_ID"]=>
int(21)
["OPTION_VALUE"]=>
string(12) "Bright Stars"
["OPTION_DEFAULT"]=>
bool(false)
}
}
["DEPENDENCY"]=>
NULL
["JOIN_OBJECT"]=>
NULL
}
}
foreach($getAllCustomFields as $fields){
foreach($fields as $field){
echo( '<strong>'.$field["FIELD_LABEL"].'</strong><br>');
foreach ($field["CUSTOM_FIELD_OPTIONS"] as $cfield){
echo($cfield["OPTION_VALUE"].'<br>');
}
echo('<br>');
}
}
Related
I'm trying to search the array and to look for keywords at the second level of the array, like ["Secondary_Volunteering__c"] and Referral_Source_within_CF__c.
If those values are found then keep them, if not delete them.
I have created the following but it comes back blank.
Any guidance will be appreciated.
foreach($getAllCustomerCustomFields as $k => $v) {
if(!in_array(["Secondary_Volunteering__c"], $v) && !in_array("Referral_Source_within_CF__c", $v))
unset($getAllCustomerCustomFields[$k]);
}
foreach($getAllCustomerCustomFields as $fields){
foreach($fields as $field){
echo( '<strong>'.$field["FIELD_LABEL"].'</strong><br>');
foreach ($field["CUSTOM_FIELD_OPTIONS"] as $cfield){
echo($cfield["OPTION_VALUE"].'<br>');
}
echo('<br>');
}
}
This is the PHP array.
array(1) {
["CUSTOMFIELDS"]=>
array(46) {
["Secondary_Volunteering__c"]=>
array(12) {
["FIELD_NAME"]=>
string(25) "Secondary_Volunteering__c"
["FIELD_ORDER"]=>
int(2)
["FIELD_FOR"]=>
string(7) "CONTACT"
["FIELD_LABEL"]=>
string(22) "Secondary Volunteering"
["FIELD_TYPE"]=>
string(11) "MULTISELECT"
["FIELD_HELP_TEXT"]=>
NULL
["DEFAULT_VALUE"]=>
NULL
["EDITABLE"]=>
bool(true)
["VISIBLE"]=>
bool(true)
["CUSTOM_FIELD_OPTIONS"]=>
array(9) {
[0]=>
array(3) {
["OPTION_ID"]=>
int(3)
["OPTION_VALUE"]=>
string(6) "Events"
["OPTION_DEFAULT"]=>
bool(false)
}
}
["DEPENDENCY"]=>
NULL
["JOIN_OBJECT"]=>
NULL
}
["Primary_Volunteering__c"]=>
array(12) {
["FIELD_NAME"]=>
string(23) "Primary_Volunteering__c"
["FIELD_ORDER"]=>
int(3)
["FIELD_FOR"]=>
string(7) "CONTACT"
["FIELD_LABEL"]=>
string(20) "Primary Volunteering"
["FIELD_TYPE"]=>
string(11) "MULTISELECT"
["FIELD_HELP_TEXT"]=>
NULL
["DEFAULT_VALUE"]=>
NULL
["EDITABLE"]=>
bool(true)
["VISIBLE"]=>
bool(true)
["CUSTOM_FIELD_OPTIONS"]=>
array(27) {
[0]=>
array(3) {
["OPTION_ID"]=>
int(87)
["OPTION_VALUE"]=>
string(9) "Animal Me"
["OPTION_DEFAULT"]=>
bool(false)
}
}
["DEPENDENCY"]=>
NULL
["JOIN_OBJECT"]=>
NULL
}
["Referral_Source_within_CF__c"]=>
array(12) {
["FIELD_NAME"]=>
string(30) "Referral_Source_within_CF__c"
["FIELD_ORDER"]=>
int(4)
["FIELD_FOR"]=>
string(7) "CONTACT"
["FIELD_LABEL"]=>
string(27) "Referral Source within CF"
["FIELD_TYPE"]=>
string(11) "MULTISELECT"
["FIELD_HELP_TEXT"]=>
NULL
["DEFAULT_VALUE"]=>
NULL
["EDITABLE"]=>
bool(true)
["VISIBLE"]=>
bool(true)
["CUSTOM_FIELD_OPTIONS"]=>
array(8) {
[0]=>
array(3) {
["OPTION_ID"]=>
int(1)
["OPTION_VALUE"]=>
string(11) "Sarah "
["OPTION_DEFAULT"]=>
bool(false)
}
}
["DEPENDENCY"]=>
NULL
["JOIN_OBJECT"]=>
NULL
}
If I understand you correctly, it looks like array_filter will help you here...
<?php
$getAllCustomerCustomFields = array_filter($getAllCustomerCustomFields, function($key) {
return $key == 'Secondary_Volunteering__c' || $key == 'Referral_Source_within_CF__c';
}, ARRAY_FILTER_USE_KEY)
This basically filters off (reduces) all the unwanted keys of the array
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 want to parse JSON that I recieve from steam via api link http://steamcommunity.com/id/theorangeass/inventory/json/753/1/ , but when I try to print it with echo it show nothing.
Here is the code
$data = file_get_contents('http://steamcommunity.com/id/theorangeass/inventory/json/753/1/');
$json = json_decode($data, true);
echo $json->success;
var_dump:
array(6) { ["success"]=> bool(true) ["rgInventory"]=> array(1) { ["922506184369981039"]=> array(5) { ["id"]=> string(18) "922506184369981039" ["classid"]=> string(10) "1254492673" ["instanceid"]=> string(10) "2070301907" ["amount"]=> string(1) "1" ["pos"]=> int(1) } } ["rgCurrency"]=> array(0) { } ["rgDescriptions"]=> array(1) { ["1254492673_2070301907"]=> array(18) { ["appid"]=> string(3) "753" ["classid"]=> string(10) "1254492673" ["instanceid"]=> string(10) "2070301907" ["icon_url"]=> string(116) "U8721VM9p9C2v1o6cKJ4qEnGqnE7IoTQgZI-VTdwyTBeimAcIowbqB-harb00cJ0fNdiCJoFB3O541FNc9ZPYXYjjL7UqfFEwOtgZKcs0eWlClqzSJn6" ["icon_url_large"]=> string(106) "U8721VM9p9C2v1o6cKJ4qEnGqnE7IoTQgZI-VTdwyTBeimAcIowbqB-harb00cJ0fNdiA54UEGOnqGQPJ9hDZHA50feEo7RMyO_GQNzkkA" ["icon_drag_url"]=> string(0) "" ["name"]=> string(4) "BEEP" ["market_name"]=> string(0) "" ["name_color"]=> string(0) "" ["background_color"]=> string(0) "" ["type"]=> string(4) "Gift" ["tradable"]=> int(0) ["marketable"]=> int(0) ["commodity"]=> int(0) ["cache_expiration"]=> string(20) "2017-01-02T00:00:00Z" ["fraudwarnings"]=> array(1) { [0]=> string(223) "This is a restricted gift which can only be redeemed in these countries: Armenia, Azerbaijan, Belarus, Georgia, Kyrgyzstan, Kazakhstan, Moldova, Republic of, Tajikistan, Turkmenistan, Uzbekistan, Ukraine, Russian Federation" } ["descriptions"]=> array(1) { [0]=> array(1) { ["value"]=> string(216) "A combination of Yoshi’s Island-style platforming with a gravity gun right out of Half-Life, BEEP is an amazing physics-platformer. Despite its friendly art style, this is a hardcore platformer in the truest sense." } } ["actions"]=> array(1) { [0]=> array(2) { ["name"]=> string(13) "View in store" ["link"]=> string(41) "http://store.steampowered.com/app/104200/" } } } } ["more"]=> bool(false) ["more_start"]=> bool(false) }
You are trying to echo a boolean. If you are wanting to echo true you're going to need to do an if, or a switch statement. An if statement would probably be easiest.
echo $json->success == true ? 'TRUE' : 'FALSE';
To parse the array to retrieve the item id's in the rgInventory to must do a foreach.
foreach ($json['rgInventory'] as $item) {
echo $item['id'];
}
Where you should start learning about arrays is Here
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 am using NOTORM to generate queries, and there is one case on the application where the user can select fields, conditions, etc and thus basically come up with custom queries. That way the user can generate a report like 'people with first name = Jack that have spent > 800'.
I am using NOTORM to generate this query, but I'd like to store the query for usage by another part of the application that is not using NOTORM. Is there any function to retrieve the query generated?
This is how the NOTORM object looks after the queries were generated and executed:
object(NotORM_Result)#55 (33) {
["single":protected]=>
bool(false)
["select":protected]=>
array(0) {
}
["conditions":protected]=>
array(7) {
[0]=>
string(9) "name = ? "
[1]=>
string(12) "surname = ? "
[2]=>
string(15) "custom_field_19"
[3]=>
string(31) "custom_field_20 BETWEEN ? AND ?"
[4]=>
string(21) "custom_field_18 <= ? "
[5]=>
string(15) "custom_field_16"
[6]=>
string(10) "email = ? "
}
["where":protected]=>
array(7) {
[0]=>
string(9) "name = ? "
[1]=>
string(12) "surname = ? "
[2]=>
string(52) "custom_field_19 IN ('Yes', 'Sent lender info', 'No')"
[3]=>
string(31) "custom_field_20 BETWEEN ? AND ?"
[4]=>
string(21) "custom_field_18 <= ? "
[5]=>
string(28) "custom_field_16 IN ('Buyer')"
[6]=>
string(10) "email = ? "
}
["parameters":protected]=>
array(6) {
[0]=>
string(1) "a"
[1]=>
string(6) "Franco"
[2]=>
int(1218146400)
[3]=>
int(1249682400)
[4]=>
string(3) "800"
[5]=>
string(1) "?"
}
["order":protected]=>
array(0) {
}
["limit":protected]=>
NULL
["offset":protected]=>
NULL
["group":protected]=>
string(0) ""
["having":protected]=>
string(0) ""
["lock":protected]=>
NULL
["union":protected]=>
array(0) {
}
["unionOrder":protected]=>
array(0) {
}
["unionLimit":protected]=>
NULL
["unionOffset":protected]=>
NULL
["data":protected]=>
NULL
["referencing":protected]=>
array(0) {
}
["aggregation":protected]=>
array(0) {
}
["accessed":protected]=>
NULL
["access":protected]=>
NULL
["keys":protected]=>
array(0) {
}
["connection":protected]=>
NULL
["driver":protected]=>
NULL
["structure":protected]=>
NULL
["cache":protected]=>
NULL
["notORM":protected]=>
object(NotORM)#3 (12) {
["connection":protected]=>
object(PDO)#2 (0) {
}
["driver":protected]=>
string(5) "mysql"
["structure":protected]=>
object(NotORM_Structure_Convention)#4 (4) {
["primary":protected]=>
string(2) "id"
["foreign":protected]=>
string(5) "%s_id"
["table":protected]=>
string(2) "%s"
["prefix":protected]=>
string(0) ""
}
["cache":protected]=>
NULL
["notORM":protected]=>
NULL
["table":protected]=>
NULL
["primary":protected]=>
NULL
["rows":protected]=>
NULL
["referenced":protected]=>
array(0) {
}
["debug":protected]=>
bool(false)
["freeze":protected]=>
bool(false)
["rowClass":protected]=>
string(10) "NotORM_Row"
}
["table":protected]=>
string(5) "users"
["primary":protected]=>
string(2) "id"
["rows":protected]=>
NULL
["referenced":protected]=>
array(0) {
}
["debug":protected]=>
bool(false)
["freeze":protected]=>
bool(false)
["rowClass":protected]=>
string(10) "NotORM_Row"
}
I could do some kind of script that iterates over 'where' and 'parameters' but seems not too elegant, especially since the order of those arrays is not correct (I would have to do something like replace each "?" with the next item in "parameters")... Any NOTORM functions? Or any more elegant way that you suggest?
This is the 'where' and 'parameters' part in case that you have something elegant for the last option:
["where":protected]=>
array(7) {
[0]=>
string(9) "name = ? "
[1]=>
string(12) "surname = ? "
[2]=>
string(52) "custom_field_19 IN ('Yes', 'Sent lender info', 'No')"
[3]=>
string(31) "custom_field_20 BETWEEN ? AND ?"
[4]=>
string(21) "custom_field_18 <= ? "
[5]=>
string(28) "custom_field_16 IN ('Buyer')"
[6]=>
string(10) "email = ? "
}
["parameters":protected]=>
array(6) {
[0]=>
string(1) "a"
[1]=>
string(6) "Franco"
[2]=>
int(1218146400)
[3]=>
int(1249682400)
[4]=>
string(3) "800"
[5]=>
string(1) "?"
}
Just convert the NotORM_Result object to an string
$sql = (string)$result;
//or
$sql = $result->__toString();