Trying to access the value from a nested array - arrays

I'm currently working with woocommerce using timber plugin that provides a twig template engine. Right now I'm trying to access a value that is within a nested array ["sizes"]. How do I fetch the value 400ml from inside the "[sizes]" array.
["_product_attributes"]=>
array(2) {
["sizes"]=>
array(6) {
["name"]=>
string(5) "Sizes"
["value"]=>
string(5) "400ml"
["position"]=>
string(1) "0"
["is_visible"]=>
string(1) "1"
["is_variation"]=>
string(1) "0"
["is_taxonomy"]=>
string(1) "0"
}
["color"]=>
array(6) {
["name"]=>
string(5) "Color"
["value"]=>
string(3) "Red"
["position"]=>
string(1) "1"
["is_visible"]=>
string(1) "1"
["is_variation"]=>
string(1) "0"
["is_taxonomy"]=>
string(1) "0"
}
}
I tried the snippet below but it only returns empty
{% for attr in post._product_attributes.sizes.value %}
{{ attr }}
{% endfor %}
The snippet below returns NULL
{% for attr in post._product_attributes.sizes %}
<pre>
{{ dump(attr.value) }}
</pre>
{% endfor %}

Related

Access a value in an array within a private object in Twig

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 %}

Loop twig object

i have an object like this:
> object(stdClass)#526 (2) { ["0"]=> object(stdClass)#525 (2) {
["title"]=> string(8) "title 1"
["text"]=> string(11) "paragraph 1"
}
["1"]=> object(stdClass)#527 (2) {
["title"]=> string(8) "title 2"
["text"]=> string(11) "paragraph 2"
}
}
how can i loop it in twig?
if i do:
{% for paragraph in data.data.extraDescription.0.contents.paragraphs %}
{{ dump(paragraph.title) }}
{% endfor %}
it doens't work

Angular Error: [ngRepeat:dupes]

I get the following error Error: [ngRepeat:dupes].
<tbody>
<tr data-ng-repeat="row in data">
<td>{{row.id}}</td>
<td>{{row.string}}</td>
</tr>
</tbody>
After looking through this site I saw a solution, use track by index: track by $index
The error is now gone but I don't get any data displayed.
As you can see below I tried a few ways to output data but this does not work.
Can anyone see my error?
<tbody>
<tr data-ng-repeat="row in data track by $index">
<td>{{row.id}}{{$index.id}}{{$index.row.id}}{{$index[id]}}{{$index[row.id]}}</td>
<td>{{row.string}}</td>
</tr>
</tbody>
This is my function:
$scope.displayString = function(){
$http.get('/angular/all')
.success(function(data){
$scope.data = data
})
}
And the data coming out of /angular/all
array(4) { [0]=> array(2) { ["id"]=> string(1) "1" ["string"]=> string(5) "vdfdf" } [1]=> array(2) { ["id"]=> string(1) "2" ["string"]=> string(4) "dfgh" } [2]=> array(2) { ["id"]=> string(1) "3" ["string"]=> string(4) "dfgh" } [3]=> array(2) { ["id"]=> string(1) "4" ["string"]=> string(3) "963" } }

Why I get empty POST in server from Angular JS?

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).

1+ array output in Smarty

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

Resources