0
votes

i am trying to get array values to string, but i fail

My array ($epg) looks like this:

Array
(
)
Array
(
    [0] => Array
        (
            [title] => VGhlIEZhbnRhc3kgRm9vdGJhbGwgQ2x1Yg==
            [lang] => en
            [start] => 1425385800
            [end] => 1425387600
            [description] => Sm9obiBGZW5kbGV5IGFuZCBQYXVsIE1lcnNvbiBwcmVzZW50IGEgZGlzY3Vzc2lvbiBvbiBrZXkgZmFudGFzeSBmb290YmFsbCBpc3N1ZXMsIGFzIHdlbGwgYXMgdGhlIHdlZWtlbmQncyBQcmVtaWVyIExlYWd1ZSBtYXRjaGVzLiBBbHNvIGZlYXR1cmluZyBndWVzdHMgZnJvbSB0aGUgd29ybGRzIG9mIHNwb3J0IGFuZCBzaG93Yml6Lg==
        )

    [1] => Array
        (
            [title] => QmFyY2xheXMgUHJlbWllciBMZWFndWUgUmV2aWV3
            [lang] => en
            [start] => 1425387600
            [end] => 1425391200
            [description] => QSBsb29rIGJhY2sgYXQgcmVjZW50IGZpeHR1cmVzIGluIHRoZSBFbmdsaXNoIFByZW1pZXIgTGVhZ3VlLCBhcyB0aGUgc2Vhc29uIGNvbnRpbnVlZCB3aXRoIG1hdGNoZXMgYWZmZWN0aW5nIGJvdGggZW5kcyBvZiB0aGUgdGFibGUu
        )

)
Array
(
)

And then i create foreach loop, and try to get values like this:

$title = $epg['title'];
$lang = $epg['lang'];
echo $lang;
echo $title;

But i get errors:

Notice: Undefined index: title in........ Notice: Undefined index: lang in.........

I am guessing that happens, because i have strange array , these empty arrays at start and end.

If so, how can i fix it?

Regards M

4
buddy you really need to go through the basicsDeepanshu Goyal
Maybe share some light how i can get the values to string?user2033139

4 Answers

1
votes

When you define the array like here

[0] => Array
    (
        [title] => VGhlIEZhbnRhc3kgRm9vdGJhbGwgQ2x1Yg==
        [lang] => en
        [start] => 1425385800
        [end] => 1425387600
        [description] => Sm9obiBGZW5k...
    )

If you don't have title, lang, etc. delcared as variables you need to have

[0] => Array
    (
        ['title'] => VGhlIEZhbnRhc3kgRm9vdGJhbGwgQ2x1Yg==
        ['lang'] => en
        ['start'] => 1425385800
        ['end'] => 1425387600
        ['description'] => Sm9obiBGZW5k...
    )
0
votes

Use this remove ur empty and then use this

print_r(array_filter($epg));

echo $title = $epg['title']
0
votes

Check if your array is not empty

if (count($epg) != 0)
0
votes

You get notices, not errors. But it is good practice to treat them as errors and removing from Your code.

These notices are about undefined indexes, so You have to check if given index is present in current element of array. You can do it like this:

$title = array_key_exists('title',$epg) ? $epg['title'] : NULL;

Manual