如何将键添加到现有的多个数组

时间:2016-02-05 22:09:41

标签: php arrays

我有下面的多个数组:

array:4 [▼
  0 => array:5 [▼
    title => "some content"
    subtitle => "some content"
    somekey => "some content"
    somekey2 => "some content"
    somekey3 => "some content"
  ]
  1 => array:5 [▼
    title => "some content"
    subtitle => "some content"
    somekey => "some content"
    somekey2 => "some content"
    somekey3 => "some content"
  ]
  2 => array:6 [▼
    title => "some content"
    subtitle => "some content"
    somekey => "some content"
    somekey2 => "some content"
    somekey3 => "some content"
    somekey4 => "some content"
  ]
  3 => array:5 [▼
    title => "some content"
    subtitle => "some content"
    somekey => "some content"
    somekey2 => "some content"
    somekey3 => "some content"
  ]
]

如何向此阵列添加键,使其如下所示:

<svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" version="1.1" width="400" height="200">
    <style>
    .draggable {
        cursor: move;
    }
    </style>
    <script type="text/ecmascript">
    < ![CDATA[
        var selectedElement = 0;
        var currentX = 0;
        var currentY = 0;
        var currentMatrix = 0;

        function selectElement(evt) {
            selectedElement = evt.target;
            currentX = evt.clientX;
            currentY = evt.clientY;
            currentMatrix = selectedElement.getAttributeNS(null, "transform").slice(7, -1).split(' ');

            for (var i = 0; i < currentMatrix.length; i++) {
                currentMatrix[i] = parseFloat(currentMatrix[i]);
            }

            selectedElement.setAttributeNS(null, "onmousemove", "moveElement(evt)");
            selectedElement.setAttributeNS(null, "onmouseout", "deselectElement(evt)");
            selectedElement.setAttributeNS(null, "onmouseup", "deselectElement(evt)");
        }

        function moveElement(evt) {
            var dx = evt.clientX - currentX;
            var dy = evt.clientY - currentY;
            currentMatrix[4] += dx;
            currentMatrix[5] += dy;
            selectedElement.setAttributeNS(null, "transform", "matrix(" + currentMatrix.join(' ') + ")");
            currentX = evt.clientX;
            currentY = evt.clientY;
        }

        function deselectElement(evt) {
            if (selectedElement != 0) {
                selectedElement.removeAttributeNS(null, "onmousemove");
                selectedElement.removeAttributeNS(null, "onmouseout");
                selectedElement.removeAttributeNS(null, "onmouseup");
                selectedElement = 0;
            }
        }
    ]] >
    </script>
    <rect x="0.5" y="0.5" width="399" height="199" fill="none" stroke="black" />
    <rect class="draggable" x="30" y="30" width="80" height="80" fill="blue" transform="matrix(1 0 0 1 25 20)" onmousedown="selectElement(evt)" />
    <rect class="draggable" x="160" y="50" width="50" height="50" fill="green" transform="matrix(1 0 0 1 103 -25)" onmousedown="selectElement(evt)" />
</svg>

有没有办法没有for循环呢?

之前没有任何方法可以添加密钥,因为我不知道将创建多少个数组。

非常感谢您的关注和参与。

3 个答案:

答案 0 :(得分:1)

我建议使用array_walkarray_combine的组合来迭代数组,并将预设的键列表与每个嵌套数组的值组合在一起。

  

array_walk - 将用户提供的函数应用于数组的每个成员
  array_combine - 通过使用一个数组作为键而另一个数组作为其值

来创建数组

示例代码

$array=array(
  array('some content1','some content','some content','some content','some content'),
  array('some content2','some content','some content','some content','some content'),
  array('some content3','some content','some content','some content','some content'),
  array('some content4','some content','some content','some content','some content')
);    

$keys=array('title','subtitle','somekey','somekey2','somekey3');

function combine(&$v,$i,$keys) {
  $v=array_combine($keys,$v);
}

array_walk($array,'combine',$keys);

print_r($array);

<强>输出

Array    (
    [0] => Array
        (
            [title] => some content1
            [subtitle] => some content
            [somekey] => some content
            [somekey2] => some content
            [somekey3] => some content
        )

    [1] => Array
        (
            [title] => some content2
            [subtitle] => some content
            [somekey] => some content
            [somekey2] => some content
            [somekey3] => some content
        )

    [2] => Array
        (
            [title] => some content3
            [subtitle] => some content
            [somekey] => some content
            [somekey2] => some content
            [somekey3] => some content
        )

    [3] => Array
        (
            [title] => some content4
            [subtitle] => some content
            [somekey] => some content
            [somekey2] => some content
            [somekey3] => some content
        )   
)

这里是working example

请注意,这仅适用于嵌套数组包含相同数量的元素且键值数组包含相同数量的元素(在本例中为5)的情况。否则,您可能会收到关于&#34的PHP错误;这两个参数应该具有相同数量的元素&#34;。

如果您愿意,可以使用匿名函数在一行中实现:

array_walk($array,function(&$v,$i,$keys){$v=array_combine($keys,$v);},$keys);

答案 1 :(得分:0)

信息不足。

function convertKeys($table) {
    foreach($table as $key => $value) {
        if (0 === $key) {
            $table['title'] = $value;
            unset($table[$key]);
        } else if (1 === $key) {
            $table['subtitle'] = $value;
            unset($table[$key]);
        } else //another mapping
    }
}

foreach($table as $val) {
    //$val is arrray
    convertKeys($val);
}

答案 2 :(得分:0)

在foreach循环中使用array_combine函数。见下面的例子

$keys_array = array("title","subtitle","somekey","somekey2","somekey3");
$array=array(
  array('some content','some content','some content','some content','some content'),
  array('some content','some content','some content','some content','some content'),
  array('some content','some content','some content','some content','some content'),
  array('some content','some content','some content','some content','some content'),
  array('some content','some content','some content','some content','some content')
);
$new_array =array();
foreach($array as $key=>$value){
     $new_array[] = array_combine($keys_array, $value);
}
print_r($new_array);