拆分数组基于密钥的值小数组? [英] Split Array into Smaller Arrays Based on Value of Key?

查看:139
本文介绍了拆分数组基于密钥的值小数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关键字搜索MySQL的搜索结果在我的网站正在执行。它们是由会员等级(0-3)进行分类。但是,我需要不同于对方显示的行列 - 像3级变得更突出的格式比其他人。

我想了分裂行到单个阵列。因此,像ARRAY0将包含所有的具有0等级等,然后遍历这些阵列显示结果行。我只是不知道如何做到这一点 - 数组分割成更小的数组。

(仅供参考,我发现这个问题:<一href=\"http://stackoverflow.com/questions/4578303/splitting-a-large-array-into-smaller-arrays-based-on-the-key-names\">splitting大阵为更小的阵列基于密钥的名字。,但我真的不知道,如果这就是我需要的东西......也许有些澄清在于q将有助于在这里?)

例如,这里是我的数组:

 阵列(
     [等级] =&GT; 3
     [机构] =&GT;测试公司
     [imagecompany] =&GT; 1636.gif
     [公司网址] =&GT; http://www.google.com
     [手机] =&GT; 344-433-3424
     [传真] =&GT;
     [address_on_web] =&GT;物理
     [地址] =&GT; 2342测试大道
     [城市] =&GT;纽约
     [stateprov] =&GT; WA
     [邮政code =&GT; 00000
     [address_mailing] =&GT; 2342测试大道
     [city_mailing] =&GT;西雅图
     [state_mailing] =&GT; WA
     [zip_mailing] =&GT; 00000
     [说明] =&GT; 测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试
     [客户] =&GT; 1636年)


解决方案

您可以使用等级作为重点来创建多维数组是这样的:

  $ aRanks =阵列();
的foreach($ aArray为$ aEntry){
    $ aRanks [$ aEntry ['排名'] [] = $ aEntry;
}
呼应'&LT; pre&GT;';
的print_r($ aRanks);

I have mysql search results from a keyword search being performed on my site. They're sorted by membership rank (0-3). However, I need to display the ranks differently than each other - like rank 3 gets more prominent formatting than the others.

I was thinking of splitting the rows up into individual arrays. So like array0 would contain all the rows that had the rank of 0, etc. Then loop through these arrays to display the results. I just have NO idea how to do this -- split the array up into smaller arrays.

(For reference, I found this question: splitting a large array into smaller arrays based on the key names. but I wasn't really sure if that's what I needed... maybe some clarification on that q would help here?)

For example here is my array:

Array ( 
     [rank]         => 3 
     [organization] => Test Company 
     [imagecompany] => 1636.gif 
     [website]      => http://www.google.com 
     [phone]        => 344-433-3424 
     [fax]          => 
     [address_on_web] => physical   
     [address] => 2342 Test Ave 
     [city] => York 
     [stateprov] => WA 
     [postalcode] => 00000 
     [address_mailing] => 2342 Test Ave 
     [city_mailing] => Seattle 
     [state_mailing] => WA 
     [zip_mailing] => 00000 
     [description] => 'Test test Test test Test test Test test Test 

test Test test Test test Test test Test test Test test Test test Test test Test test 

Test test Test test'
     [customerid] => 1636 ) 

解决方案

You can use the rank as a key to create an multidimensional array like this:

$aRanks = array();
foreach($aArray as $aEntry) {
    $aRanks[$aEntry['rank']][] = $aEntry;
}
echo '<pre>';
print_r($aRanks);

这篇关于拆分数组基于密钥的值小数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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