1
<?xml version="1.0" encoding="UTF-8"?>
<TVchannel>
    <month-name month="September">
        <channel-name name="IT">
            <title>Welcome to IT-TV</title>
            <image-no-1></image-no-1>
            <image-no-2></image-no-2>
            <image-no-3></image-no-3>
            <image-no-4></image-no-4>
            <image-no-5></image-no-5>
        </channel-name>
        <channel-name name="PTG">
            <title>Welcome to PTG-TV</title>
            <image-no-1></image-no-1>
            <image-no-2></image-no-2>
            <image-no-3></image-no-3>
            <image-no-4></image-no-4>
            <image-no-5></image-no-5>
        </channel-name>
        <channel-name name="HR">
            <title>Welcome to HR-TV</title>
            <image-no-1></image-no-1>
            <image-no-2></image-no-2>
            <image-no-3></image-no-3>
            <image-no-4></image-no-4>
            <image-no-5></image-no-5>
        </channel-name>
    </month-name>

    <month-name month="October">
        <channel-name name="IT">
            <title>Welcome to IT-TV</title>
            <image-no-1></image-no-1>
            <image-no-2></image-no-2>
            <image-no-3></image-no-3>
            <image-no-4></image-no-4>
            <image-no-5></image-no-5>
        </channel-name>
        <channel-name name="PTG">
            <title>Welcome to PTG-TV</title>
            <image-no-1></image-no-1>
            <image-no-2></image-no-2>
            <image-no-3></image-no-3>
            <image-no-4></image-no-4>
            <image-no-5></image-no-5>
        </channel-name>
        <channel-name name="HR">
            <title>Welcome to HR-TV</title>
            <image-no-1></image-no-1>
            <image-no-2></image-no-2>
            <image-no-3></image-no-3>
            <image-no-4></image-no-4>
            <image-no-5></image-no-5>
        </channel-name>
    </month-name>
</TVchannel>

I have the above XML data file. I am trying to echo the title in the channel-name = "HR" section. So,the echo should be ' Welcome to HR-TV.

This is my php code for doing this

<?php
    $picture_container = simplexml_load_file('data.xml');
    echo $picture_container->[month-name[0]]->[channel-name[1]]->title;
?>

However, when I run this is get this error Parse error: syntax error, unexpected '[', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in C:\wamp64\www\POC - ITTV\logic.php on line 3

Kindly help

Aakash Tiwari
  • 45
  • 1
  • 5

3 Answers3

1
echo $picture_container->{'month-name'}[0]->{'channel-name'}[1]->title->__toString();

BTW, it should be {'channel-name'}[2] for output 'Welcome to HR-TV'.

Tomasz Winter
  • 287
  • 1
  • 9
1

There are a lot of errors in this :

1. month-name or channel-name are not constants. You can't use them as contants even they were constants you can't use "-" letter when declaring a constant or variable or any method or any function. ( PHP Interpreter understands it as minus operation. )

2. If you wanna access a generated property with -! or keys like these, you should use a syntax like this :

$something->{'keyword-name'}

So you can access your Simple XML Generated Properties like :

$picture_container->{'month-name'}[0]['channel_name'][1]->title;

aprogrammer
  • 1,764
  • 1
  • 10
  • 20
-2

Simply, try to remove -> operator when using square brackets wrapping. Similar as follows:

<?php
    $picture_container = simplexml_load_file('data.xml');
    echo $picture_container["month-name"][0]["channel-name"][1]->title;
?>
Naman
  • 1,519
  • 18
  • 32