PHP display values of specific keys from one array based on values from another array

1.8k views Asked by At

For the past week, I've been working on a PHP page to display an online internal stock of devices

The issue I'm facing and where I got stuck is the following:

I have 2 arrays, as follow:

  • the first one is created using the explode() function on a string;
  • the second one is created using a foreach() on a $query->result_array() received from MySQL query interrogation

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
  • 2nd has the length 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 }

I've tried a foreach() and a for() loop, but it will only show me only 1 value ($i always is 0)

Here's the double foreach() loops I tried:

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($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:

  • Name: 'test'
  • Tag: '123456789'
  • Service: 'PAID'
  • Type: 'SIM'
  • Comment: 'Needs activation'
  • Location: 'A city'

@Don't Panic: Correct, "STATUS" was a typo! Sorry about that

2

There are 2 answers

0
Sean On

Try using isset() with the value of the 1st array as the key of the 2nd array -

foreach($1st_array as $key){
    if(isset($2nd_array[$key])){
        echo $key . ": " . $2nd_array[$key]
    }
}
0
Mubo On

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-