Category: PHP Array

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 sum array values using php?

If you get all value from php array then you can use foreach. This is simple code which sum all value then give final result and also get average value .

<?php
// as of PHP 5.4
$ratings = [
    0 => ['rating' => 5],
    1 => ['rating' => 10],
    2 => ['rating' => 15],
    3 => ['rating' => 20],
    4 => ['rating' => 25],
];

$array_length = count($ratings); // array length
$final_result = 0;
$sum = 0; // this is store all sum value so first assign 0
foreach ($ratings as $ratingss) {
    $sum += $ratingss['rating']; // sum value with previous value and store it and no need to convert string type to int cause php do it
}
echo "Total Sum:".$sum ."<br>"; // this is final value
echo "Average:".$final_result = $sum / $array_length; // this is your desire result

?>

this is simple procedure got get all sum from php array.If you face any problem then please inform me.

Comment

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

How to textarea lines convert to array using php?

If you want to textarea lines convert to array using php then check this demo code which help you.

Important thing that your textarea must need new line. In this code i added
for new line for better understand

$text = "Name:Jay <br/>
Email:jayviru@demo.com <br/>
Contact:9876541230";
$array_data = explode('<br/>', $text);

$final_data = array();
foreach ($array_data as $data){
    $format_data = explode(':',$data);
    $final_data[trim($format_data[0])] = trim($format_data[1]);
}
echo "<pre>";
print_r($final_data);

Then output is :

Array
(
    [Name] => Jay
    [Email] => jayviru@demo.com
    [Contact] => 9876541230
)

And if your text area value get using php variable then simple use this code

<?php
$text = $_POST['filed_value'];
$array_data = explode(PHP_EOL, $text);

$final_data = array();
foreach ($array_data as $data){
    $format_data = explode(':',$data);
    $final_data[trim($format_data[0])] = trim($format_data[1]);
}
echo "<pre>";
print_r($final_data);

This is get same value, if you face any problem then please inform me. I will try to help you

Comment

php remove duplicates and sum of them from array

If you want to remove duplicates then sum of them from array then this code will help you. this is code which remove duplicate and sum of them.

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

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

Comment

check value using in_array with two dimension array php

Suppose you need to check value from two dimension array then normally in_array not work so you can check this value using this code which check value from two dimension array. this is simple code

<?php

Array ( [0] => Array ( [id] => 9545 [store] => Some Store Name )[1] => Array ( [id] => 9544 [store] => Some Store Name ); // this array 
 
 
$id = 9545; this is check value 
$store = 'Some Store Name'; this is check value 
if (in_array(array('id' => $id, 'store' => $store), $array)) {
 echo "Found!";
}
?>

then output is : Found.

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

Comment