Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As noted in the comments above, I would try to modify the <code>$matches</code> if at all possible - it would be far easier to work with associative arrays for this use case.</p> <p>However, taking it on face value that you're stuck with that data structure, here's one probably very naive approach... </p> <p>this assumes that your data will always come out in the format <code>[key, value, key, value, ...]</code>:</p> <pre><code>&lt;?php // original data $matches = array( array( 'Age:', '22 Yrs.', 'Ethnicity:', 'Caucasian', 'Location:', 'London, United Kingdom', 'Location:', ) ); // chunk $matches sub-array into pairs // we want each chunk to contain two elements, // the first will be the key and the second will be the value $chunks = array_chunk($matches[0], 2); // apply a callback to each array element $pairs = array_map(function($pair) { // some basic error checking... // if the matches subarray count is uneven for some reason if (count($pair) !== 2) { return array(); } // assign each element in the pair list($key, $val) = $pair; // some basic key normalisation // remove non alpha chars and lowercase $key = strtolower($key); $key = preg_replace('/\W+/', '', $key); // return a key value pair return array($key =&gt; $val); }, $chunks); // iterate over the pairs // and extract into the current scope... foreach ($pairs as $pair) { extract($pair); } // and you should now be able to echo your values echo $age; echo $ethnicity; echo $location; </code></pre> <p>Result:</p> <pre><code>22 Yrs. Caucasian London, United Kingdom </code></pre> <p>Hope this helps :) To reiterate, I would really recommend restructuring the original data structure because the above solution is hardly optimal.</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