Hướng dẫn array_map multidimensional php

Sometimes, for compatible in activerecord on a lot of php framework, we have an array then create a temporary array for suitable it.

So, more elegant if we don't need to make a temporary array. My favourite is array_map. What if an array like this :

Array
[
[0] => Array
    [
        [0] => 2017-05-19
        [1] => HEUNG-A_HCHIMINH_0010S
    ]

[1] => Array
    [
        [0] => 2017-05-19
        [1] => KITI_BHUM
    ]

]

Into

Array
[
   [0] => Array
       [
         [date] => 2017-04-15
         [vessel] => KMTC_HOCHIMINH
]

   [1] => Array
       [
         [date] => 2017-04-15
         [vessel] => OCL_NAGOYA
       ]
]

I need to use array_map.

Please advise.

LF00

25.4k27 gold badges140 silver badges269 bronze badges

asked May 19, 2017 at 4:21

0

array_walk[$arr, function[&$v] {
    $v = array_combine[['date', 'vessel'], $v];
}];

...or...

$arr = array_map['array_combine', array_fill[0, count[$arr], ['date', 'vessel']], $arr];

...or...

$arr = array_map[function[$a] {
    return array_combine[['date', 'vessel'], $a];
}, $arr];

answered May 19, 2017 at 4:25

EnstageEnstage

2,07612 silver badges20 bronze badges

0

Here's how you can overcome using array_map

$arr = [

  [

    '2017-05-19',
    'HEUNG-A_HCHIMINH_0010S'
],
[
    '2017-05-19',
    'KITI_BHUM'
]

];
echo '
';print_r[$arr];

$formatted = array_map[ function[$v] {
  return [
      'date' => $v[0],
      'vessel' => $v[1]
    ];
}, $arr];
echo '
';print_r[$formatted];

answered May 19, 2017 at 4:49

Basheer KharotiBasheer Kharoti

4,0925 gold badges21 silver badges48 bronze badges

You could use array_combine[] to convert an indexed array into an associative array. It merges one array for keys [date/vessel] with another for values.

array_map[] would be used to iterate over the array.

Example:

$keys_array = ['date', 'vessel'];

$new_array = array_map[ function[ $item ] {
    return array_combine[ $keys_array, $item ];
}, $array ];
  • //php.net/manual/en/function.array-combine.php
  • //php.net/manual/en/function.array-map.php

answered May 19, 2017 at 4:29

Nathan DawsonNathan Dawson

16.9k3 gold badges45 silver badges51 bronze badges

This should do it:

$newArr = array_map[function[$a]{
    return ["date" => $a[0], "vessel" => $a[1]];
}, $oldArr];

var_dump[$newArr];

answered May 19, 2017 at 4:30

CodeGodieCodeGodie

12k5 gold badges36 silver badges64 bronze badges

I know this question is old, but I'm providing the following answer in response to another question that pointed here after being marked as a duplicate. This is another alternative I don't see mentioned in the current answers.

Nội dung chính

  • 1. PHP Get Highest Value in Multidimensional Array
  • 2. PHP Find Max/Largest Value in Multidimensional Array
  • How to get highest value from multidimensional array
  • Get the highest Value form multidimensional array using foreach
  • Get the highest Value form multidimensional array using for loop

I know there's a function called max but that doesn't work with a multidimensional array like this.

You can get around that with array_column which makes getting the maximum value very easy:

$arr = [['message_id' => 1,
             'points' => 3],
        ['message_id' => 2,
             'points' => 2],
        ['message_id' => 3,
             'points' => 2]];

// max value
$max = max[array_column[$arr, 'points']];

Getting the associative key is where it gets a little more tricky, considering that you might actually want multiple keys [if $max matches more than one value]. You can do this with an anonymous function inside array_map, and use array_filter to remove the null values:

// keys of max value
$keys = array_filter[array_map[function [$arr] use [$max] {
    return $arr['points'] == $max ? $arr['message_id'] : null;
}, $arr]];

Output:

array[1] {
  [0]=>
  int[1]
}

If you do end up with multiples keys but are only interested in the first match found, then simply reference $keys[0].

PHP get or find the highest or maximum value in a multidimensional array. In this tutorial, you will learn how to get the maximum or highest value from the multidimensional array in PHP.

You should also read this array posts in PHP:

  • PHP compare two array keys and values
  • php remove element from array
  • php remove duplicates from array

This tutorial shows you two easy ways to find the highest or max value from the multidimensional array in PHP.

1. PHP Get Highest Value in Multidimensional Array

Let’s take the first example, to get the highest value in the multidimensional array using foreach loop in PHP.

The output of the above code is: 784

2. PHP Find Max/Largest Value in Multidimensional Array

Let’s take the second example, to get the highest value in the multidimensional array using for loop in PHP.

The output of the above code is: 7840

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

View all posts by Admin

How to get highest value from multidimensional array

Get the highest Value form multidimensional array using foreach

Get the highest Value form multidimensional array using for loop


Chủ Đề