Note that there are some explanatory texts on larger screens.

plurals
  1. POExploding and replacing one array field
    primarykey
    data
    text
    <p>So, I have an array that, for unrelated reasons, has one imploded field in itself. Now, I'm interested in exploding the string in that field, and replacing that field with the results of the blast. I kinda-sorta have a working solution here, but it looks clunky, and I'm interested in something more efficient. Or at least aesthetically pleasing.</p> <p>Example array:</p> <pre><code>$items = array( 'name' =&gt; 'shirt', 'kind' =&gt; 'blue|long|L', 'price' =&gt; 10, 'amount' =&gt; 5); </code></pre> <p>And the goal is to replace the <code>'kind'</code> field with <code>'color', 'lenght', 'size'</code> fields.</p> <p>So far I've got:</p> <pre><code>$attribs = array('color', 'lenght', 'size'); // future indices $temp = explode("|", $items['kind']); // exploding the field foreach ($items as $key =&gt; $value) { // iterating through old array if ($key == 'kind') { foreach ($temp as $k =&gt; $v) { // iterating through exploded array $new_items[$attribs[$k]] = $v; // assigning new array, exploded values } } else $new_items[$key] = $value; // assigning new array, untouched values } </code></pre> <p>This <strong>should</strong> (I'm writing by heart, don't have the access to my own code, and I can't verify the one I just wrote... so if there's any errors, I apologize) result in a new array, that looks something like this:</p> <pre><code>$new_items = array( 'name' =&gt; 'shirt', 'color' =&gt; 'blue', 'lenght' =&gt; 'long', 'size' =&gt; 'L', 'price' =&gt; 10, 'amount' =&gt; 5); </code></pre> <p>I could, for instance, just append those values to the <code>$items</code> array and <code>unset($items['kind'])</code>, but that would throw the order out of whack, and I kinda need it for subsequent <code>foreach</code> loops.</p> <p>So, is there an easier way to do it?</p> <p>EDIT: (Reply to Visage and Ignacio - since reply messes the code up) If I call one in a foreach loop, it calls them in a specific order. If I just append, I mess with the order I need for a table display. I'd have to complicate the display code, which relies on a set way I get the initial data. Currently, I display data with (or equivalent):</p> <pre><code>foreach($new_items as $v) echo "&lt;td&gt;$v&lt;/td&gt;\n"; </code></pre> <p>If I just append, I'd have to do something like:</p> <pre><code>echo "&lt;td&gt;$new_items['name']&lt;/td&gt;\n"; foreach ($attribs as $v) echo "&lt;td&gt;$new_items[$v]&lt;/td&gt;\n"; echo "&lt;td&gt;$new_items['price']&lt;/td&gt;\n"; echo "&lt;td&gt;$new_items['amount']&lt;/td&gt;\n"; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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