thanks for checking out my question i have the following json data
array(568) {
[0]=>
array(11) {
["ID"]=>
string(6) "000001"
["Portrait"]=>
string(12) "BaseGoku.png"
["Name"]=>
string(42) "[The Saiyan who grew up on Earth] Son Goku"
["Series"]=>
string(11) "Dragon Ball"
["MaxRarity"]=>
string(6) "6 Star"
["Type"]=>
string(4) "Blue"
["Class"]=>
string(4) "Tank"
["Era"]=>
string(4) "1980"
["ReleaseDate"]=>
string(10) "2018-03-28"
["Farmable"]=>
string(1) "0"
["Method"]=>
string(17) "Trade Medal Store"
}
[1]=>
array(11) {
["ID"]=>
string(6) "000003"
["Portrait"]=>
string(8) "Goku.png"
["Name"]=>
string(8) "Son Goku"
["Series"]=>
string(11) "Dragon Ball"
["MaxRarity"]=>
string(6) "6 Star"
["Type"]=>
string(6) "Yellow"
["Class"]=>
string(3) "DPS"
["Era"]=>
string(4) "1980"
["ReleaseDate"]=>
string(10) "2018-03-28"
["Farmable"]=>
string(1) "0"
["Method"]=>
string(14) "Standard Gacha"
} etc
i have been tring to decode this and pull the data using a future
Future<Users> fetchInfo() async {
final response = await http.get(jsonplaceholder);
final jsonresponse = json.decode(response.body);
return Users.fromJson(jsonresponse[8]);
}
but i keep getting the error "Error FormatException: Unexpected character(at character 1) array(568)
not to sure what im doing wrong as i am new to flutter
any help would be appreciated thanks
heres the users model
class Users {
final int userID;
final String name;
final String portrait;
final String series;
Users({this.name, this.userID, this.portrait, this.series});
factory Users.fromJson(Map<String, dynamic> usersjson)=> Users(
name: usersjson["name"],
);
}
First of all are you sure this is the json data you're getting cause it should be like
[
{
"ID": "000001"
"Name": "[The Saiyan who grew up on Earth] Son Goku",
"Portrait": "BaseGoku.png",
"Series": "Dragon Ball",
"MaxRarity": "6 start",
"Type": "Blue",
"Class": "Tank",
"Era": "1980",
"Release Date": "2018-03-28",
"Farmable": "0",
"Method": "Trade Medal Store"
}
]
Flutter expects Map type for json.decode. If it's a public api can you please post it here.
Related
I want to get hold of the value of DashboardCalc, which lies in the following array:
array(1) {
[0]=>
object(MatrixSubHeadline)#27226 (4) {
["schemaHeadline":"MatrixSubHeadline":private]=>
object(common_db_schemacontent)#28107 (12) {
["viewhelp":"common_db_schemacontent":private]=>
NULL
["unsavedTranslations"]=>
array(0) {
}
["objectLangCode":"common_translation_translateobject":private]=>
string(0) ""
["transUseFallback":"common_translation_translateobject":private]=>
bool(true)
["classData":protected]=>
array(15) {
["SchemaContentID"]=>
string(5) "15611"
["SchemaID"]=>
string(3) "132"
["IndexNo"]=>
string(1) "0"
["PageNo"]=>
string(1) "7"
["ContentType"]=>
string(8) "headline"
["QuestionID"]=>
string(1) "0"
["HeadlineContentID"]=>
string(1) "0"
["ImagePath"]=>
string(0) ""
["ThumbPath"]=>
string(0) ""
["Text"]=>
string(21) "Generelle kommentarer"
["ContentSettings"]=>
string(2) "[]"
["CreatedUserID"]=>
string(2) "15"
["CreatedTime"]=>
string(19) "2021-11-11 12:36:00"
["UpdatedUserID"]=>
string(2) "15"
["UpdatedTime"]=>
string(19) "2021-12-17 15:35:00"
}
["classChanges":protected]=>
array(0) {
}
["tableName":protected]=>
string(13) "schemacontent"
["primaryIDName":protected]=>
string(15) "SchemaContentID"
["cacheEnabled":protected]=>
bool(true)
["accountTable":protected]=>
bool(false)
["extraData":protected]=>
array(0) {
}
["saveError":"common_library_dbbase":private]=>
bool(false)
}
["questionList":"MatrixSubHeadline":private]=>
array(1) {
[0]=>
object(MatrixQuestion)#27225 (4) {
["content":"MatrixQuestion":private]=>
object(common_db_schemacontent)#28106 (12) {
["viewhelp":"common_db_schemacontent":private]=>
NULL
["unsavedTranslations"]=>
array(0) {
}
["objectLangCode":"common_translation_translateobject":private]=>
string(0) ""
["transUseFallback":"common_translation_translateobject":private]=>
bool(true)
["classData":protected]=>
array(15) {
["SchemaContentID"]=>
string(5) "15612"
["SchemaID"]=>
string(3) "132"
["IndexNo"]=>
string(1) "1"
["PageNo"]=>
string(1) "7"
["ContentType"]=>
string(8) "question"
["QuestionID"]=>
string(3) "923"
["HeadlineContentID"]=>
string(5) "15611"
["ImagePath"]=>
string(0) ""
["ThumbPath"]=>
string(0) ""
["Text"]=>
string(0) ""
["ContentSettings"]=>
string(21) "{"criteriaenabled":0}"
["CreatedUserID"]=>
string(2) "15"
["CreatedTime"]=>
string(19) "2021-11-11 12:36:00"
["UpdatedUserID"]=>
string(2) "15"
["UpdatedTime"]=>
string(19) "2021-12-17 15:35:00"
}
["classChanges":protected]=>
array(0) {
}
["tableName":protected]=>
string(13) "schemacontent"
["primaryIDName":protected]=>
string(15) "SchemaContentID"
["cacheEnabled":protected]=>
bool(true)
["accountTable":protected]=>
bool(false)
["extraData":protected]=>
array(0) {
}
["saveError":"common_library_dbbase":private]=>
bool(false)
}
["question":"MatrixQuestion":private]=>
object(common_db_question)#27289 (13) {
["schemaUsage":"common_db_question":private]=>
NULL
["viewHelper":"common_db_question":private]=>
NULL
["unsavedTranslations"]=>
array(0) {
}
["objectLangCode":"common_translation_translateobject":private]=>
string(0) ""
["transUseFallback":"common_translation_translateobject":private]=>
bool(true)
["classData":protected]=>
array(23) {
["QuestionID"]=>
string(3) "923"
["OwnerAccountID"]=>
string(1) "0"
["Question"]=>
string(21) "Generelle kommentarer"
["QuestionType"]=>
string(1) "5"
["Answers"]=>
string(178) "{"answers":null,"groupanswers":[{"count":"1","text":"Ja, jeg vil gerne tilf\u00f8je en generel kommentar","index":"0"}],"questions":["Har du andre kommentarer til din trivsel?"]}"
["SubText"]=>
string(59) "(Skriv dine kommentarer ved at trykke på kommentar-ikonet)"
["AllowOther"]=>
string(1) "0"
["MixAnswers"]=>
string(1) "0"
["MustAnswer"]=>
string(1) "0"
["MinAnswers"]=>
string(1) "0"
["MaxAnswers"]=>
string(1) "0"
["Columns"]=>
string(1) "1"
["OtherText"]=>
string(0) ""
["CreatedUserID"]=>
string(2) "15"
["CreatedTime"]=>
string(19) "2021-11-11 12:36:00"
["UpdatedUserID"]=>
string(2) "15"
["UpdatedTime"]=>
string(19) "2021-11-11 12:44:00"
["DashboardShow"]=>
string(1) "0"
["DashboardCalc"]=>
string(1) "0"
["QuestionTags"]=>
string(1) "0"
["UseComment"]=>
string(1) "1"
["IsPublic"]=>
string(1) "0"
["StdAnswer"]=>
string(1) "0"
}
["classChanges":protected]=>
array(0) {
}
["tableName":protected]=>
string(8) "question"
["primaryIDName":protected]=>
string(10) "QuestionID"
["cacheEnabled":protected]=>
bool(true)
["accountTable":protected]=>
bool(false)
["extraData":protected]=>
array(0) {
}
["saveError":"common_library_dbbase":private]=>
bool(false)
}
["text":"MatrixQuestion":private]=>
string(41) "Har du andre kommentarer til din trivsel?"
["index":"MatrixQuestion":private]=>
int(0)
}
}
["questionMap":"MatrixSubHeadline":private]=>
array(1) {
[15612]=>
array(1) {
[0]=>
int(0)
}
}
["useComments":"MatrixSubHeadline":private]=>
bool(true)
}
}
Please note: the array contains a few objects, which are private. I am not sure whether private objects can be accessed or not, or if the "private" is about the name, or the content.
The data is inherited from a database.
You can access object value by using loop iteration like this:
I am selecting here SchemaContentID, SchemaID and IndexNo.
{% for arrayObj in Objs %}
<ul>
<li>{{ Objs.SchemaContentID}}</li>
<li>{{ Objs.SchemaID}}</li>
<li>{{ Objs.IndexNo}}</li>
</ul>
{% endfor %}
I am using this function to return all available image size as array.
function king_image_size() {
$image_sizes = get_intermediate_image_sizes();
foreach ($image_sizes as $tax) {
$image_sizes[$tax] = $tax;
}
return $image_sizes
}
But this duplicates the return array. Output
array(18) { [0]=> string(9) "thumbnail" [1]=> string(6) "medium" [2]=> string(12) "medium_large" [3]=> string(5) "large" [4]=> string(9) "dhaka-por" [5]=> string(11) "dhaka-por-h" [6]=> string(15) "dhaka-landscape" [7]=> string(7) "dhaka-h" [8]=> string(8) "dhaka-sq" ["thumbnail"]=> string(9) "thumbnail" ["medium"]=> string(6) "medium" ["medium_large"]=> string(12) "medium_large" ["large"]=> string(5) "large" ["dhaka-por"]=> string(9) "dhaka-por" ["dhaka-por-h"]=> string(11) "dhaka-por-h" ["dhaka-landscape"]=> string(15) "dhaka-landscape" ["dhaka-h"]=> string(7) "dhaka-h" ["dhaka-sq"]=> string(8) "dhaka-sq" }
Anotherway that I found very common,
function king_image_size() {
$image_sizes = get_intermediate_image_sizes();
return $image_sizes;
}
Then I am getting 0,1,2,3... output in page builder. I need the function to work like
function king_image_size(){
return array(
'medium' => 'medium' ,
);
}
So you just want an array that uses the values you got, as the keys as well …?
For that, you can simply use array_combine, and just feed the value array in twice:
function king_image_size() {
$image_sizes = get_intermediate_image_sizes();
return array_combine($image_sizes, $image_sizes);
}
I have a client who wants to re-route all the carts from his various WooCommerce stores to one cart. For example the carts from stores A, B, C, and D will be passed to store F's payment gateway.
I figured out a way to do this by taking the contents of a cart from any of the stores and passing that to the function that makes the order in WooCommerce, but I have a problem here because if I var_dump WC()->cart->get_cart() it returns this array:
array(1) {
["98f13708210194c475687be6106a3b84"]=>
array(10) {
["product_id"]=>
int(20)
["variation_id"]=>
int(0)
["variation"]=>
array(0) {
}
["quantity"]=>
int(2)
["line_total"]=>
float(3001.98)
["line_subtotal"]=>
float(3001.98)
["line_tax"]=>
float(0)
["line_subtotal_tax"]=>
float(0)
["line_tax_data"]=>
array(2) {
["total"]=>
array(0) {
}
["subtotal"]=>
array(0) {
}
}
["data"]=>
object(WC_Product_Simple)#8809 (12) {
["object_type":protected]=>
string(7) "product"
["post_type":protected]=>
string(7) "product"
["cache_group":protected]=>
string(8) "products"
["data":protected]=>
array(48) {
["name"]=>
string(7) "Jet sky"
["slug"]=>
string(7) "jet-sky"
["date_created"]=>
object(WC_DateTime)#8801 (4) {
["utc_offset":protected]=>
int(0)
["date"]=>
string(26) "2017-06-23 15:25:27.000000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+00:00"
}
["date_modified"]=>
object(WC_DateTime)#8804 (4) {
["utc_offset":protected]=>
int(0)
["date"]=>
string(26) "2017-06-23 15:25:27.000000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+00:00"
}
["status"]=>
string(7) "publish"
["featured"]=>
bool(false)
["catalog_visibility"]=>
string(7) "visible"
["description"]=>
string(278) "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo."
["short_description"]=>
string(93) "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas."
["sku"]=>
string(0) ""
["price"]=>
string(7) "1500.99"
["regular_price"]=>
string(7) "1500.99"
["sale_price"]=>
string(0) ""
["date_on_sale_from"]=>
NULL
["date_on_sale_to"]=>
NULL
["total_sales"]=>
int(0)
["tax_status"]=>
string(7) "taxable"
["tax_class"]=>
string(0) ""
["manage_stock"]=>
bool(false)
["stock_quantity"]=>
NULL
["stock_status"]=>
string(7) "instock"
["backorders"]=>
string(2) "no"
["sold_individually"]=>
bool(false)
["weight"]=>
string(0) ""
["length"]=>
string(0) ""
["width"]=>
string(0) ""
["height"]=>
string(0) ""
["upsell_ids"]=>
array(0) {
}
["cross_sell_ids"]=>
array(0) {
}
["parent_id"]=>
int(0)
["reviews_allowed"]=>
bool(true)
["purchase_note"]=>
string(0) ""
["attributes"]=>
array(0) {
}
["default_attributes"]=>
array(0) {
}
["menu_order"]=>
int(0)
["virtual"]=>
bool(false)
["downloadable"]=>
bool(false)
["category_ids"]=>
array(0) {
}
["tag_ids"]=>
array(0) {
}
["shipping_class_id"]=>
int(0)
["downloads"]=>
array(0) {
}
["image_id"]=>
string(2) "18"
["gallery_image_ids"]=>
array(1) {
[0]=>
int(19)
}
["download_limit"]=>
int(-1)
["download_expiry"]=>
int(-1)
["rating_counts"]=>
array(0) {
}
["average_rating"]=>
string(1) "0"
["review_count"]=>
int(0)
}
["supports":protected]=>
array(1) {
[0]=>
string(16) "ajax_add_to_cart"
}
["id":protected]=>
int(20)
["changes":protected]=>
array(0) {
}
["object_read":protected]=>
bool(true)
["extra_data":protected]=>
array(0) {
}
["default_data":protected]=>
array(48) {
["name"]=>
string(0) ""
["slug"]=>
string(0) ""
["date_created"]=>
NULL
["date_modified"]=>
NULL
["status"]=>
bool(false)
["featured"]=>
bool(false)
["catalog_visibility"]=>
string(7) "visible"
["description"]=>
string(0) ""
["short_description"]=>
string(0) ""
["sku"]=>
string(0) ""
["price"]=>
string(0) ""
["regular_price"]=>
string(0) ""
["sale_price"]=>
string(0) ""
["date_on_sale_from"]=>
NULL
["date_on_sale_to"]=>
NULL
["total_sales"]=>
string(1) "0"
["tax_status"]=>
string(7) "taxable"
["tax_class"]=>
string(0) ""
["manage_stock"]=>
bool(false)
["stock_quantity"]=>
NULL
["stock_status"]=>
string(7) "instock"
["backorders"]=>
string(2) "no"
["sold_individually"]=>
bool(false)
["weight"]=>
string(0) ""
["length"]=>
string(0) ""
["width"]=>
string(0) ""
["height"]=>
string(0) ""
["upsell_ids"]=>
array(0) {
}
["cross_sell_ids"]=>
array(0) {
}
["parent_id"]=>
int(0)
["reviews_allowed"]=>
bool(true)
["purchase_note"]=>
string(0) ""
["attributes"]=>
array(0) {
}
["default_attributes"]=>
array(0) {
}
["menu_order"]=>
int(0)
["virtual"]=>
bool(false)
["downloadable"]=>
bool(false)
["category_ids"]=>
array(0) {
}
["tag_ids"]=>
array(0) {
}
["shipping_class_id"]=>
int(0)
["downloads"]=>
array(0) {
}
["image_id"]=>
string(0) ""
["gallery_image_ids"]=>
array(0) {
}
["download_limit"]=>
int(-1)
["download_expiry"]=>
int(-1)
["rating_counts"]=>
array(0) {
}
["average_rating"]=>
int(0)
["review_count"]=>
int(0)
}
["data_store":protected]=>
object(WC_Data_Store)#8807 (4) {
["instance":"WC_Data_Store":private]=>
object(WC_Product_Data_Store_CPT)#8806 (5) {
["internal_meta_keys":protected]=>
array(39) {
[0]=>
string(11) "_visibility"
[1]=>
string(4) "_sku"
[2]=>
string(6) "_price"
[3]=>
string(14) "_regular_price"
[4]=>
string(11) "_sale_price"
[5]=>
string(22) "_sale_price_dates_from"
[6]=>
string(20) "_sale_price_dates_to"
[7]=>
string(11) "total_sales"
[8]=>
string(11) "_tax_status"
[9]=>
string(10) "_tax_class"
[10]=>
string(13) "_manage_stock"
[11]=>
string(6) "_stock"
[12]=>
string(13) "_stock_status"
[13]=>
string(11) "_backorders"
[14]=>
string(18) "_sold_individually"
[15]=>
string(7) "_weight"
[16]=>
string(7) "_length"
[17]=>
string(6) "_width"
[18]=>
string(7) "_height"
[19]=>
string(11) "_upsell_ids"
[20]=>
string(14) "_crosssell_ids"
[21]=>
string(14) "_purchase_note"
[22]=>
string(19) "_default_attributes"
[23]=>
string(19) "_product_attributes"
[24]=>
string(8) "_virtual"
[25]=>
string(13) "_downloadable"
[26]=>
string(9) "_featured"
[27]=>
string(19) "_downloadable_files"
[28]=>
string(16) "_wc_rating_count"
[29]=>
string(18) "_wc_average_rating"
[30]=>
string(16) "_wc_review_count"
[31]=>
string(22) "_variation_description"
[32]=>
string(13) "_thumbnail_id"
[33]=>
string(11) "_file_paths"
[34]=>
string(22) "_product_image_gallery"
[35]=>
string(16) "_product_version"
[36]=>
string(12) "_wp_old_slug"
[37]=>
string(10) "_edit_last"
[38]=>
string(10) "_edit_lock"
}
["extra_data_saved":protected]=>
bool(false)
["updated_props":protected]=>
array(0) {
}
["meta_type":protected]=>
string(4) "post"
["object_id_field_for_meta":protected]=>
string(0) ""
}
["stores":"WC_Data_Store":private]=>
array(18) {
["coupon"]=>
string(24) "WC_Coupon_Data_Store_CPT"
["customer"]=>
string(22) "WC_Customer_Data_Store"
["customer-download"]=>
string(31) "WC_Customer_Download_Data_Store"
["customer-session"]=>
string(30) "WC_Customer_Data_Store_Session"
["order"]=>
string(23) "WC_Order_Data_Store_CPT"
["order-refund"]=>
string(30) "WC_Order_Refund_Data_Store_CPT"
["order-item…
When I try to loop through it and store the serialized or encoded values, this is what I get:
{ ["98f13708210194c475687be6106a3b84"]=> array(10)
{ ["product_id"]=> int(20)
["variation_id"]=> int(0)
["variation"]=> array(0) { }
["quantity"]=> int(2)
["line_total"]=> float(3001.98)
["line_subtotal"]=> float(3001.98)
["line_tax"]=> int(0)
["line_subtotal_tax"]=> int(0)
["line_tax_data"]=> array(2) {
["total"]=> array(0) { }
["subtotal"]=> array(0) { } }
["data"]=> array(0) { } } }`
My questions:
What could be happening?
Why do I lose so much data?
How to get the missing data?
You are loosing the ["data"] because as you can see it's a WC_product object and you can access the data directly since woocommerce 3+.
All other data after is protected so you can't get it too.
So you need to use a foreach loop this way:
// iterating through each cart items
foreach ( WC()->cart->get_cart() as $item_key => $item_values ){
// We set the cart item data in an array
$wc_get_cart[$item_key] = $item_values;
// We remove the WC_Product object from this array
unset($wc_get_cart[$item_key]['data']);
// We set the data with WC_Data get_data() method in a variable (array)
$product_data = $item_values['data']->get_data();
// We set back this data
$wc_get_cart[$item_key]['data'] = $product_data;
}
Now you can serialize $wc_get_cart multi dimensional array and you will see that you have all the needed data
So you can use WC_Data get_data() method to convert WooCommerce objects like Orders or Products in an array and then to access properties without using the classes methods.
I have array in Angular JS:
$scope.formData.conditions
Before AJAX request I display this varaible:
[1: "1", 2: "2", 3: "3", 4: "4", 5: "5"]
But on server PHP I get array like as:
["conditions"]=>
array(6) {
[0]=>
string(0) ""
[1]=>
string(1) "1"
[2]=>
string(0) ""
[3]=>
string(1) "3"
[4]=>
string(1) "4"
[5]=>
string(1) "5"
}
Why element with index 2 is empty?
Because that's not an array, that's an object. Server-side you then convert it to an array. This conversion process is the problem. Send it as a dictionary ({1: "1", 2: "2", 3: "3", 4: "4", 5: "5"}) and PHP will know to convert it into a sparse array (which is a dictionary too).
I need to output following array
object(stdClass)#6 (14) { ["subcategories"]=>
array(9) { [0]=> object(stdClass)#31 (13) { ["id"]=> string(2) "10"
["parent_id"]=> string(1) "6" ["name"]=> string(8) "Soup" ["description"]=> string(0) "" ["url"]=> string(4) "supy" ["meta_title"]=> string(8) "Soup" ["meta_keywords"]=> string(8) "Soup" ["meta_description"]=> string(0) "" ["image"]=> string(0) "" ["visible"]=> string(1) "1" ["position"]=> string(1) "7" ["path"]=> array(2) { [0]=> RECURSION [1]=> RECURSION } ["children"]=> array(1) { [0]=> int(10) } } [1]=> object(stdClass)#32 (13) { ["id"]=> string(2) "11" ["parent_id"]=> string(1) "6" ["name"]=> string(12) "Сашими" ["description"]=> string(0) "" ["url"]=> string(7) "sashimi" ["meta_title"]=> string(12) "Сашими" ["meta_keywords"]=> string(12) "Сашими" ["children"]=> array(1) { [0]=> int(10) } } [1]=> object(stdClass)#32 (13) { ["id"]=> string(2) "11" ["parent_id"]=> string(1) "6" ["name"]=> string(12) "Сашими" ["description"]=> string(0) "" ["url"]=> string(7) "sashimi" ["meta_title"]=> string(12) "Сашими" ["meta_keywords"]=> string(12) "Сашими" ["meta_description"]=> string(0) "" ["image"]=> string(0) "" ["visible"]=> string(1) "1" ["position"]=> string(1) "8" ["path"]=> array(2) { [0]=> RECURSION [1]=> RECURSION }
i think that here is multiple foreach need for output each ["name"] from ["subcategories"]
Now i've the following code
{foreach name=cats from=$category->name item=n}
<li>
<a href="catalog/{$n->url}" category_id="{$n->id}">
<b>{$n}</b>
</a>
</li>
{/foreach}
which displays only one Name of the current category (Soup).
How can i output each ["name"] from ["subcategories"] ?
As you said, you need a foreach for every level:
{foreach name=cats from=$category->name item=n}
<li>
<a href="catalog/{$n->url}" category_id="{$n->id}">
<b>{$n}</b>
</a>
{foreach name=subcats from=$n.subcategories item=s}
....
{/foreach}
</li>
{/foreach}
or maybe is from=$n->subcategories, I'm not sure, as you put all the array in a single line and it's difficult to see it