Sorry for the question I am not PHP skiller :)
I have a problem with parsing data from JSON.
This is my JSON after dump
$response = curl_exec($ch);
$data = json_decode($response, true);
$c = count($data['products']);
var_dump($data);
array(4) {
["products"]=>
array(23347) {
[0]=>
array(25) {
["name"]=>
string(15) "DOOKY Strieška"
["description"]=>
string(0) ""
["count"]=>
float(0)
["measureType"]=>
string(2) "ks"
["price"]=>
float(20.83)
["vat"]=>
float(20)
["stockCardNumber"]=>
string(6) "000004"
["typeId"]=>
int(1)
["warehouseCode"]=>
string(3) "MOL"
["timestamp"]=>
string(19) "2019-05-09T10:38:30"
["foreignName"]=>
string(0) ""
["customText"]=>
string(12) "silver stars"
["warrantyPeriod"]=>
float(0)
["weightPerUnit"]=>
float(0)
["volumePerUnit"]=>
float(0)
["numberingSequenceCode"]=>
string(2) "SK"
["ean"]=>
string(13) "5038278982969"
["stn"]=>
string(0) ""
["jkpov"]=>
string(6) "126367"
["din"]=>
string(0) ""
["bn"]=>
string(0) ""
["plu"]=>
int(0)
["warrantyPeriodTypeId"]=>
int(0)
["priceWithVat"]=>
float(25)
["eshop"]=>
NULL
}
I need to get every EAN, JKPOV, COUNT to variable, and insert to MySQL
I have this code but it doesn't work properly, because not write me all data. But in var_dump($data); I have this missing data...
$i=0;
for ($i=0; $i <= $c; $i++) {
$ean = $data['products'][$i]['ean'];
$count = $data['products'][$i]['count'];
$jkpov = $data['products'][$i]['jkpov'];
conf::$db->query("insert ignore into sklad set `ean`='$ean', `jkpov`='$jkpov', `count`='$count'");
$i++; }
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 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 have this array, that is dumped from WooCommerce using these lines:
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
// some code here…
}
If I make a raw output of my cart ($items) and I get this:
["f584d8671586d336d84e8cf9ed43303c"]=> array(11) {
["booking"]=> array(15) {
["_year"]=> int(2016)
["_month"]=> int(8)
["_day"]=> int(28)
["_persons"]=> array(1) {
[0]=> int(1)
}
["_date"]=> string(9) "2016-8-28"
["date"]=> string(13) "28 août 2016"
["_time"]=> string(5) "21:30"
["time"]=> string(11) "21 h 30 min"
["_qty"]=> int(1)
["Personnes"]=> int(1)
["_start_date"]=> int(1472419800)
["_end_date"]=> int(1472421600)
["_all_day"]=> int(0)
["_cost"]=> int(0)
["_booking_id"]=> int(13013)
}
["product_id"]=> int(12856)
["variation_id"]=> int(0)
["variation"]=> array(0) { }
["quantity"]=> int(1)
["line_total"]=> float(0)
["line_tax"]=> int(0)
["line_subtotal"]=> int(0)
["line_subtotal_tax"]=> int(0)
["line_tax_data"]=> array(2) {
["total"]=> array(0) { }
["subtotal"]=> array(0) { }
}
["data"]=> object(WC_Product_Booking)#11131 (20) {
["availability_rules":"WC_Product_Booking":private]=> array(0) { }
["id"]=> int(12856)
["post"]=> object(WP_Post)#11132 (24) {
["ID"]=> int(12856)
["post_author"]=> string(2) "14"
["post_date"]=> string(19) "2016-08-16 22:04:09"
["post_date_gmt"]=> string(19) "2016-08-16 20:04:09"
["post_content"]=> string(0) ""
["post_title"]=> string(10) "La Cuchara"
["post_excerpt"]=> string(0) ""
["post_status"]=> string(7) "publish"
["comment_status"]=> string(4) "open"
["ping_status"]=> string(6) "closed"
["post_password"]=> string(0) ""
["post_name"]=> string(12) "la-cuchara-2"
["to_ping"]=> string(0) ""
["pinged"]=> string(0) ""
["post_modified"]=> string(19) "2016-08-16 22:13:52"
["post_modified_gmt"]=> string(19) "2016-08-16 20:13:52"
["post_content_filtered"]=> string(0) ""
["post_parent"]=> int(0)
["guid"]=> string(59) ""
["menu_order"]=> int(0)
["post_type"]=> string(7) "product"
["post_mime_type"]=> string(0) ""
["comment_count"]=> string(1) "0"
["filter"]=> string(3) "raw"
}
["product_type"]=> string(7) "booking"
["shipping_class":protected]=> string(0) ""
["shipping_class_id":protected]=> int(0)
["total_stock"]=> NULL
["supports":protected]=> array(0) { }
["price"]=> string(1) "0"
["wc_display_cost"]=> string(0) ""
["wc_booking_base_cost"]=> string(0) ""
["wc_booking_min_duration"]=> string(1) "1"
["wc_booking_cost"]=> string(0) ""
["wc_booking_has_resources"]=> string(2) "no"
["wc_booking_has_persons"]=> string(3) "yes"
["wc_booking_has_person_types"]=> string(2) "no"
["wc_booking_min_persons_group"]=> string(1) "1"
["tax_status"]=> string(7) "taxable"
["stock_status"]=> string(7) "instock"
["manage_stock"]=> string(2) "no"
}
}
I would like to use specific data values in some php variables, and I don't find the right way to achieve this…
How can I do to get the specific value from ["wc_booking_has_resources"]?
Thanks
FIRST — Try first this (but I am unsure as I think it can be an object, so may be it will not work):
echo $items[0]['data']['wc_booking_has_resources'] . '<br>';
SECOND — With a foreach loop it will work certainly:
$items = WC()->cart->get_cart();
foreach($items as $item) {
$item_data = $item['data'];
}
// displaying the value for test
echo 'Booking has ressources: ' . $item_data['wc_booking_has_resources'];
// or may be this one (as $item['data'] is an object)
echo 'Booking has ressources: ' . $item_data->wc_booking_has_resources;
One of these will work, for sure…
Update related to author comments
How to get The booking try one of these:
// first (very unsure of this one)
$item_data = $items[0]['data']['id'];
echo $item_data['id'] . '<br>'; // not sure this one work
Using a for each loop it will be much better:
$items = WC()->cart->get_cart();
foreach($items as $item) {
$item_data = $item['data'];
}
// As $item['data'] is an object
$booking_has_resources = $item_data->booking_has_resources;
// displaying
echo 'Booking has ressources: ' . $booking_has_resources;
This should work
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"];
?>