[英]How to combine two arrays into a single 2D array
print_r($size);
是
Array
(
[0] => 2
)
Array
(
[0] => 2
[1] => 1
)
Array
(
[0] => 2
[1] => 1
[2] => 5
)
print_r($tag1);
是
Array
(
[0] => ew-language
)
Array
(
[0] => ew-language
[1] => global
)
Array
(
[0] => ew-language
[1] => global
[2] => phrase
)
如何将这两个 arrays 组合在一个二维数组中? 在索引0
上,我可以获得size[1]
和tag1[1]
,即2
和ew-language
。 我将这两个 arrays 组合在一起,以便稍后在我的代码中的 for 循环中,可以比较大小并可以根据大小比较条件打印标签。
预计 output
$array[$size][$tag1]=Array(
[0] =>[2] [ew-language]
[1] =>[1] [global]
[2] =>[5] [phrase]
)
对不起,如果我错了,我不知道那是否是二维数组。 当前代码
<?php
if(isset($_POST['submit'])){
unset($_POST['submit']);
function printxml ($array)
{
$xml = new DOMDocument('1.0','UTF-8');
// $xpath = new DOMXPath($xml);
$xml->formatOutput = true;
$last_index_ids = array();
$i_id=0;
$parentid = array();
$x=0;
$tag1=array();//contains all tags
$tag=array();
$t=0;
$size=array();//contains size of all arrays
foreach ($array as $key => $value)
{
$key = $key;
//contains all the values of respective ids
$attrvalue= $value;
if ($array[$key] == "" || ctype_space($array[$key]) ) {
$array[$key] = null;
}
//split unique input field names
$expkey = explode("__", $key);
$array_size=sizeof($expkey);
array_push($size, $array_size);
//echo"<pre>"; print_r($expkey); echo"</pre>";
$result = array();
foreach ($size as $i => $val) {
$result[] = array($val, $tag1[$i]);
}
echo "<pre>";
print_r($result);
if ($array_size == 1){
array_push($tag1 ,($expkey[0]));
}
else {
array_push($tag1 ,($expkey[$array_size-2]));
}
$calculated = $array_size;
$present_element = $element_tag;
if(isset($check)) {
if(($calculated < $check) || ($calculated == $check))
{
// echo $calculated.' is less than '.$check;echo "<br/>";
if ($array_size == 1){
$first_tag->appendChild($element_tag);
}
else{ //logic to be implemented
}
}
else
{
if ($array_size>1){
$previous_element->appendChild($present_element);
}
else{ echo"condition not valid"; }
// echo $calculated.' is more than '.$check;echo "<br/>";
}
}
else
{
// echo "first";
}
$check = $array_size;
$previous_element = $element_tag;
echo"<pre>"; print_r($size); echo"</pre>";
echo"<pre>"; print_r($tag1); echo"</pre>";
$xml->save("file.xml");
}
}
printxml($_POST);
}
?>
请尝试以下:
<?php
$size = Array ( 2 ,1);
$tag1 = Array ( 'ew-language','global' );
$result = array();
foreach ($size as $i => $val) {
$result[] = array($val, $tag1[$i]);
}
echo "<pre>";
print_r($result);
?>
在进一步实际编码之前,您可能需要进一步阅读有关数组的内容。 从长远来看,实现一些你甚至不熟悉的东西将花费你更多的时间,这是大多数程序员的常见陷阱。 你需要首先了解所有选项,你需要在技术上熟练,所以阅读......
资源
在完成所有阅读之后,您将决定实际执行过程中真正需要的结构。
干杯。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.