I have the following multidimensional array.
"PM" => array:6 [▼
0 => "Zeb"
1 => "Pen"
2 => "Zeb"
3 => "Eds"
4 => "Fsa"
5 => "Zeb"
]
"OS" => array:3 [▼
0 => "Min"
1 => "Kep"
2 => "Min"
]
"IT" => array:8 [▶]
]
And I would like to remove the duplicate values from the nested arrays. In this case, have only one value Zeb in PM and Min in OS. Please keep in mind that, I don't know in which array there are duplicates so I need a way to check all the nested arrays for duplicates.
Thank you.
$arr = [
"PM" => ["Zeb", "Pen", "Zeb", "Eds", "Fsa", "Zeb"],
"OS" => ["Min", "Kep", "Min"],
];
$cleanArray = collect($arr)->map(fn($subarr) => array_unique($subarr))->toArray();
will produce:
[
"PM" => ["Zeb", "Pen", "Eds", "Fsa"],
"OS" => ["Min", "Kep"]
]
Related
i have value array like this
^ array:2 [▼
0 => "random value1<tr>random string1</tr>random endvalue1"
1 => "random value2<tr>random string2</tr>random endvalue2"
)
]
how do i change the value of the dimensional array above to be like below
^ array:2 [▼
0 => "random string1"
1 => "random string2"
)
]
im trying using
$array = array_map(function($key) {
$exp = explode('<tr>', $tes);
//$exp1 = explode('</tr>', exp[1]); -from this line3 my explode cant access array
return exp;
}, $myarray);
dd($array);
my array in line3 become like this
^ array:2 [▼
0 => array:2 [▼
0 => "random value1"
1 => "random string1</tr>random endvalue1"
]
1 => array:2 [▼
0 => "random value2"
1 => "random string2</tr>random endvalue2"
]
)
]
how to improve conditions like this, or have another way that is better ?
$data = [
"random value1<tr>random string1</tr>random endvalue1",
"random value2<tr>random string2</tr>random endvalue2",
];
$trim = collect($data)
->map(function (string $value) {
preg_match('/(?<=<tr>)(.*)(?=<\/tr>)/', $value, $match);
return empty($match) ? $value : $match[1];
})
->toArray();
Could be done this way, using the framework collect method to map through the values & extract the string you want.
I have this collection
1 => {#27
+"id": 1
+"name": "Zel"
+"age": "43"
}
2 => {#28
+"id": 2
+"name": "Kol"
+"age": "56"
}
3 => {#29
+"id": 3
+"name": "Mil"
+"age": "32"
}
and I would like to return an array with the key values as a string like this:
[
'id',
'name',
'age',
]
Can someone help me with that, please?
use array_keys :
$keys = array_keys($collection->first());
Laravel collection has a keys() method you could simply use it like this:
$keys = $collection->keys();
$get = $keys->all();
It is all clearly written in the Laravel Documentation
EDIT
After looking at your edit, my first consideration would be that if your collection is consistent you could get the first one and subsequently get the keys from there on:
$keys = $collection->first();
$get = $keys->keys()->all();
Or simply put $collection->first()->keys()->all();
EDIT
Here is how i was able to reproduce your problem:
$collection = collect([
[
'id' => 1,
'name' => 'Zel',
'age' => 43
],
[
'id' => 2,
'name' => 'Kol',
'age' => 56
],
[
'id' => 3,
'name' => 'Mil',
'age' => 32
],
]);
$keys = collect($collection->first())->keys()->all();
Here is the Result I got:
array:3 [▼
0 => "id"
1 => "name"
2 => "age"
]
If it still returns a collection or an object based on your last comment, you could try any one of these:
$keys = $keys->toArray();
$keys = collect($keys)->toArray();
I have html-form that contains arrays.
It has such structure:
"q" => array:1 [▼
"new" => array:1 [▼
1 => array:2 [▼
"name" => "a"
"v" => array:2 [▼
1 => array:1 [▼
"text" => "b"
]
2 => array:1 [▼
"text" => "c"
]
]
]
]
]
I've created form request. And now it has one rule:
'q.new.*.v.*.text' => 'required|min:2|max:128'
And do you know what? Sometimes it works. When q.new.*.v array contains less than 4 elements everything validates just fine (I can see error messages). But when i send for example such form,
data is not flashed to storage and i can not retrieve it using old() function. Image.
Some debug information:
[2020-02-06 22:37:51] local.INFO: Request all in form request: Array
(
[q] => Array
(
[new] => Array
(
[1] => Array
(
[name] => a
[v] => Array
(
[1] => Array
(
[text] => b
)
[2] => Array
(
[text] => c
)
[3] => Array
(
[text] => d
)
[4] => Array
(
[text] => e
)
)
)
)
)
)
[2020-02-06 22:37:51] local.INFO: Old() in form request: Array
(
)
[2020-02-06 22:37:51] local.INFO: Old in controller after redirect: Array
(
)
[2020-02-06 22:37:51] local.INFO: Request all in controller after redirect: Array
(
)
I've tried to transform q.new.*.v.*.text into array of q.new.1.v.1.text, q.new.1.v.2.text, q.new.1.v.3.text, but that didn't give much results.
The problem was with default place of storing sessions. By default, SESSION_DRIVER is cookie (their size is limited). So I have changed driver to file and all worked in the right way.
My array looks like this:
array:145 [▼
144 => array:2 [▼
0 => 1559739600000
1 => 39103.5828125
]
143 => array:2 [▼
0 => 1559739000000
1 => 39102.619270833
]
142 => array:2 [▼
0 => 1559738400000
1 => 39101.740234375
]
...
I need to change the 0s to time and 1s to data, so I can work with them within Laravel. If I try to reference them within Laravel like $0 I get T_Variable errors because of the rules that variables should not start with numbers. So I'm in a bind. So it ends up looking something like this:
array:145 [▼
144 => array:2 [▼
time => 1559739600000
data => 39103.5828125
]
143 => array:2 [▼
time => 1559739000000
data => 39102.619270833
]
142 => array:2 [▼
time => 1559738400000
data => 39101.740234375
]
...
You could do something like this using Laravel Collection.
$result = collect($array)->map(function ($item) {
return [
'time' => $item[0],
'data' => $item[1],
];
})->all();
I find this approach quite common in the Laravel community. For more info, pls check: https://laravel.com/docs/5.8/collections
I met some difficulties when I was trying to get value from an array in my php blade.
It has rather clear structure (printed with dd function)
{{dd($attr)}}
array:4 [▼
"id" => "215"
"type" => "select"
"name" => "Status"
"value" => array:2 [▼
"pred" => array:3 [▼
0 => "Employed"
1 => "On vacation"
2 => "Dismissed"
]
"sel_val" => "0"
]
]
And when I want to get a value by key 'sel_val' or 'pred'
print_r($attr['value']['pred']);
it gives me Illegal string offset 'pred'
And it works nice in Controller. What should i do?
It gives that error because pred is also an array. you'll have to do $attr['value']['pred'][0] to get Employed, $attr['value']['pred'][1] to get On vacation, $attr['value']['pred'][2] to get Dismissed and $attr['value']['sel_val'] to get the value of sel_val which is 0 in this case. Hope this helps.
working fine when we send array in compact function in the controller
$record = array('id' => '215', 'type' => 'select', 'value' => array('pred' => array('0'=> 'Employed', '1' => 'On vacation', '2' => 'Dismissed'),'sel_val' => '0'));
return view('home', compact('record'));