1

I have data like this:

array:1 [
  0 => "No Brand,ddfg"
]

First of all this data is wrong, what I want is to have something like this:

array:2 [
  0 => "No Brand"
  1 => "ddfg"
]

So now its really an array :)

Then I need my array data transform to lower case like:

array:2 [
  0 => "no brand"
  1 => "ddfg"
]

Code

$sibarBrandsArray = SidebarManager::first()->pluck('brands')->toArray();

This return data like:

array:1 [
  0 => "No Brand,ddfg"
]

And this is how my data looks like in database:

one

Any idea?

mafortis
  • 6,750
  • 23
  • 130
  • 288
  • 1
    This is probably a good example of why you shouldn't store comma separated lists in tables - [Is storing a delimited list in a database column really that bad?](https://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad) – Nigel Ren Jul 18 '20 at 07:51
  • @NigelRen It would be very helpful if you provide a solution then add a suggestion link :) – mafortis Jul 18 '20 at 07:53
  • Why don't you save it at json – ashok poudel Jul 18 '20 at 07:54
  • The solution is to redesign your database to be normalized properly. I'm sorry that without any database structure in the question it's difficult to provide an answer. – Nigel Ren Jul 18 '20 at 07:55
  • @ashokpoudel dealing with json data and going trough lots of foreach's is a nightmare. – mafortis Jul 18 '20 at 07:55
  • @NigelRen I've already share database screenshot so you know how it's structured but ok thanks. – mafortis Jul 18 '20 at 07:56
  • A screenshot of 1 table does not give a database structure. – Nigel Ren Jul 18 '20 at 07:57
  • 1
    So how can a foreach be worse then a table that is incorrect and out right wrong? I mean what happens if someone adds as input `hello, my name is John` but that should be one item not two? – Andreas Jul 18 '20 at 07:57
  • @Andreas this case of my table the stored data does not have comma in between like `hello,....` that i'm assure of. – mafortis Jul 18 '20 at 08:00
  • "_First of all this data is wrong_" But that's the data you store in your database. There might be something wrong with how you store data then. You can use [explode()](https://www.php.net/manual/en/function.explode.php) to explode that string by `,`, then [foreach](https://www.php.net/manual/en/control-structures.foreach.php) to loop over the exploded array and [stotolower()](https://www.php.net/manual/en/function.strtolower.php) to make them lowercase – brombeer Jul 18 '20 at 08:09
  • @kerbh0lz thanks man already solved it and shared my solution. – mafortis Jul 18 '20 at 08:11

3 Answers3

1

Solved

// get my table row
$sibarBrandsArray = SidebarManager::first();
// get my row column
$getBrandColumn = $sibarBrandsArray->brands;
// separate data in that column with comma
$separateBrands = explode(',', $getBrandColumn);
// lowercase each separated data
$brandsArray = array_map('strtolower', $separateBrands);
// dump the result
dd($brandsArray);

Result

array:2 [
  0 => "no brand"
  1 => "ddfg"
]
Dharman
  • 30,962
  • 25
  • 85
  • 135
mafortis
  • 6,750
  • 23
  • 130
  • 288
0

Laravel has a very efficient and easy way to work with arrays. It's called a collection. Click here to learn more. Don't convert your response to the array, use collection directly.

$sibarBrandsCollection = SidebarManager::first()->pluck('brands');

Laravel eloquent by default gives you collection instance when you get a response. so pluck in above call is nothing but calling pluck on collection instance. We can chain method to the collection and do manipulation as needed.

$sibarBrandsCollection = $sibarBrandsCollection->map(function ($name) {
   return strtolower($name);
});

Above code will automatically convert all of your values to lowercase. Similarly, you can explode the value to get your intended result. At last, if you have to send data as array to the browser just add toArray() method at the end of your collection.

I would not use core PHP array function unless needed, Laravel collection is great way to work with arrays.

-1
$yourArray = array_map('strtolower', $yourArray);
$yourArray = array_map('nestedLowercase', $yourArray);

function nestedLowercase($value) {
    if (is_array($value)) {
        return array_map('nestedLowercase', $value);
    }
    return strtolower($value);
}

or you can use:

$query->whereRaw('LOWER(`newsTitle`) LIKE ? ',[trim(strtolower($newsTitle)).'%']);
R3Z4
  • 51
  • 1
  • 1
  • 9
  • 1
    any explanation? – mafortis Jul 18 '20 at 07:54
  • nested one return error but first `array map` return this `array:1 [ 0 => "no brand,ddfg"]` just make it lower but not separate them – mafortis Jul 18 '20 at 07:58
  • @Reza please provide more details about the first block of your code. Also, the author claims that this solution doesn't work, so please provide some kind of proof to show that it works – Meir Gabay Jul 22 '20 at 11:27