Jump to content

Php Arrays


stopblackholes

Recommended Posts

Ok im working on a xml parser just like this one

 

 

<?php

$xml_file = "News.xml";

$xml_headline_key = "*NEWS*STORY*HEADLINE";
$xml_description_key = "*NEWS*STORY*DESCRIPTION";

$story_array = array();

$counter = 0;
class xml_story{
    var $headline, $description;
}

function startTag($parser, $data){
    global $current_tag;
    $current_tag .= "*$data";
}

function endTag($parser, $data){
    global $current_tag;
    $tag_key = strrpos($current_tag, '*');
    $current_tag = substr($current_tag, 0, $tag_key);
}

function contents($parser, $data){
    global $current_tag, $xml_headline_key, $xml_description_key, $counter, $story_array;
    switch($current_tag){
        case $xml_headline_key:
            $story_array[$counter] = new xml_story();
            $story_array[$counter]->headline = $data;
            break;
        case $xml_description_key:
            $story_array[$counter]->description = $data;
            $counter++;
            break;
    }
}

$xml_parser = xml_parser_create();

xml_set_element_handler($xml_parser, "startTag", "endTag");

xml_set_character_data_handler($xml_parser, "contents");

$fp = fopen($xml_file, "r") or die("Could not open file");

$data = fread($fp, filesize($xml_file)) or die("Could not read file");

if(!(xml_parse($xml_parser, $data, feof($fp)))){
    die("Error on line " . xml_get_current_line_number($xml_parser));
}

xml_parser_free($xml_parser);

fclose($fp);

?>

<html>
<head>
<title>NEWS</title>
</head>
<body bgcolor="#FFFFFF">
<?php
for($x=0;$x<count($story_array);$x++){
    echo "\t<h2>" . $story_array[$x]->headline . "</h2>\n";
    echo "\t\t\n";
    echo "\t<i>" . $story_array[$x]->description . "</i>\n";
}
?>

</body>
</html>

 

 

Now what i need is a function that will find a exact value in the headline I was thinking of using in_array so if it matches then it would grab it  then somehow remove it from the array then unshift and place it at the very top of the array. 

 

What im trying to do is just find a exact value then if it exists move it to the very top of the array.

 

What would be the best way to go about doing this?

Link to comment
https://forums.phpfreaks.com/topic/57709-php-arrays/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.