Tag: multidimensional array

how to convert multidimensional array to single array using php?

For convert multidimensional array to single array then this is php code which help you lot . just follow this code

<?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);

if you run this code then you will see this output like this :

Input 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
        )
 
)

then output is :

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

if you face any problem then inform me, i will try to help you.

Comment

Multidimensional array sorting with key value using php

If you want to sorting multidimensional array using php then check this code , hope it will help you.

 <?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);
?>

this code will give this output :

Array
(
    [0] => Array
        (
            [bill_number] => 4
            [amount] => 50
        )
 
    [1] => Array
        (
            [bill_number] => 3
            [amount] => 100
        )
 
    [2] => Array
        (
            [bill_number] => 5
            [amount] => 150
        )
 
)

View 1 Comment

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

If you want to get get sum of values in specific index of multidimensional array using php then check this code which help you.

<?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 );

if you face any problem then inform me.

Comment