-1

我需要 foreach 这个数组的值:

我的代码:

<?php

print_r(array_filter($matches));

?>

此代码的结果:

Array ( 
   [0] => Array ( 
      [0] => Age:                     // Name
      [1] => 22 Yrs.                  // Value
      [2] => Ethnicity:               // Name
      [3] => Caucasian                // Value
      [4] => Location:                // Name
      [5] => London, United Kingdom   // Value

现在我想像这样分离/过滤这些值:

$location = 'London, United Kingdom';
$age = '22 Yrs.';

所以我可以简单地做一个echo $age;

备注:在某些情况下,用户没有设置所有值。所以不能保证 Value Age 总是存在于这个数组中。因此我需要类似的东西:

如果在数组中找到“年龄:”,则 $age =

我怎样才能做到这一点?我在这个数组中总共有 33 个名称和 33 个值,需要根据 IF 语句对它们进行过滤,以确保该值是正确的值。

结果var_dump($matches);:_

array(1) { 
    [0]=> array(66) { 
         [0]=> string(202) " Age: " 
         [1]=> string(157) " 22 Yrs. " 
         [2]=> string(196) " Ethnicity: " 
         [3]=> string(146) " Caucasian " 
         [4]=> string(195) " Location: " 
         [5]=> string(175) " London, United Kingdom " 

结果var_export($matches);:_

array ( 
     0 => array ( 
         0 => ' Age: ', 
         1 => ' 22 Yrs. ', 
         2 => ' Ethnicity: ', 
         3 => ' Caucasian ',
         4 => ' Location: ', 
         5 => ' London, United Kingdom ',

我是新手,所以如果您有更好的方法来做到这一点,请提出建议。谢谢你的热心帮助。

4

3 回答 3

1

正如上面评论中所指出的,如果可能的话,我会尝试修改$matches- 在这个用例中使用关联数组会容易得多。

然而,从表面上看,你坚持使用该数据结构,这可能是一种非常幼稚的方法......

这假设您的数据将始终以以下格式出现[key, value, key, value, ...]

<?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 => $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;

结果:

22 Yrs.
Caucasian
London, United Kingdom

希望这会有所帮助:) 重申一下,我真的建议重组原始数据结构,因为上述解决方案几乎不是最优的。

于 2013-11-07T00:01:05.923 回答
0

This answer assumes you're working with the array that directly contains the data, unlike your output example that shows 3 nested arrays.

for($i=0; $i<(count($matches)/2); $i++){
 $label = substr($matches[$i*2],0,-1);
 $output[$label] = $matches[$i*2+1];
}

This will make $output an associative array, addressable like this:

echo($output["Age"]);

Unfortunately, it's impossible as far as I know to make it into individual variables.

于 2013-11-06T23:24:47.983 回答
0

尝试单独使用数组可能更容易,例如。

$Age = Array ( 
    [0] => 22,
    [1] => 33
);

$Ethnicity = Array ( 
    [0] => "Caucasian",
    [1] => "Other"
);

$Location= Array ( 
    [0] => "London, United Kingdom",
    [1] => "Germany"
);
于 2013-11-06T23:42:25.993 回答