I am attempting to parse an xml file based on a value from an array. If a certain value exists in the xml, several elements from the xml will be stored to a second array which eventually is saved to a file. I am having a problem in the foreach loop. Below is example of xml being parsed:
<?xml version="1.0" encoding="UTF-8"?>
<tv>
<start value="201603212000">
<programme>
<id>I396.20453.schedulesdirect.org</id> //THIS IS ABC
<title>Dancing With the Stars</title>
</programme>
<programme>
<id>I282.16331.schedulesdirect.org</id>
<title>Insane Pools: Off the Deep End</title> //THIS IS ANIMALPLT
</programme>
<start value="201603212101">
<programme>
<id>I282.16331.schedulesdirect.org</id>
<title>Treehouse Masters</title>
</programme>
<programme>
<id>I29.30438.schedulesdirect.org</id>
<title>Lucifer</title>
</programme>
Procedure to parse:
I have an array already created prior in the script called rolist that will be used as the control to check by. What is suppose to happen, a loop is created thru the xml picking out the value of "start". The "start" value is stored as an element in roguide. A foreach loop then checks the control name against a holder array to make sure it has not already been added, if it has not been added, a foreach loop starts thru the programme element of the xml. If the $program['id'] matches the $value['gid'] (from the control) the first part of the If statement is true so the info is stored to roguide array. The name is stored to the $holder array. If the id does not match the gid then that particular time slot has no program scheduled but I don't want to leave it out so I want to add it to the roguide array but with "No Program" as the title value. For example, the first entry in the xml is for "ABC" which has a program schedule, so it is written to the roguide with the title "Dancing With The Stars". The next channel in the control (rolist) is "AE", the id numbers don't match becuase there is no show in that time slot so it should go to the else part of the if statement. Same for "AMC" but then for the "ANIMALPLT" the id matches so it write program title. The problem is when I run the code below I get the return shown at bottom of post:
The control (rolist) array has a list of channel names and id numbers being used to check the xml agains. Looks like:
$items=array (
0 =>
array (
'name' => 'ABC',
'gid' => 'I396.20453.schedulesdirect.org',
),
1 =>
array (
'name' => 'AE',
'gid' => 'I265.10035.schedulesdirect.org',
),
2 =>
array (
'name' => 'AMC',
'gid' => 'I254.59337.schedulesdirect.org',
),
3 =>array (
'name' => 'ANIMALPLT',
'gid' => 'I282.16331.schedulesdirect.org',
),
Here is the code to loop thru the control and the xml:
foreach($result as $start){ //the $result is the return of json_decode of xml file
foreach($start['start'] as $xitem){ //loop thru each start element in xml
echo "START TIME. . ".$xitem['@attributes']['value']."<BR>";
$holder=array();
$roguide[]=array(
'start'=>$xitem['@attributes']['value'],
);
foreach($rolist as $key=>$value){
$check=in_multiarray(trim($value['name']),$holder,'name'); //in_multiarray is function to check for existence
if($check!=1){ //DOES NOT EXIST IN ARRAY- PUT IN ARRAY
echo "CHECK. . NOT IN ARRAY. . .ADD TO ARRAY. . .".$value['name']."<br>";
foreach($xitem['programme'] as $program){
if($program['id']==$value['gid']){ //IF ID MATCHES WRITE TO ARRAY
$roguide[]=array(
'time'=>$xitem['@attributes']['value'],
'name'=>$value['name'],
'id'=>$value['gid'],
'title'=>$program['title'],
);
$holder[]=array(
'name'=>$value['name'],
);
break;
}else{
$roguide[]=array(
'time'=>$xitem['@attributes']['value'],
'name'=>$value['name'],
'id'=>$value['gid'],
'title'=>"NO PROGRAM",
);
$holder[]=array(
'name'=>$value['name'],
);
break;
}
}
}else{
echo "EXISTS IN ARRAY. . . ".$value['name']."<br>";
}
}
}
}
Result from script, after it enters the "else" of the if statement doesn't seem to want to exit. It gets the first match correctly but not when it gets to the next match it still wants to enter the else clause, almost like it's stuck. When it hits ANIMALPLT it should not have NO PROGRAM in title but the actual title. Any ideas??
$items=array (
0 =>
array (
'start' => '201603212000',
),
1 =>
array (
'time' => '201603212000',
'name' => 'ABC',
'id' => 'I396.20453.schedulesdirect.org',
'title' => 'Dancing With the Stars',
'desc' => 'Following an opening number with all the couples and head judge Len Goodman, the contestants each perform a cha-cha, fox trot, jive, tango or quickstep.',
),
2 =>
array (
'time' => '201603212000',
'name' => 'AE',
'id' => '',
'title' => 'NO PROGRAM',
'desc' => '',
),
3 =>
array (
'time' => '201603212000',
'name' => 'AMC',
'id' => '',
'title' => 'NO PROGRAM',
'desc' => '',
),
4 =>
array (
'time' => '201603212000',
'name' => 'ANIMALPLT',
'id' => '',
'title' => 'NO PROGRAM',
'desc' => '',
),