简体   繁体   English

PHP根据另一个数组的值显示一个数组的特定键的值

[英]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个数组,如下所示:

  • the first one is created using the explode() function on a string; 第一个是使用explode()函数在字符串上创建的;
  • the second one is created using a 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: 数组长度不一样:

  • 1st has the length 6 第一个长度为6
  • 2nd has the length 9 第二个有长度9

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个数组的输出将是这样的:

  • Name: 'test' 名称:“ test”
  • Tag: '123456789' 标签:'123456789'
  • Service: 'PAID' 服务:“ PAID”
  • Type: 'SIM' 类型:“ SIM”
  • Comment: 'Needs activation' 评论:“需要激活”
  • Location: 'A city' 地点:“城市”

@ 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.

相关问题 通过比较另一个数组的键从一个数组中删除数组值 - Removing Array Values From One Array By Comparing Keys Of Another Array PHP:如何使用另一个数组中的值覆盖一个数组中的值而不向数组添加新键? - PHP: How to overwrite values in one array with values from another without adding new keys to the array? 短代码获取数组,该数组从php中的一个数组获取键并从另一个数组获取值 - A short code to get an array that gets keys from one array and values from another in php 如何将一个数组中的值作为键与另一个数组相关联? - How to associate values from one array to another array as keys? 将键从一个数组匹配到另一个数组,并使用其值创建一个新数组? - Matching keys from one array to another and creating a new array with their values? 如何在同一键处将值从一个数组追加到另一个数组? - How to append values from one array to another array at same keys? 如何从另一个数组的值构建PHP多维数组的键? - How to build the keys of a multidimensional array in PHP from the values of another array? PHP用另一个数组的键值更改数组键 - PHP change array keys with key values from another array PHP使用另一个数组中的值和键更新数组 - PHP update array with values and keys from another array PHP 将两 (2) 个值(非键)从一个数组复制到新数组 - PHP Copy two (2) Values (Not Keys) from one array into new array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM