Note that there are some explanatory texts on larger screens.

plurals
  1. POArray Looping and Unset
    text
    copied!<p>I have the following PHP array:</p> <pre><code>$data = Array ( [rules1.txt] =&gt; Array ( [rules] =&gt; Array ( [0] =&gt; throw rule blah [1] =&gt; punt rule blah [2] =&gt; #comment [3] =&gt; [4] =&gt; #punt rule that is commented out ) [item] =&gt; rules1 [itemId] =&gt; 4 [new] =&gt; true ) [rules2.txt] =&gt; Array ( ... ) ... ) </code></pre> <p>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.</p> <p>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.</p> <p>So in whatever loop(s) I end up using I need to end up with a structure like...</p> <pre><code>$data = Array ( [rules1.txt] =&gt; Array ( [rules] =&gt; Array ( [0] =&gt; throw rule blah [1] =&gt; punt rule blah ) [item] =&gt; rules1 [itemId] =&gt; 4 [new] =&gt; true ) [rules2.txt] =&gt; Array ( ... ) ... ) </code></pre> <p>In my efforts to achieve my goal I created a simple, additional array holding the keywords...</p> <pre><code>$actions = Array( 'throw', 'punt' ); </code></pre> <p>...to use in looping to create a third array that would look something like...</p> <pre><code>$removeThese = Array ( [rules1.txt] =&gt; Array ( [0] =&gt; 2 [1] =&gt; 3 [2] =&gt; 4 ) [rules2.txt] =&gt; Array ( ... ) ... ) </code></pre> <p>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.).</p> <p>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.</p> <p>Looking for suggestions to best get from point A (original $data with items to be removed) to point B (updated $date with items removed).</p> <p>Many thanks.</p> <h1>Edit</h1> <p>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'].</p> <pre><code>foreach ($data as $fileName =&gt; $fileData) { foreach ($fileData as $fdKey =&gt; $fdValue) { if ($fdKey === 'rules') { foreach ($fdValue as $key =&gt; $value) { foreach ($actions as $action) { if (strpos($value, $action) != 0) { if (!in_array($value, $removeThese[$fileName])) { $removeThese[$fileName][] = $key; } } } } } } } </code></pre> <p>So I see two issues:</p> <p>1) Where have I gone wrong causing $removeThese to be overwritten?<br> 2) What's a better way to write this?</p> <h1>Fixed</h1> <p>The final code I was looking for, which I don't think is too bad - though I have a lot to learn...</p> <pre><code>foreach ($data as $fileName =&gt; $fileData) { foreach ($fileData as $fdKey =&gt; $fdValue) { if ($fdKey === 'rules') { foreach ($fdValue as $key =&gt; $value) { $value = rtrim($value, "\n"); if (!in_array($value, $actions) === true) { unset($data[$fileName][$fdKey][$key]); } } } } } </code></pre> <p>A <em>whole lot</em> 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.</p> <p>Now how do I up-vote my own work and accept my own solution?!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload