skip to Main Content

I have this array

echo '<script type="application/ld+json">';

$data = array(
    '@context' => 'https://schema.org',
    '@graph' => array(),
);

$data['@graph'][] = [
    "@type" => "ImageObject",
   
];

$data['@graph'][] = [
    "@type" => "BreadcrumbList",
    "itemListElement" => array(),
];

print_r(json_encode($data));

echo "</script>";

Now I want to add another array "itemListElement" inside the last $data[‘@graph’][] and print but don’t know how to go about it.

am expecting

{
        "@type": "BreadcrumbList",
        "@id": "http://localhost/#breadcrumb",
        "itemListElement": [{
            "@type": "ListItem",
            "position": "1",
            "item": {
                "@id": "http://localhost",
                "name": "Home"
            }
        }, {
            "@type": "ListItem",
            "position": "1",
            "item": {
                "@id": "link 2",
                "name": "Home"
            }
        }]
    } 

2

Answers


  1. You’re referring to nested arrays.

    echo '<script type="application/ld+json">';
    
    $data['@graph'][] = [
        '@type' => 'BreadcrumbList',
        '@id' => "http://localhost/#breadcrumb",
        'itemListElement' => [
            [
                '@type' => 'ListItem',
                'position' => '1',
                'item' => [
                    '@id' => "http://localhost",
                    'name' => 'Home'
                ]
            ],
            [
                '@type' => 'ListItem',
                'position' => '2',
                'item' => [
                    '@id' => 'link 2',
                    'name' => 'Home'
                ]
            ]
        ]
    ];
    
    print_r(json_encode($data));
    
    echo "</script>";
    

    Good luck on your test

    Login or Signup to reply.
  2. <?php
      
    
    
    // PHP program to creating two 
    // dimensional associative array
    $marks = array(
      
        // Ankit will act as key
        "Ankit" => array(
              
            // Subject and marks are
            // the key value pair
            "C" => 95,
            "DCO" => 85,
            "FOL" => 74,
        ),
              
        // Ram will act as key
        "Ram" => array(
              
            // Subject and marks are
            // the key value pair
            "C" => 78,
            "DCO" => 98,
            "FOL" => 46,
        ),
          
        // Anoop will act as key
        "Anoop" => array(
              
            // Subject and marks are
            // the key value pair
            "C" => 88,
            "DCO" => 46,
            "FOL" => 99,
        ),
    );
      
    echo "Display Marks: n";
          
    print_r($marks);
    
    ?>
    
    **Output:**
    
    
    Display Marks: 
    Array
    (
        [Ankit] => Array
            (
                [C] => 95
                [DCO] => 85
                [FOL] => 74
            )
    
        [Ram] => Array
            (
                [C] => 78
                [DCO] => 98
                [FOL] => 46
            )
    
        [Anoop] => Array
            (
                [C] => 88
                [DCO] => 46
                [FOL] => 99
            )`enter code here`
    
    )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search