如何在 php 中使用 array_map 按单词排序

How use array_map for order by word in php

我有这个二维数组

array
(
    0 => array
    (
        "id_category" => 3
    , "name" => "mesa plegable"
    ),

    1 => array
    (
        "id_category" => 4
    , "name" => "cama plegable"
    ),

    2 => array
    (
        "id_category" => 5
    , "name" => "sillas plegables"
    ),

    3 => array
    (
        "id_category" => 6
    , "name" => "bicicleta plegable"
    ),

    4 => array
    (
        "id_category" => 7
    , "name" => "carpas plegables"
    ),

    5 => array
    (
        "id_category" => 8
    , "name" => "bicicleta estatica plegable"
    ),

    6 => array
    (
        "id_category" => 9
    , "name" => "bicicleta electrica plegable"
    ),

    7 => array
    (
        "id_category" => 10
    , "name" => "cinta de correr plegable"
    ),

    8 => array
    (
        "id_category" => 11
    , "name" => "carro compra plegable"
    ),

    9 => array
    (
        "id_category" => 12
    , "name" => "mesa plegable cocina"
    ),

    10 => array
    (
        "id_category" => 13
    , "name" => "puertas plegables"
    ),

    11 => array
    (
        "id_category" => 14
    , "name" => "tumbona plegable"
    ),

    12 => array
    (
        "id_category" => 15
    , "name" => "escalera plegable"
    ),

    13 => array
    (
        "id_category" => 16
    , "name" => "mesa plegable pared"
    ),

);

我想要这样的结果:

array (
  'mesa' => 
  array (
    0 => 'mesa plegable',
    1 => 'mesa plegable cocina',
    2 => 'mesa plegable pared',
  ),
  'cama' => 
  array (
    0 => 'cama plegable',
  ),
  'sillas' => 
  array (
    0 => 'sillas plegables',
  ),
  'bicicleta' => 
  array (
    0 => 'bicicleta plegable',
    1 => 'bicicleta estatica plegable',
    2 => 'bicicleta electrica plegable',
  ),
  'carpas' => 
  array (
    0 => 'carpas plegables',
  ),
  'cinta' => 
  array (
    0 => 'cinta de correr plegable',
  ),
  'carro' => 
  array (
    0 => 'carro compra plegable',
  ),
  'puertas' => 
  array (
    0 => 'puertas plegables',
  ),
  'tumbona' => 
  array (
    0 => 'tumbona plegable',
  ),
  'escalera' => 
  array (
    0 => 'escalera plegable',
  ),
)

我是初学者,所以可能有些地方没有这样做。请理解;)

$write_element = '';

foreach ($original_array as $element){
 $name= explode(" ",$element['name']);
 $clean_name = $nombre_cat[0];

 $group_by_name = array_map("myfunction",$original_array,$clean_name);
}

function myfunction($original_array,$clean_name)
{
 //this is what I don't know how to do

 if $clean_name exists in $original_array push in new array to return all matches
}

我知道如何 运行 两个 foreach 一个在另一个里面并寻找匹配项,但我认为数组映射会更快更干净,对吧?也许我也错了。

你能帮帮我吗

这是我的尝试

输入数组是$array

输出数组是$output

您可以将此代码复制并粘贴到 this online php code testing site

<?php
// assign input data
$array = [
    [
        'id_category' => 3,
        'name' => 'mesa plegable'
    ],
    [   'id_category' => 4,
        'name' => 'cama plegable'
    ],
    [
        'id_category' => 5,
        'name' => 'sillas plegables'
    ],
    [
        'id_category' => 6,
        'name' => 'bicicleta plegable'
    ], 
    [
        'id_category' => 7,
        'name' => 'carpas plegables'
    ],
    [
        'id_category' => 8,
        'name' => 'bicicleta estatica plegable'
    ],
    [
        'id_category' => 9,
        'name' => 'bicicleta electrica plegable'
    ],
    [
        'id_category' => 10,
        'name' => 'cinta de correr plegable'
    ],
    [
        'id_category' => 11,
        'name' => 'carro compra plegable'
    ],
    [
        'id_category' => 12,
        'name' => 'mesa plegable cocina'
    ],
    [
        'id_category' => 13,
        'name' => 'puertas plegables'
    ],
    [
        'id_category' => 14,
        'name' => 'tumbona plegable'
    ],
    [
        'id_category' => 15,
        'name' => 'escalera plegable'
    ],
    [
        'id_category' => 16,
        'name' => 'mesa plegable pared'
    ]
];

// get strings
$strings = [];
foreach( $array as $key => $value ){
    $strings[] = $value['name'];
}

// create group headings
$string_group_names = [];
foreach( $strings as $key => $value ){
    $string_group_names[] = explode(" ", $value)[0];    
}

// clean array to remove duplicates
$string_group_names = array_unique($string_group_names);

// create output array
$output = [];
foreach( $string_group_names as $key => $value ){
    $items = [];    
    foreach( $strings as $key2 => $value2 ){
        $isMatch = explode(' ', $value2)[0] == $value;
        if($isMatch){
            $items[] = $value2; 
        }
    }
    $output[$value] = $items;    
}

// do something with the output
var_dump($output);

输入数据

$array = [
    [
        'id_category' => 3,
        'name' => 'mesa plegable'
    ],
    [   'id_category' => 4,
        'name' => 'cama plegable'
    ],
    [
        'id_category' => 5,
        'name' => 'sillas plegables'
    ],
    [
        'id_category' => 6,
        'name' => 'bicicleta plegable'
    ], 
    [
        'id_category' => 7,
        'name' => 'carpas plegables'
    ],
    [
        'id_category' => 8,
        'name' => 'bicicleta estatica plegable'
    ],
    [
        'id_category' => 9,
        'name' => 'bicicleta electrica plegable'
    ],
    [
        'id_category' => 10,
        'name' => 'cinta de correr plegable'
    ],
    [
        'id_category' => 11,
        'name' => 'carro compra plegable'
    ],
    [
        'id_category' => 12,
        'name' => 'mesa plegable cocina'
    ],
    [
        'id_category' => 13,
        'name' => 'puertas plegables'
    ],
    [
        'id_category' => 14,
        'name' => 'tumbona plegable'
    ],
    [
        'id_category' => 15,
        'name' => 'escalera plegable'
    ],
    [
        'id_category' => 16,
        'name' => 'mesa plegable pared'
    ]
];

输出

array(10) {
  ["mesa"]=>
  array(3) {
    [0]=>
    string(13) "mesa plegable"
    [1]=>
    string(20) "mesa plegable cocina"
    [2]=>
    string(19) "mesa plegable pared"
  }
  ["cama"]=>
  array(1) {
    [0]=>
    string(13) "cama plegable"
  }
  ["sillas"]=>
  array(1) {
    [0]=>
    string(16) "sillas plegables"
  }
  ["bicicleta"]=>
  array(3) {
    [0]=>
    string(18) "bicicleta plegable"
    [1]=>
    string(27) "bicicleta estatica plegable"
    [2]=>
    string(28) "bicicleta electrica plegable"
  }
  ["carpas"]=>
  array(1) {
    [0]=>
    string(16) "carpas plegables"
  }
  ["cinta"]=>
  array(1) {
    [0]=>
    string(24) "cinta de correr plegable"
  }
  ["carro"]=>
  array(1) {
    [0]=>
    string(21) "carro compra plegable"
  }
  ["puertas"]=>
  array(1) {
    [0]=>
    string(17) "puertas plegables"
  }
  ["tumbona"]=>
  array(1) {
    [0]=>
    string(16) "tumbona plegable"
  }
  ["escalera"]=>
  array(1) {
    [0]=>
    string(17) "escalera plegable"
  }
}

$data - 表示原始数据。

$result——代表最终的预期output/result.

$result = [];

array_map(function ($value) use (&$result) {
    if (!array_key_exists($key = substr($value["name"], 0, strpos($value["name"], " ")), $result)) $result[$key] = [];

    $result[$key][] = $value["name"];

}, $data);

var_export($result);

附录:

或者,您可以使用 array_reduce() as suggested by @ryan-vincent in a :

Rather than use array_map, I would have used array_reduce. Why? array_map is designed to give give you back an array that has the same number of entries as the input where each entry is modified by the callback. The array_reduce function is designed to return a result that is definitely different from the input array. It can be a value or an array, which is why I would use it for your requirement.

$result = array_reduce($data, function ($result, $value) {
    if (!array_key_exists($key = substr($value["name"], 0, strpos($value["name"], " ")), $result)) $result[$key] = [];

    $result[$key][] = $value["name"];
    return $result;

}, []);

var_export($result);