How to get sum of values in specific index of multidimensional array using php?

You can use this code.this is demo code for test.

<?php
$items = array(
 array(1 => 1, 2 => 'White Shirt', 3 => 2),
 array(1 => 2, 2 => 'Blue Shirt', 3 => 3)
);
echo "<pre>";
print_r($items);

echo array_sum(array_column($items, 3)); // output 5

//it will work for php 5.5+
 // PHP 5.5+
 echo array_sum(array_column($array, 'yourindexname')); // 

// PHP 4+
function sumArray($item) {
return $item['yourindex'];
}

echo array_sum(array_map('sumArray', $array));

// PHP 5.3+
echo array_sum(array_map(
function($item) {
    return $item['yourindex'];
}, $items)
);



$sumvalue = array();

foreach ($array as $k=>$sub_array) {
  foreach ($sub_array as $id=>$value) {
  $sumvalue [$id]+=$value;
}
}

 print_r($sumvalue );
Advertisements

Multidimensional array sorting with key value

you can use usort() built in function with your custom function like this

  <?php
$yourarray = array(
    0 => array(
        'bill_number' => 3,
        'amount' => 100
    ),
    1 => array(
        'bill_number' => 4,
        'amount' => 50
    ),
    2 => array(
        'bill_number' => 5,
        'amount' => 150
    ),
);

function sortByOrder($a, $b)
{
    return $a['amount'] - $b['amount'];
}

usort($yourarray, 'sortByOrder');
echo "<pre>";
print_r($yourarray);
?>

then output will be


Array
(
    [0] => Array
        (
            [bill_number] => 4
            [amount] => 50
        )

    [1] => Array
        (
            [bill_number] => 3
            [amount] => 100
        )

    [2] => Array
        (
            [bill_number] => 5
            [amount] => 150
        )

)

hope it will help you

php remove duplicates and sum of them from array

try this, it will check that this key already exit if exit then sum value .if array stdClass Object then

$result = array();
foreach ($data as $val) {
    if (!isset($result[$val->reservations_detail_article]))
        $result[$val->reservations_detail_article] = $val;
    else
        $result[$val->reservations_detail_article]['reservations_detail_qte'] += $val['reservations_detail_qte'];
}
$result = array_values($result); 
print_r($result);

it will remove duplicates and sum of them from array

convert multidimensional array to single array using php

check array_reduce () built in function.


<?php
$your_array = array(0 => array('payment_id' => 3160), 1 => array('action' => 'update'), 2 => array('date' => '2017-05-17 09:59:40'), 3 => array('payment_date' => '23.05.201'));
echo "<pre>";
print_r($your_array);
$convert_array = array_reduce($your_array, 'array_merge', array());
echo "<pre>";
print_r($convert_array);

then output is :

Original Array :

 Array
(
    [0] => Array
        (
            [payment_id] => 3160
        )

    [1] => Array
        (
            [action] => update
        )

    [2] => Array
        (
            [date] => 2017-05-17 09:59:40
        )

    [3] => Array
        (
            [payment_date] => 23.05.201
        )

)
Output :

 Array
(
    [payment_id] => 3160
    [action] => update
    [date] => 2017-05-17 09:59:40
    [payment_date] => 23.05.201
)

How can I extract a file zip into a directory created before by code (PHP)?

this is your path problem so update your path then it will work perfectly and this is demo code which help you to upload zip file and extract this zip in same file with zip folder also.

<?php
function rmdir_recursive($dir) {
    foreach(scandir($dir) as $file) {
        if ('.' === $file || '..' === $file) continue;
        if (is_dir("$dir/$file")) rmdir_recursive("$dir/$file");
        else unlink("$dir/$file");
    }

    rmdir($dir);
}

if(!empty($_FILES["file_name"]["name"])) {
    $filename = $_FILES["file_name"]["name"];
    $source = $_FILES["file_name"]["tmp_name"];
    $type = $_FILES["file_name"]["type"];

    $name = explode(".", $filename);
    $accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
    foreach($accepted_types as $mime_type) {
        if($mime_type == $type) {
            $okay = true;
            break;
        }
    }

    $continue = strtolower($name[1]) == 'zip' ? true : false;
    if(!$continue) {
        $message = "The file you are trying to upload is not a .zip file. Please try again.";
    }

    $path = dirname(__FILE__).'/';  
    $filenoext = basename ($filename, '.zip');
    $filenoext = basename ($filenoext, '.ZIP');

    $targetdir = $path . $filenoext; 
    $targetzip = $path . $filename; 
    if (is_dir($targetdir))  rmdir_recursive ( $targetdir);

    mkdir($targetdir, 0777);

    if(move_uploaded_file($source, $targetzip)) {
        $zip = new ZipArchive();
        $x = $zip->open($targetzip);
        if ($x === true) {
            $zip->extractTo($targetdir.'/');
            $zip->close();

            unlink($targetzip);
        }
        $message = "Your file is uploaded and unpacked.";
    } else {
        $message = "There is a problem to upload.";
    }
}

?>

<?php if(isset($message)) echo "<p>$message</p>"; ?>
<form enctype="multipart/form-data" method="post" action="">
    <label>Upload a zip file to upload: <input type="file" name="file_name" /></label>
    <br />
    <input type="submit" name="submit" value="Upload" />
</form>

hope it will help you

Check if Two Videos are the Same using php

this php code check two video same or not
<?php
define('READ_LEN', 4096);

if(files_identical('demo.mp4', 'demo1.mp4'))
echo 'files identical';
else
echo 'files not identical';

// pass two file names
// returns TRUE if files are the same, FALSE otherwise
function files_identical($fn1, $fn2) {
if(filetype($fn1) !== filetype($fn2))
return FALSE;

if(filesize($fn1) !== filesize($fn2))
return FALSE;

if(!$fp1 = fopen($fn1, 'rb'))
return FALSE;

if(!$fp2 = fopen($fn2, 'rb')) {
fclose($fp1);
return FALSE;
}

$same = TRUE;
while (!feof($fp1) and !feof($fp2))
if(fread($fp1, READ_LEN) !== fread($fp2, READ_LEN)) {
$same = FALSE;
break;
}

if(feof($fp1) !== feof($fp2))
$same = FALSE;

fclose($fp1);
fclose($fp2);

return $same;
}
?>

hope it will help you

in_array with two dimension array

php in_array with two dimension array .

This is simple code that you can use it


Array ( [0] => Array ( [id] => 9545 [store] => Some Store Name )[1] => Array ( [id] => 9544 [store] => Some Store Name );


$id = 9545;
$store = 'Some Store Name';
if (in_array(array('id' => $id, 'store' => $store), $array)) {
 print "Found!";
}

 

hope it works. if you find any problem then give a comment.

Resource

Web Development Resource

UI and UX Design

User Interface and User Experience

php tutorial

code,Tips,Tricks in php,mysql,javascript,jquery,ajax,yii,codeigniter,laravel,drupal,slim,bootstrap,api,jQuery Mobile