Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I ignore certain sets from a given array using PHP

Tags:

arrays

php

Here's the problem I have, I'm trying to separate News by categories, I have the following txt file (which includes all of the news divided by

 <item></item>

Here is a set of 4 News, on my actual file I have thousands.

 <item>
 Title: News from Washington
 Author: John Doe
 Category: New Laws
 Body: News content...
 </item>

 <item>
 Title: News from Texas
 Author: General Lee
 Category: Road Accidents
 Body: News content/
 </item>

 <item>
 Title: News from Georgia
 Author: Marcus Smith
 Category: Street Food
 Body: News content
 </item>

 <item>
 Title: News from Illinois
 Author: Robert Simpson
 Category: School Projects
 Body: News content
 </item>

I have the following coding:

//I get the content from the news file:
 $news = file_get_contents("news.txt");

//Then I create the following variables to get each set of news from the news variable:
 $regexp = '@<item>(.*?)</item>@msi';

what I want to do from here is that in case that I just want to get a file with News which includes just the "Street Food" as a category and to dismiss/ignore the rest of the other News with different categories.

e.g.

My result from the above example will be a file which only includes this item:

 <item>
 Title: News from Georgia
 Author: Marcus Smith
 Category: Street Food
 Body: News content
 </item>

I tried using a preg_match_all and a foreach function to get a set of news with certain category with no luck.

What do you suggest to accomplish this? or if you could provide me with an example that'll be great.

Thanks in advance!

like image 768
Luis Franco Avatar asked Jun 26 '26 03:06

Luis Franco


1 Answers

You can try

$final = array();
$filename = "log.txt";
$news = simplexml_load_file($filename);

foreach ( $news as $item ) {
    $item = trim($item);
    $content = array();
    foreach ( explode("\n", $item) as $info ) {
        list($title, $data) = explode(":", $info);
        $content[trim($title)] = $data;
    }
    $final[trim($content['Category'])][] = $content;
}


#Remove Street Food
unset($final['Street Food']);

#Output The Rest 
var_dump($final);

Output

    array
  'New Laws' => 
    array
      0 => 
        array
          'Title' => string ' News from Washington' (length=21)
          'Author' => string ' John Doe' (length=9)
          'Category' => string ' New Laws' (length=9)
          'Body' => string ' News content...' (length=16)
  'Road Accidents' => 
    array
      0 => 
        array
          'Title' => string ' News from Texas' (length=16)
          'Author' => string ' General Lee' (length=12)
          'Category' => string ' Road Accidents' (length=15)
          'Body' => string ' News content/' (length=14)
  'School Projects' => 
    array
      0 => 
        array
          'Title' => string ' News from Illinois' (length=19)
          'Author' => string ' Robert Simpson' (length=15)
          'Category' => string ' School Projects' (length=16)
          'Body' => string ' News content' (length=13)

You can also Rewrite The XML using the following

#Rewrite the array to new XML Fromat
rewriteToXML($final,"log.xml");

This would return

<?xml version="1.0"?>
<items>
    <item>
        <Title> News from Washington</Title>
        <Author> John Doe</Author>
        <Category> New Laws</Category>
        <Body> News content...</Body>
    </item>
    <item>
        <Title> News from Texas</Title>
        <Author> General Lee</Author>
        <Category> Road Accidents</Category>
        <Body> News content/</Body>
    </item>
    <item>
        <Title> News from Illinois</Title>
        <Author> Robert Simpson</Author>
        <Category> School Projects</Category>
        <Body> News content</Body>
    </item>
</items>

Reading new format easier

$final = array();
$filename = "log.xml";
$news = simplexml_load_file($filename);

foreach ( $news as $item ) {
    #Check if not Street Food
    if(trim($item->Category) != 'Street Food')
            $final[trim($item->Category)][] = (array) $item;
}

#Output The Rest
var_dump($final);

Re Write Function

function rewriteToXML($array, $fileName = null) {
    $xml = new SimpleXMLElement("<items />");
    foreach ( $array as $key => $item ) {
        $child = $xml->addChild("item");
        foreach ( $item as $list ) {
            foreach ( $list as $title => $data ) 
            {
                $child->addChild($title, $data);
            }
        }
    }
    $xml->asXML($fileName);
}
like image 62
Baba Avatar answered Jun 27 '26 17:06

Baba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!