Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to convert my Associate Array into array based on certain criteria:

  • It should contain multiple column (Hence, can't user array_column in this case.)
  • It should ADD duplicate date
  • It should remove particular sub-array if NULL. (array_filter)

Input :

array (
  0 => 
  array (
    'date' => "2019-03-31",
    'a' => '1',
    'b' => '1',
  ),
  1 => 
  array (
    'date' => "2019-04-02",   // If "SAME" date then ADD them
    'a' => '1',
    'b' => '1',
  ),
  2 => 
  array (
    'date' => "2019-04-02",   // If "SAME" date then ADD them
    'a' => '2',
    'b' => '1',
  ),
  3 => 
  array (
    'date' => "2019-04-10",
    'a' => '',                // If "NULL" then remove the particular sub-array
    'b' => '1',               
  ),
  4 => 
  array (
    'date' => "2019-04-18",
    'a' => '4',
    'b' => '10',
  ),
)


I've tried the following,

Although I was able to select multiple column in array but this is giving me:

  • "Duplicate" date.
  • And including sub-array which has "Null" value.
function colsFromArray(array $array, $keys)
{
    if (!is_array($keys)) $keys = [$keys];
    return array_map(function ($el) use ($keys) {
        $o = [];
        foreach($keys as $key){
            $o[$key] = isset($el[$key])?$el[$key]:false;
        }
        return $o;
    }, $array);
}


$result =  colsFromArray($arr, array("date", "a"));


Required Output :

array (
  0 => 
  array (
    'date' => "2019-03-31",
    'a' => '1',
  ),
  1 => 
  array (
    'date' => "2019-04-02",
    'a' => '3',
  ),
  2 => 
  array (
    'date' => "2019-04-18",
    'a' => '4',
  ),
)
question from:https://stackoverflow.com/questions/66063578/getting-multiple-specific-key-value-pairs-if-exist-in-associate-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
181 views
Welcome To Ask or Share your Answers For Others

1 Answer

You could try something like this:

function colsFromArray(array $array, $keys)
{
    if (!is_array($keys)) $keys = [$keys];

    $returnArray = [];

    foreach($array as $in){
        foreach($keys as $key){

            //Check if the key already has a value
            if(isset($returnArray[$in['date']][$key]) && is_numeric($returnArray[$in['date']][$key])){
                $returnArray[$in['date']][$key] += $in[$key];
            }
            //If the key is empty, continue
            elseif(empty($in[$key])){
                continue;
            }
            //If value is to be set, set date as well
            else{
                $returnArray[$in['date']]['date'] = $in['date'];
                $returnArray[$in['date']][$key] = $in[$key];
            }
        }
    }

    //Return array after resetting indexes and removing empty entries
    return array_values(array_filter($returnArray));
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...