如何从Array PHP /过滤器值进行排序? [英] How to sort/filter values from Array PHP?

查看:157
本文介绍了如何从Array PHP /过滤器值进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的foreach这个数组的值:

我的code:

 < PHP的print_r(array_filter($比赛));?>

这code的结果:

 阵列(
   [0] =>阵列(
      [0] =>年龄://名称
      [1] => 22 - 22岁。 //值
      [2] =>种族://名称
      [3] =>高加索//值
      [4] =>地点://名称
      [5] =>伦敦,英国//值

现在我要得到这个值分离/过滤,如:

  $位置='伦敦,英国';
$年龄= '22 - 22岁'。

所以,我可以简单地做一个回声$年龄;

备注:在某些情况下,用户没有设置所有的值。因此,它不能保证该值始终年龄该数组中存在。因此,我需要的是这样的:

如果年龄:在数组中找到,那么$年龄=

我怎样才能做到这一点?我有这个数组中完全33名和33的价值观和需要过滤它们全部基于IF语句,以确保该值是正确的值。

;

结果的var_dump($比赛)的

 阵列(1){
    [0] =>阵列(66){
         [0] =>串(202)年龄:​​
         [1] =>串(157)22岁和。
         [2] =>串(196)种族
         [3] =>串(146)高加索
         [4] =>串(195)的位置:
         [5] =>串(175)英国伦敦

;

结果 var_export($比赛)的

 阵列(
     0 =>阵列(
         0 => 年龄:,
         1 => 22' - 22岁。 ',
         2 => 种族:',
         3 => 高加索,
         4 => ' 位置: ',
         5 => ' 伦敦,英国 ',

林新手,所以如果你有更好的方法来做到这一点,那么请咨询。
谢谢你的热心帮助。


解决方案

如上评论指出的那样,我会尝试修改 $匹配如果在所有可能的 - 这将是更容易与工作关联数组这种使用情况。

不过,服用上你坚持该数据结构面值,这里有一个很可能是幼稚的做法...

这假定您的数据总是在格式问世 [键,值,键,值,...]

 < PHP//原始数据
$匹配=阵列(
    阵列(
        '年龄:',
        '22 - 22岁。',
        '种族:',
        高加索,
        '位置:',
        '伦敦,英国',
        '位置:',
    )
);//块$子阵列成对匹配
//我们希望每个块包含两个元素,
//第一个将是关键,第二将是值
$块= array_chunk($比赛[0],2);//应用回调每个数组元素
$ =对array_map(函数($对){    //一些基本的错误检查...
    //如果匹配子阵数不均由于某种原因    如果(计数($对)!== 2){
        返回阵列();
    }    //在对分配给每个元素
    列表($键,$ VAL)= $对;
    //一些基本的关键正常化
    //删除非字母字符和小写
    $键=用strtolower($键);
    $键= preg_replace('/ \\ W + /','',$键);    //返回一个键值对
    返回数组($关键=> $ VAL);},$块);//遍历对
//并提取到当前的范围...
的foreach($对作为$对){
    提取物($对);
}//你现在应该能够呼应你的价值观
回声$年龄;
回声$种族;
回声$位置;

结果:

  22  -  22岁。
高加索
伦敦,英国

希望这有助于:)再次重申,我真的建议重组原始数据结构,因为上述解决方案是很难最优的。

I need to foreach values of this array:

My CODE:

<?php

print_r(array_filter($matches));

?>

Result of this CODE:

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

Now I want to get this values separated/filtered like:

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

So I can simply do a echo $age;

Remark: In some cases the user have NOT set all the values. So its not guaranteed that Value Age always exists in this array. Therefore I need something like:

If "Age:" is found in array then $age =

How can I do this? I have totally 33 names and 33 values in this array and need to filter them all based on IF statement to be sure that the value is the right value.

Result of 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 " 

Result of var_export($matches); :

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

Im newbie so if you have a better ways to do this then please advice. Thank you for your kind help.

解决方案

As noted in the comments above, I would try to modify the $matches if at all possible - it would be far easier to work with associative arrays for this use case.

However, taking it on face value that you're stuck with that data structure, here's one probably very naive approach...

this assumes that your data will always come out in the format [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;

Result:

22 Yrs.
Caucasian
London, United Kingdom

Hope this helps :) To reiterate, I would really recommend restructuring the original data structure because the above solution is hardly optimal.

这篇关于如何从Array PHP /过滤器值进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆