user1054080
user1054080

Reputation:

Compare two arrays and return value

I have two arrays:

Array
(
    [1] = 1575255
    [2] = 1575258
)

Array
(
    [Aantal kleuren opdruk] = Array
        (
            [1575252] = 1 kleur
            [1575253] = 2 kleuren
            [1575254] = 3 kleuren
            [1575255] = 4 kleuren
        )

    [Opdrukpositie] = Array
        (
            [1575256] = Borst
            [1575258] = Borst en rug
            [1575257] = Rug
        )

)

How can I compare the value of array 2 with the value of array 1 in their current order?

Upvotes: 0

Views: 65

Answers (2)

hassan
hassan

Reputation: 8308

using array_column :

$arr = [];
foreach ($arr1 as $key => $value) {
    $arr[] = array_column($arr2 ,$value);
}

print_r($arr);

assuming that the first array is $arr1 and the second is $arr2

EDIT

the one line solution using array_column and array_map

$arr = array_map(function($value) use ($arr2) {return array_column($arr2, $value);} ,$arr1);

print_r($arr);

Upvotes: 1

Kinshuk Lahiri
Kinshuk Lahiri

Reputation: 1500

OK so the first array has this. Take it as $a:

  $a = ["1575255", "1575258"];
  $second = ["Aantal kleuren opdruk" => [
        "1575252" => "1 kleur",
        "1575253" => "2 kleuren",
        "1575254" => "3 kleuren",
        "1575255" => "4 kleuren"
    ],
    "Opdrukpositie" => [
        "1575256" => "Borst",
        "1575258" => "Borst en rug",
        "1575257" => "Rug"
              ]
    ];
   foreach($second as $val){
     foreach($val as $key => $v){
      if(in_array($key, $a)){
         echo $v."<br>";
      }
    } 
   }

Upvotes: 0

Related Questions