[英]PHP display values of specific keys from one array based on values from another array
For the past week, I've been working on a PHP page to display an online internal stock of devices 在过去的一周中,我一直在PHP页面上显示在线内部设备库存。
The issue I'm facing and where I got stuck is the following: 我面临的问题以及卡住的地方如下:
I have 2 arrays, as follow: 我有2个数组,如下所示:
explode()
function on a string; 第一个是使用explode()
函数在字符串上创建的; foreach()
on a $query->result_array()
received from MySQL query interrogation 第二个是使用从MySQL查询询问接收到的$query->result_array()
上的foreach()
创建的 When I output both arrays, the look like this: 当我输出两个数组时,如下所示:
// 1st array
Array (
[0] => NAME
[1] => TAG
[2] => SERVICE
[3] => TYPE
[4] => COMMENT
[5] => LOCATION
)
// 2nd array
Array (
[ID] => 3
[ID_CAT] => 10
[NAME] => test
[TAG] => 123456789
[SERVICE] => PAID
[TYPE] => SIM
[COMMENT] => Needs activation
[LOCATION] => A city
[STATUS] => Available
)
The arrays are not the same length: 数组长度不一样:
My question is: How can I show the values of 2nd array, based on the match made on the 1st array ? 我的问题是: 如何根据第一个数组的匹配结果显示第二个数组的值?
Actually the match is something like this: if (1st array value == 2nd array key) { output }
实际上,匹配是这样的: if (1st array value == 2nd array key) { output }
I've tried a foreach()
and a for()
loop, but it will only show me only 1 value ( $i
always is 0) 我已经尝试过foreach()
和for()
循环,但是只会显示1个值( $i
始终为0)
Here's the double foreach()
loops I tried: 这是我尝试过的双重foreach()
循环:
foreach($1st_array as $key1st => $value1st) {
foreach($2nd_array as $values2nd) {
if (strcmp($key1st , $values2nd) == 0) { print '<td>'.$value1st.'</td>'; }
}
}
... and here is the foreach()
and for()
loops I've tried: ...这是我尝试过的foreach()
和for()
循环:
foreach($1st_array as $key1st => $value1st) {
for($i = 0; $i < count($2nd_array); $i++) {
if ($key1st == $2nd_array[$i]) {
print '<td id="'.$i.'">'.$value1st.'</td>';
}
}
}
Any ideas on how I can make this work ? 关于如何进行这项工作的任何想法?
This is giving me a headache :( 这让我头疼:(
All answers will be deeply appreciated! 所有答案将不胜感激!
My best regards, Michael 最好的问候,迈克尔
LATER EDIT: 之后编辑:
@ Nevermind : The output I want from the 2 arrays would be like this: @ Nevermind :我想要的2个数组的输出将是这样的:
@ Don't Panic : Correct, "STATUS" was a typo! // @ 不要惊慌 :正确,“状态”是一个错字! Sorry about that 对于那个很抱歉
Try using isset()
with the value of the 1st array as the key of the 2nd array - 尝试将isset()
与第一个数组的值用作第二个数组的键-
foreach($1st_array as $key){
if(isset($2nd_array[$key])){
echo $key . ": " . $2nd_array[$key]
}
}
According to your description, you would like to merge the two arrays, mapping the values of the first array to the keys of the second. 根据您的描述,您想合并两个数组,将第一个数组的值映射到第二个数组的键。
Try the following. 请尝试以下方法。
<?php
$array1 = Array (
'NAME',
'TAG',
'SERVICE',
'TYPE',
'COMMENT',
'LOCATION'
);
$array2 = $favorite_foods = Array (
'ID'=> 3,
'ID_CAT' => 10,
'NAME' => 'test',
'TAG'=> 123456789,
'SERVICE' => 'PAID',
'TYPE' => 'SIM',
'COMMENT' => 'Needs activation',
'LOCATION' => 'A city',
'STATUS' => 'Available'
);
foreach ($array2 as $key => $value):
//search the value in array, which is the
//key of the array2.
$position = array_search($key, $array1);
if ($position !== false):
echo " $key<br/>";
else:
echo "-NO MATCH-<br/>";
endif;
endforeach;
--Output--
-NO MATCH-
-NO MATCH-
NAME
TAG
SERVICE
TYPE
COMMENT
LOCATION
-NO MATCH-
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.