Skip to content
Advertisement

How use array_map for order by word in php

I have this two-dimensional array

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"
    ),

);

And I would like this result:

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',
  ),
)

I am a beginner, so there may be things that are not done this way. Please understand 😉

$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
}

I know how to run two foreach one inside the other and look for matches, but I think an array map would be faster and cleaner, right? Maybe I’m wrong about this too.

Can you help me please

Advertisement

Answer

$data – represents the raw data.

$result – represents the final expected 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);

Addendum:

Alternatively, you could use array_reduce() as suggested by @ryan-vincent in a comment:

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);
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement