从键创建多维数组

时间:2014-10-11 23:19:35

标签: php arrays multidimensional-array

有没有办法创建dynamicaly多维数组?我已经存储在数据库" path"对于每个字段=>这样的值:

field_name : [slideshow][slide][0][title]
field_value : my title

field_name : [slideshow][slide][0][desc]
field_value : my desc

field_name : [slideshow][slide][1][title]
field value : my other title

field_name : [slideshow][slide][1][desc]
field value : my other desc

field_name : [slideshow][settings][duration]
field value : 300

现在我试图弄清楚如何再次成为一个阵列。显然可能有很多字段和复杂性,所以我想尽可能避免一些递归,因为我不确定它会如何影响性能。

我正在玩变量变量并尝试类似:

$array_name = 'arr';
${$array_name}[slideshow][slide][1][title] = $field->field_value;
print_r($arr);

但这只是在它的字面意思,并且没有这样的工作:

${$array_name}.$field->field_name = $field->field_value;

我基本上需要将每个字段存储为单独的行(例如,对于那些字段中的搜索),值可以是不同类型(甚至是序列化数组),并且包含html。

任何建议都表示赞赏。

2 个答案:

答案 0 :(得分:0)

基本思想是拆分field_name字符串并向后循环部分以构建数组。一些递归用于合并数组,但任何性能影响应该可以忽略不计。


实施例

// Set up sample data.
$field = new stdClass();
$field->field_name = '[slideshow][slide][0][title]';
$field->field_value = 'my title';
$fields[] = $field;

$field = new stdClass();
$field->field_name = '[slideshow][slide][0][desc]';
$field->field_value = 'my desc';
$fields[] = $field;

$field = new stdClass();
$field->field_name = '[slideshow][slide][1][title]';
$field->field_value = 'my other title';
$fields[] = $field;

$field = new stdClass();
$field->field_name = '[slideshow][slide][1][desc]';
$field->field_value = 'my other desc';
$fields[] = $field;

$field = new stdClass();
$field->field_name = '[slideshow][settings][duration]';
$field->field_value = '300';
$fields[] = $field;
// End sample data.

// array_merge_recursive() doesn't do what we want with numeric keys, so use this
function merge($base, $array) {
    foreach ($array as $key => $value) {
        if (isset($base[$key]) && is_array($base[$key]) && is_array($value)) {
            $base[$key] = merge($base[$key], $value);
        } else {
            $base[$key] = $value;
        }
    }
    return $base;
}

$result = [];
foreach ($fields as $field) {
    $parts  = array_reverse(explode('][', trim($field->field_name, '[]')));
    $value = $field->field_value;
    foreach ($parts as $part) {
        $value = [$part => $value];
    }
    $result = merge($result, $value);
}
print_r($result);

输出:

Array
(
    [slideshow] => Array
        (
            [slide] => Array
                (
                    [0] => Array
                        (
                            [title] => my title
                            [desc] => my desc
                        )

                    [1] => Array
                        (
                            [title] => my other title
                            [desc] => my other desc
                        )

                )

            [settings] => Array
                (
                    [duration] => 300
                )

        )

)

答案 1 :(得分:-1)

您可以尝试类似this的内容。

     $cars = array
      (
      array("Volvo",22,18),
      array("BMW",15,13),
      array("Saab",5,2),
      array("Land Rover",17,15)
      );

<?php
echo $cars[0][0].": In stock: ".$cars[0][1].", sold: ".$cars[0][2].".<br>";
echo $cars[1][0].": In stock: ".$cars[1][1].", sold: ".$cars[1][2].".<br>";
echo $cars[2][0].": In stock: ".$cars[2][1].", sold: ".$cars[2][2].".<br>";
echo $cars[3][0].": In stock: ".$cars[3][1].", sold: ".$cars[3][2].".<br>";
?>

<?php
for ($row = 0; $row < 4; $row++) {
  echo "<p><b>Row number $row</b></p>";
  echo "<ul>";
  for ($col = 0; $col < 3; $col++) {
    echo "<li>".$cars[$row][$col]."</li>";
  }
  echo "</ul>";
}
?>