Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9025261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:13:43+00:00 2026-06-16T06:13:43+00:00

I have the following PHP array: $data = Array ( [rules1.txt] => Array (

  • 0

I have the following PHP array:

$data = Array 
(   
    [rules1.txt] => Array 
    ( 
        [rules] => Array 
        ( 
            [0] => throw rule blah
            [1] => punt rule blah
            [2] => #comment 
            [3] =>
            [4] => #punt rule that is commented out
        ) 
        [item] => rules1 
        [itemId] => 4 
        [new] => true 
    ) 

    [rules2.txt] => Array
    (
        ...
    )

    ...
)

The part of the array I’m concerned with is $data[‘rules’] and its keys and values. This portion of the array has varying numbers of keys and, of course, the values vary as well.

What I’m trying to do is “find” the values that begin with particular keywords, such ‘throw’ or ‘punt’… the blank lines (such as $data[‘rules’][3]) and commented values (such as $data[‘rules’][2] and …[4]) need to be removed.

So in whatever loop(s) I end up using I need to end up with a structure like…

$data = Array 
(   
    [rules1.txt] => Array 
    ( 
        [rules] => Array 
        ( 
            [0] => throw rule blah
            [1] => punt rule blah
        ) 
        [item] => rules1 
        [itemId] => 4 
        [new] => true 
    ) 

    [rules2.txt] => Array
    (
        ...
    )

    ...
)

In my efforts to achieve my goal I created a simple, additional array holding the keywords…

$actions = Array(
    'throw',
    'punt'
);

…to use in looping to create a third array that would look something like…

$removeThese = Array 
(   
    [rules1.txt] => Array 
    ( 
        [0] => 2
        [1] => 3
        [2] => 4
    ) 

    [rules2.txt] => Array
    (
        ...
    )

    ...
)

The above array, $removeThese, simply holds the $data[‘rules’] indexes which need to be removed from $data[‘rules’] for each file (the rules1.txt, rules2.txt etc.).

Since I’m wet behind the ears with PHP I have failed at each approach I have tried. The correct data is not being unset() or when creating $removeThese I am erroneously overwriting my array.

Looking for suggestions to best get from point A (original $data with items to be removed) to point B (updated $date with items removed).

Many thanks.

Edit

Tried the following code which seems to be close but $removeThese is being overwritten… ends up with only the last file from $data and the last index of $data[‘rules’].

foreach ($data as $fileName => $fileData)
{
    foreach ($fileData as $fdKey => $fdValue)
    {
        if ($fdKey === 'rules')
        {
            foreach ($fdValue as $key => $value)
            {
                foreach ($actions as $action)
                {
                    if (strpos($value, $action) != 0)
                    {
                        if (!in_array($value, $removeThese[$fileName]))
                        {
                            $removeThese[$fileName][] = $key;
                        }
                    }
                }
            }
        }
    }
}

So I see two issues:

1) Where have I gone wrong causing $removeThese to be overwritten?
2) What’s a better way to write this?

Fixed

The final code I was looking for, which I don’t think is too bad – though I have a lot to learn…

foreach ($data as $fileName => $fileData)
{
    foreach ($fileData as $fdKey => $fdValue)
    {
        if ($fdKey === 'rules')
        {
            foreach ($fdValue as $key => $value)
            {
                $value = rtrim($value, "\n");

                if (!in_array($value, $actions) === true)
                {
                    unset($data[$fileName][$fdKey][$key]);
                }
            }
        }
    }
}

A whole lot of trial and error but alas it works! I’m sure many, many of you could have done this better and significantly faster; hopefully I will get up to speed.

Now how do I up-vote my own work and accept my own solution?!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T06:13:45+00:00Added an answer on June 16, 2026 at 6:13 am

    I suggest using array_filter(). First make the function that recognizes the values to ignore:

    function shouldIgnore($value) {
      return !in_array($value, array('throw', 'punt'));
    }
    

    Then filter like this:

    $data["rules1.txt"]["rules"] = array_filter($data["rules1.txt"]["rules"], "shouldIgnore");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following php code that gets a serialized array from a mysql
I have the following php array that gets all values from a form full
I have the following quite simple test PHP code that extracts the data and
I have the following serialized PHP array stored in a mySQL database: a:2:{i:2070;s:4:0.00;i:1901;s:4:1.00;} Now,
I have the following case in php $one = array('one' => 1, 2 =>
I have the following code in config: <?php return array( 'di' => array( 'instance'
I am using the MongoDB PHP Library and have the following query array Array
I have the following code: <?php function foo($bar) { global $products; //$products = array();
I have the following PHP object but I'm struggling to get the array item
I have the following code in php: <?php $con = mysql_connect(localhost,john,john); mysql_select_db(data, $con); if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.