[英]merge two different array into one from one is dynamic & other is static
I want to merge three different arrays in a single array, from those, $array2
is dynamic and the others are static. 我想将三个不同的数组合并到一个数组中,其中,
$array2
是动态的,其他是静态的。 How would I get output from all merged arrays but with $array2
's multiple entries and other's single. 我如何从所有合并的数组中获得输出,但要使用
$array2
的多个条目和其他的单个条目。
I have 2 problems: 我有2个问题:
1) If I closed while
loop after $arrary2
(before other array), I only get $array2
's last entry...not all 1)如果在
$arrary2
(在其他数组之前)关闭while
循环,我只会得到$array2
的最后一个条目...不是全部
2) Mainly this: if I close the while
loop after $output['data'] = array($result);
2)主要是这样的:如果我在
$output['data'] = array($result);
之后关闭while
循环$output['data'] = array($result);
, then the second entry will create a new array...but I want it in a single array. ,那么第二个条目将创建一个新数组...但我希望将其放在一个数组中。 Any help would be appreciated.
任何帮助,将不胜感激。
Here is part of my code: 这是我的代码的一部分:
<?php
if(mysql_num_rows($result) == 1) {
$q2="select `image_kit_id` from `user_transaction` where `user_id`='$col'";
$r2=mysql_query($q2);
if($r2) {
$count=0;
while($ids=mysql_fetch_array($r2)) {
$image_kit_id=$ids['image_kit_id'];
//echo $image_kit_id;
$q3="select * from `images` where `image_kit_id` = '$image_kit_id'";
$r3=mysql_query($q3);
$data=array();
$i=0;
while($row1 = mysql_fetch_array($r3)) {
$arr = array(
'imageid' => $row1['image_id'],
);
$data[$i] = $arr;
$i++;
}
$array2 = array($data);
$count++;
$array1 = array('user_id'=>$col);
$array3 = array($data1);
$array4 = array($data2);
$array5 = array($data3);
$result = array_merge($array1, $array2 ,$array3, $array4 ,$array5);
$output['data'] = array($result);
}
?>
There is logic error in your code. 您的代码中存在逻辑错误。 I have corrected that according to my understanding, below.
根据我的理解,我已经在下面对此进行了更正。 Tell me its working or otherwise what you want.
告诉我它的工作原理,或者您想要什么。
$count=0; //here is no use of this, may be you need for counting
$array2=array();
while($ids=mysql_fetch_array($r2)) {
$image_kit_id=$ids['image_kit_id'];
$q3="select * from `images` where `image_kit_id` = '$image_kit_id'";
$r3=mysql_query($q3);
$data=array();
while($row1 = mysql_fetch_array($r3)) {
$arr = array('imageid' => $row1['image_id']);
$data[] = $arr;
}
$array2[] = $data;
$count++;
}
$array1 = array('user_id'=>$col);
$array3 = array($data1);
$array4 = array($data2);
$array5 = array($data3);
$result = array_merge($array1, $array2 ,$array3, $array4 ,$array5);
$output['data'] = array($result);
Also there are some logics are not clear, may be you need that type of $output
, so just mentioning my points here: 另外还有一些逻辑不清楚,可能是您需要这种类型的
$output
,所以在这里仅提及我的观点:
1) You can directly store $result
in $ouput['data']=$result;
1)您可以将
$result
直接存储在$ouput['data']=$result;
, coz $result
is an array. ,因为
$result
是一个数组。 why you are making is multidimensional by putting it in another array array($result)
? 为什么通过将其放入另一个数组
array($result)
来实现多维?
2) same logic in $array2
, you can directly store $row1['image_id']
in $data[]
, coz in $array2
you are only storing image ids, so you don't need associative array to tell that its imageid
. 2)与
$array2
相同的逻辑,您可以直接将$row1['image_id']
在$data[]
,因为在$array2
您仅存储图像ID,因此不需要关联数组来告知其imageid
。
3) same thing is for all other arrays. 3)对于其他所有数组,这也是一样。
If your output format is not specific then I would suggest: 如果您的输出格式不是特定的,那么我建议:
$array2=array();
while($ids=mysql_fetch_array($r2)) {
$image_kit_id=$ids['image_kit_id'];
$q3="select * from `images` where `image_kit_id` = '$image_kit_id'";
$r3=mysql_query($q3);
$data=array();
while($row1 = mysql_fetch_array($r3)) {
$data[] = $row1['image_id'];
}
$array2[] = $data;
}
$output['data'] = array('user_id'=>$col, 'imageids'=>$array2, $data1, $data2, $data3);
// first two element will have specified key(index), And last three's keys will be `0, 1 & 2` OR you can specify as you need OR remove the keys for first two also then keys will be `0, 1, 2, 3 & 4`.
//OR remove the ['data'] also
$ouput=array('user_id'=>$col, 'imageids'=>$array2, 'data1'=>$data1, 'data2'=>$data2, 'data3'=>$data3);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.