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
CodePudding user response:
$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 usedarray_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. Thearray_reduce
function is designed to return aresult
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);
CodePudding user response:
here is my attempt
input array is $array
output array is $output
you can copy and paste this code at 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);
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'
]
];
Output
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"
}
}