Smarty loop php multi-dimensional array

Hi,

I want to loop the PHP multi-dimensional array in smarty, I tried the different solutions but nothing worked.

Any idea?

My array

Array
    (
        [0] => Array
            (
                [0] => Position1
                [1] => NAME1
                [2] => IMAGE1
                [3] => URL1
            )
    
        [1] => Array
            (
                [0] => Position2
                [1] => NAME2
                [2] => IMAGE2
                [3] => URL2
            )
    
        [2] => Array
            (
                [0] => Position3
                [1] => NAME3
                [2] => IMAGE3
                [3] => URL3
            )
    
    )

Expected Output

<ul>
        <li>Position1</li>
        <li>NAME1</li>
        <li>IMAGE1</li>
        <li>URL1</li>
    <ul>
    
    <ul >
        <li>Position2</li>
        <li>NAME2</li>
        <li>IMAGE2</li>
        <li>URL2</li>
    <ul>
    
    <ul >
        <li>Position3</li>
        <li>NAME3</li>
        <li>IMAGE3</li>
        <li>URL3</li>
    <ul>
    
    

// Below code show same item 4 times instead 1 item in array. Any idea how to show list of items.

{foreach from=$list_array key=parent_key item=parent_item}
        {foreach from=$parent_item key=child_key item=child_item}
            <ul>
            <li>{$child_item}</li>
            <li>{$child_item}</li>
            <li>{$child_item}</li>
            <li>{$child_item}</li>
            <ul>
        {/foreach}
    {/foreach}

Should work -

{foreach $list_array as $parent_item}
  <ul>
  {foreach $parent_item as $child_item}
    <li>{$child_item}</li>
  {/foreach}
  </ul>
{/foreach}

Thanks for support.

It is showing only first item and not all all values in UI and LI Integration.

I checked the solution in below snippet and it is showing 1 values 4 times

<script type="application/ld+json">
  {
    "@context": "https://schema.org",
    "@type": "ItemList",
    "itemListElement": [
    {foreach $list_array as $parent_item}
		{foreach $parent_item as $child_item}
	{
        "@type": "ListItem",
        "position": {$child_item},
        "name": "{$child_item|escape:'htmlall':'UTF-8'}",
		"image": "{$child_item|escape:'htmlall':'UTF-8'}",
        "url": "{$child_item|escape:'htmlall':'UTF-8'}"
    }
		{/foreach}
	{/foreach}
    ]
  }
</script>

It shows below output with wrong output as below


<script type="application/ld+json">
  {
    "@context": "https://schema.org",
    "@type": "ItemList",
    "itemListElement": [
    			{
        "@type": "ListItem",
        "position": 1,
        "name": "1",
		    "image": "1",
        "url": "1"
    }
			{
        "@type": "ListItem",
        "position": name,
        "name": "name",
		    "image": "name",
        "url": "name"
    }
			{
        "@type": "ListItem",
        "position": URL,
        "name": "URL",
		    "image": "URL",
        "url": "URL"
    }
			{
        "@type": "ListItem",
        "position": image,
        "name": "image",
		    "image": "image",
        "url": "image"
    }
			    ]
  }
</script>

In your original post you were creating an HTML list.
But now you are outputting JSON data, for that use json_encode()

I was working to loop an array in the HTML list and schema list items. I shared the HTML list because I thought it will be easy to implement as compared to schema list items.

I want to loop array and create dynamic items list in schema

The json_encode() function will take an array, an object, or array of objects and turn them into JSON data without any need for loops. So long as the array/object is formatted correctly, it is very easy to get JSON data this way.

Thanks. Key value array work,now working to create key value array dynamically.

https://www.w3schools.com/Php/func_json_encode.asp

The array loop work is done, I am facing other issue which gives invalid URL

These values in the URL making it invalid "\/\/" and "\/"

http:\/\/localhost:89\/apps\/ps1624\/en\/summer-dresses\/5-printed-summer-dress

I’m not familiar with Smarty, but I assume this is coming from the escaping.

Thanks for your support. The problem is solved. The localhost website was giving an error but the live website does not give an error.