Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php reduce

$numbers = [1,2,3,4,5];

function add ($array) {
    return array_reduce($array, fn($acc, $val) => $acc + $val, 0);
}

echo add($numbers);
Comment

array reduce associative array php

$assoc_arr = array_reduce($arr, function ($result, $item) {
    $result[$item['text']] = $item['id'];
    return $result;
}, array());
Comment

PHP Array Reduce

<?php

$array = [
    [
        'tag_id' => "6291",
        'az' => 5,
    ],
    [
        'tag_id' => "6291",
        'az' => 4,
    ],
    [
        'tag_id' => "6311",
        'az' => 4,
    ],
    [
        'tag_id' => "6427",
        'az' => 4,
    ]
];

$tag_id_indexes = []; // To store the index of the first tag_id found.

array_walk(
    $array,
    function ($sub_array, $index) use (&$array, &$tag_id_indexes) {
        // Store the index of the first tag_id found.
        if (!isset($tag_id_indexes[$sub_array['tag_id']])) {
            $tag_id_indexes[$sub_array['tag_id']] = $index;
        }
        else { // This tag_id already exists so we'll combine it.
            // Get the index of the previous tag_id.
            $first_tag_id_index = $tag_id_indexes[$sub_array['tag_id']];
            // Sum the az value.
            $array[$first_tag_id_index]['az'] += $sub_array['az'];
            // Remove this entry.
            unset($array[$index]);
        }
    }
);

print "The reduced array but with the original indexes:
" . var_export($array, true) . "
";

// If you want new indexes.
$array = array_values($array);

print "The reduced array with new indexes:
" . var_export($array, true) . "
";
Comment

PREVIOUS NEXT
Code Example
Php :: Class PHPUnit_Util_Log_TeamCity does not exist 
Php :: $score = $score + $bonus Score; return $score; php 
Php :: laravel csv import 
Php :: current tab active on page reload in php 
Php :: echo fread($myfile,filesize("webdictionary.txt")); 
Php :: WordPress Emojis abschalten 
Php :: multiple laravel site in one directory 
Php :: best web server for php 
Php :: $_get in php not working 
Php :: lazy loading vs eager loading laravel 
Php :: find common value from sub arrays 
Php :: php cut after first sentence 
Php :: salom 
Php :: wp wc php remove product from cart if amount is 0 
Php :: Limiter la révision des articles WordPress 
Php :: php calculate variance 
Php :: org.springframework.web.context.request.async.AsyncRequestTimeoutExceptionTimeoutDeferredResultProcessingInterceptor 
Php :: codeigniter sanitize array in php 
Php :: wp plugin handles 
Php :: Read the index and hashid of the last block in the blockchain 
Php :: wordrpess debugg is off but still showing 
Php :: docker commant 
Php :: PHP strcasecmp — Binary safe case-insensitive string comparison 
Php :: many posts in the isset 
Php :: How to Create a Transient PHP wordpress 
Php :: cf7 first_as_label 
Php :: voirs les cles etrangeres phpmyadmin 
Php :: php tasks 
Php :: Dynamic modal name appending in laravel 
Php :: PHP strspn — Finds the length of the initial segment of a string consisting entirely of characters contained within a given mask 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =