skip to Main Content

I’m stuck on php program.
I’ve 2 array (1 is associative array) that comes from 2 different sources:

echo('<pre>');
var_dump(array1)
echo('<pre>');

array(5) {
  [2375]=>
  array(4) {
    ["Val"]=>
    string(1) "x"
    ["Desc"]=>
    string(13) "Lorem ipsum 1"
    ["gr_id"]=>
    string(2) "33"
  } 
  [2377]=>
  array(4) {
    ["Val"]=>
    string(1) "y"
    ["Desc"]=>
    string(13) "Lorem ipsum 2"
    ["gr_id"]=>
    string(2) "33"
  } 
  [2379]=>
  array(4) {
    ["Val"]=>
    string(1) "z"
    ["Desc"]=>
    string(13) "Lorem ipsum 3"
    ["gr_id"]=>
    string(2) "33"
  } 
  [2381]=>
  array(4) {
    ["Val"]=>
    string(1) "a"
    ["Desc"]=>
    string(13) "Lorem ipsum 4"
    ["gr_id"]=>
    string(2) "33"
  } 
  [2381]=>
  array(4) {
    ["Val"]=>
    string(34) "b"
    ["Desc"]=>
    string(46) "Lorem ipsum 5"
    ["gr_id"]=>
    string(2) "33"
  } 
  [3300]=>
  array(4) {
    ["Val"]=>
    string(34) "c"
    ["Desc"]=>
    string(46) "Lorem ipsum 5"
    ["gr_id"]=>
    string(2) "83"
  }
  [3302]=>
  array(4) {
    ["Val"]=>
    string(34) "d"
    ["Desc"]=>
    string(46) "Lorem ipsum 5"
    ["gr_id"]=>
    string(2) "83"
  } 
var_dump(array2)
array(2) {
  [0]=>
  string(1) "x"
  [1]=>
  string(1) "a"
}

As a result I need an associative array called array3 that have all the "Val" of the array1 that have the property "gr_id" == 33, and a second property called selected if this value is in array2:

echo('<pre>');
var_dump(array3)
echo('<pre>');

array(5) {
 [0]=>
  array(4) {
    ["Val"]=>
    string(1) "x"
    ["Selected"]=>
    bool(true)
  } 
[1]=>
  array(4) {
    ["Val"]=>
    string(1) "y"
    ["Selected"]=>
    bool(false) 
  }
[2]=>
  array(4) {
    ["Val"]=>
    string(1) "z"
    ["Selected"]=>
    bool(false) 
  } 
[3]=>
  array(4) {
    ["Val"]=>
    string(1) "a"
    ["Selected"]=>
    bool(true) 
  } 
[4]=>
  array(4) {
    ["Val"]=>
    string(1) "b"
    ["Selected"]=>
    bool(false) 
  } 

I tried foreach and for loop but I cannot came up with a solution:

$array3 = array();

foreach($rray1 as $Key => $Values){
 $tempArr = array();
 if($Values['gr_id'] == $GrID){
 foreach($array2 as $ValueSelected){
  $IsSelected = in_array($ValueSelected, $compareArr);
 }
 $tempArr[$GrID] = array(
  'Descr' => $Values['Val'],
  'Selected' => $IsSelected
 );
 array_push($array3 , $tempArr);
 }
}

3

Answers


  1. I think array_map is the function you are looking for.

    function checkValue($value) {
        if($value['gr_id'] == 33) {
            $result['Val'] = $value['Val'];
            $result['Selected'] = false;
            if(in_array($value['Val'], $array2)) {
                $result['Selected'] = true;
            }
            return $result;
        }
    }
    

    And call this function as

    $array3 = array_map('checkValue', $array1);

    Login or Signup to reply.
  2. How about using array_filter and array_walk :

    <?php
    
    
    $source = [
        "2375" => [
            "val" => "x",
            "desc" => "Lor",
            "gr_id" => "33"
        ],
        "2377" => [
            "val" => "y",
            "desc" => "Lor 2",
            "gr_id" => "33"
        ],
        "2378" => [
            "val" => "z",
            "desc" => "Lor 3",
            "gr_id" => "43"
        ],
        "2379" => [
            "val" => "j",
            "desc" => "Lor 4",
            "gr_id" => "33"
        ],
        "2388" => [
            "val" => "y",
            "desc" => "Lor 5",
            "gr_id" => "33"
        ],
    ];
    
    $filter = ["x", "y"];
    
    $result = array_filter($source, function($element){
        return $element['gr_id'] == '33';
    });
    
    array_walk($result, function(&$element) use ($filter){
        
        $element['selected'] = in_array($element['val'], $filter);
        unset($element['desc']);
        unset($element['gr_id']);
        
    });
    
    
    echo '<pre>';
    var_dump($result);
    echo '</pre>';
    
    Login or Signup to reply.
  3. $array1 = [
        2375 => ["Val" => "x", "Desc" => "Lorem ipsum 1", "gr_id" => "33"],
        2377 => ["Val" => "y", "Desc" => "Lorem ipsum 2", "gr_id" => "33"],
        2379 => ["Val" => "z", "Desc" => "Lorem ipsum 3", "gr_id" => "33"],
        2381 => ["Val" => "a", "Desc" => "Lorem ipsum 4", "gr_id" => "33"],
        2382 => ["Val" => "b", "Desc" => "Lorem ipsum 5", "gr_id" => "33"],
        3300 => ["Val" => "c", "Desc" => "Lorem ipsum 5", "gr_id" => "83"],
        3302 => ["Val" => "d", "Desc" => "Lorem ipsum 5", "gr_id" => "83"],
    ];
    
    $array2 = ["x", "a"];
    
    $GrID = "33"; // Define the desired "gr_id"
    
    $array3 = [];
    
    foreach ($array1 as $Key => $Values) {
        if ($Values['gr_id'] == $GrID) {
            $IsSelected = in_array($Values['Val'], $array2);
            $tempArr = [
                "Val" => $Values['Val'],
                "Selected" => $IsSelected,
            ];
            $array3[] = $tempArr;
        }
    }
    
    var_dump($array3);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search