我有一个这样的数组:
0 = Array('attr' => Array('class' => 'class1'), 'content'=>'class-1-content'),
1 = Array('attr' => Array(), 'content'=>'...'),
2 = Array('attr' => Array('class' => 'whatever'), 'content'=>'...'),
3 = Array('attr' => Array('class' => 'anything'), 'content'=>'...'),
4 = Array('attr' => Array(), 'content'=>'...'),
5 = Array('attr' => Array('class' => 'class2'), 'content'=>'...'),
6 = Array('attr' => Array('key' => 'more-here'), 'content'=>'...'),
7 = Array('attr' => Array('class' => 'class1'), 'content'=>'class-1-again'),
8 = Array('attr' => Array('another-attr' => ''), 'content'=>'...'),
9 = Array('attr' => Array(), 'content'=>'...'),
10 = Array('attr' => Array('class' => 'and-so-on'), 'content'=>'...'),
...
我想创建一个功能,以获取特定的属性(例如,在我的情况下,所有类别等于' class1'或' class2')并将这些属性分组直到任何事情否则"开始"。基本上是为了创造这样的结果:
0 = Array(type='class1', 'content='class-1-content', block=Array(
0 = Array('attr' => Array(), 'content'=>'...'),
1 = Array('attr' => Array('class' => 'whatever'), 'content'=>'...'),
2 = Array('attr' => Array('class' => 'anything'), 'content'=>'...'),
3 = Array('attr' => Array(), 'content'=>'...')
),
1 = Array(type='class2', 'content'=>'content-class-2', block=Array(
0 = Array('attr' => Array('key' => 'more-here'), 'content'=>'...'),
),
2 = Array(type='class1', 'content'=>'class-1-again', block=Array(
0 = Array('attr' => Array('another-attr' => ''), 'content'=>'...'),
1 = Array('attr' => Array(), 'content'=>'...'),
2 = Array('attr' => Array('class' => 'and-so-on'), 'content'=>'...')
)
有谁能帮我解决如何以最好的方式做到这一点?很高兴能够创建一个'#34; kick-in-list"比如Array(' class1',' class2',' class3')等,以便创建这些块。
非常感谢!
答案 0 :(得分:1)
这是一个convert
函数,可以按照您的指示组装数组。它将原始数组作为第一个参数,使用正则表达式将您的类与第二个参数匹配:
function convert($data, $pattern) {
$result = array();
foreach($data as $element) {
if (array_key_exists("attr", $element) &&
array_key_exists("class", $element["attr"]) &&
preg_match($pattern, $element["attr"]["class"]) ) {
$group = $element;
// set the "type" key/value
$group["type"] = $group["attr"]["class"];
// remove the "class" key
unset($group["attr"]["class"]);
// remove the "attr" key, unless not empty
if (sizeof($group["attr"]) == 0) {
unset($group["attr"]);
}
// initialise "block" for collecting the next items
$group["block"] = array();
// add the group array to the result
$result[] = $group;
} else {
if (!sizeof($result)) {
// create a "pre" group if there was not yet a group:
$result[] = array("type" => "pre", "content" => null, "block" => array());
}
// add to block
$result[sizeof($result)-1]["block"][] = $element;
}
}
return $result;
}
// Set-up test data
$data = Array(
Array('attr' => Array('class' => 'class1'), 'content'=>'class-1-content'),
Array('attr' => Array(), 'content'=>'...'),
Array('attr' => Array('class' => 'whatever'), 'content'=>'...'),
Array('attr' => Array('class' => 'anything'), 'content'=>'...'),
Array('attr' => Array(), 'content'=>'...'),
Array('attr' => Array('class' => 'class2'), 'content'=>'class-2-content'),
Array('attr' => Array('key' => 'more-here'), 'content'=>'...'),
Array('attr' => Array('class' => 'class1'), 'content'=>'class-1-again'),
Array('attr' => Array('another-attr' => ''), 'content'=>'...'),
Array('attr' => Array(), 'content'=>'...'),
Array('attr' => Array('class' => 'and-so-on'), 'content'=>'...')
);
// Call function on test data, grouping on class1 and class2
var_export (convert($data, "/^class[1-2]$/"));
上述脚本的输出是:
array (
0 => array ('content' => 'class-1-content', 'type' => 'class1', 'block' => array (
0 => array ('attr' => array (), 'content' => '...'),
1 => array ('attr' => array ('class' => 'whatever'), 'content' => '...'),
2 => array ('attr' => array ('class' => 'anything'), 'content' => '...'),
3 => array ('attr' => array (), 'content' => '...'),
),
),
1 => array ('content' => 'class-2-content', 'type' => 'class2', 'block' => array (
0 => array ('attr' => array ('key' => 'more-here'), 'content' => '...'),
),
),
2 => array ('content' => 'class-1-again', 'type' => 'class1', 'block' => array (
0 => array ('attr' => array ('another-attr' => ''), 'content' => '...'),
1 => array ('attr' => array (), 'content' => '...'),
2 => array ('attr' => array ('class' => 'and-so-on'), 'content' => '...'),
),
),
)
...这是您指定的预期输出。
有一些完整性检查,以避免在attr
密钥不仅仅包含组class
属性时删除它。这不会出现在您的测试数据中,但最好是安全的。
此外,如果数据不是以要分组的元素开始(即示例中不是class1
或class2
的情况),则在结果中放入一个虚拟的“pre”组。将这些孤立数组分组。一个例子:
$data = Array(
Array('attr' => Array(), 'content'=>'...'),
Array('attr' => Array('class' => 'whatever'), 'content'=>'...'),
Array('attr' => Array('class' => 'anything'), 'content'=>'...'),
Array('attr' => Array(), 'content'=>'...'),
Array('attr' => Array('class' => 'class1'), 'content'=>'class-1-content'),
Array('attr' => Array(), 'content'=>'...')
);
将导致此输出:
array (
0 => array ('type' => 'pre', 'content' => NULL, 'block' => array (
0 => array ('attr' => array (), 'content' => '...'),
1 => array ('attr' => array ('class' => 'whatever'), 'content' => '...'),
2 => array ('attr' => array ('class' => 'anything'), 'content' => '...'),
3 => array ('attr' => array (), 'content' => '...')
),
),
1 => array ('content' => 'class-1-content', 'type' => 'class1', 'block' => array (
0 => array ('attr' => array (), 'content' => '...'),
),
),
)
答案 1 :(得分:0)
这可以使用当前阵列上的foreach()
来完成。然后,如果设置了['attr']['class']
值,我们会使用strpos()
检查它是否以class
开头。如果strpos() === 0
我们将它添加为新的顶部数组元素,如果不是,我们使用简单的计数器var将它作为新的子数组元素添加到最后一个顶部数组元素,即。 $i
。
$arrays = Array(
Array('attr' => Array('class' => 'class1'), 'content'=>'class-1-content'),
Array('attr' => Array(), 'content'=>'...'),
Array('attr' => Array('class' => 'whatever'), 'content'=>'...'),
Array('attr' => Array('class' => 'anything'), 'content'=>'...'),
Array('attr' => Array(), 'content'=>'...'),
Array('attr' => Array('class' => 'class2'), 'content'=>'...'),
Array('attr' => Array('key' => 'more-here'), 'content'=>'...'),
Array('attr' => Array('class' => 'class1'), 'content'=>'class-1-again'),
Array('attr' => Array('another-attr' => ''), 'content'=>'...'),
Array('attr' => Array(), 'content'=>'...'),
Array('attr' => Array('class' => 'and-so-on'), 'content'=>'...')
);
$i=-1; // simple counter
$newArray=array();
foreach($arrays as $array){
if (isset($array['attr']['class']) && (0 === strpos($array['attr']['class'], 'class'))) {
$i++; // increase the counter
$newArray[]=$array; // add a new array element
}
else {
$newArray[$i]['block'][]=$array; // add a new sub array element
}
}
此处的示例 - https://3v4l.org/U1V8n