按列值组织关联数组行
使用由子数组组成的关联数组时,通常需要根据列值重新组织数据关于特定列值。这样可以更轻松地检索和分析数据。
假设我们有一个以下格式的子数组数组:
[ 'a' => ['id' => 20, 'name' => 'chimpanzee'], 'b' => ['id' => 40, 'name' => 'meeting'], 'c' => ['id' => 20, 'name' => 'dynasty'], 'd' => ['id' => 50, 'name' => 'chocolate'], 'e' => ['id' => 10, 'name' => 'bananas'], 'f' => ['id' => 50, 'name' => 'fantasy'], 'g' => ['id' => 50, 'name' => 'football'] ]
我们的目标是根据每个子数组中都存在 'id' 字段。
要实现此目的,我们可以按照以下步骤操作:
$arr = array();
foreach ($old_arr as $key => $item) { $arr[$item['id']][$key] = $item; }
ksort($arr, SORT_NUMERIC);
array ( 10 => array ( e => array ( id = 10, name = bananas ) ) 20 => array ( a => array ( id = 20, name = chimpanzee ) c => array ( id = 20, name = dynasty ) ) 40 => array ( b => array ( id = 40, name = meeting ) ) 50 => array ( d => array ( id = 50, name = chocolate ) f => array ( id = 50, name = fantasy ) g => array ( id = 50, name = football ) ) )
以上是如何根据特定列值组织关联数组行?的详细内容。更多信息请关注PHP中文网其他相关文章!