How to do explode inside the $request->all() array

Asked

Viewed 157 times

2

I need to go through this array and get all the keys that start with col. Does anyone know how to do this?

array:8 [▼
  "_token" => "NZ68b8h3L560aUc6DXBeb8Myb4JFD0hSgfSpJ2Lk"
  "_method" => "PUT"
  "col1-22" => "22"
  "col1-26" => "26"
  "col2-23" => "23"
  "col2-24" => "24"
  "col3-24" => "24"
  "col3-25" => "25"
]
  • You would like to make the explode with which delimiter?

  • 1

    Could you ask the question? I can’t paste.

  • the delimiter is the hyphen "-".

  • What is the purpose of explode()?

  • Why explode the value in the hyphen if the value related to that key is already the desired value? For example, you do not need to explode("-", "col1-22") to get the 22 if just access the position "col1-22" of array.

  • @Steniofrancis looks at logic col1-22" => "22"
 "col1-26" => "26"
 "col2-23" => "23"
 "col2-24" => "24"
 "col3-24" => "24"
 "col3-25" => "25" They all look alike col.

  • yes, let’s forget the explode, as I go through the array and get the keys from the col?

  • @Steniofrancia you want key name or cols value?

Show 3 more comments

3 answers

2

You can use the function strpos to find the position of the first occurrence of a string.

How the function returns the numerical position of the first occurrence and you want the keys that start with the string col, then the return of the function must be equal to 0. In this way, keys like _column will not be included - observation made by @Andersoncarloswoss.

$cols = [];

foreach ($array as $key => $value) {
    if (strpos($key, 'col') === 0) {
        $cols[] = $key;
    }
}

print_r($cols); //Array ( [0] => col1-22 [1] => col1-26 [2] => col2-23 [3] => col2-24 [4] => col3-24 [5] => col3-25 )

If keys start with "col", as an example "column", these will also be included in the array $cols.

  • I wouldn’t recommend leaving > -1 for it will seek the term throughout string; that is, if there was a key "_column" she would also be returned, even without starting with "col"

  • @Victorcarnaval if (strpos($key, 'col') !== false strpos($key,'col') == 2) {
 $cols[] = $key; 
 } The col size is 3, so it is at index 2.

  • @Andersoncarloswoss, thanks for the remark, I will update the reply.

  • @Maurydeveloper This will not work in this case because of the loose comparison. It would be necessary to do !== false, or as Victor put it, > -1.

  • @Andersoncarloswoss I was going to put,I even edited.

  • 1

    @Maurydeveloper Still wrong.

  • @Andersoncarloswoss Because I forgot the &&? if (strpos($key, 'col') !== false && strpos($key,'col') === 0) { $cols[] = $key; }

  • 2

    @Maurydeveloper Because the function strpos returns the index where the string, not where it ends. It makes no difference the size of the string you are looking for, the desired index will be 0 since we only want those that start with this value. You can read more about the function in the documentation.

  • @Andersoncarloswoss I know, but I forgot. Thank you for warning.

Show 4 more comments

1

You can make a foreach and take the key and from there do the explode

<?php 
    $array = array(
              "_token" => "NZ68b8h3L560aUc6DXBeb8Myb4JFD0hSgfSpJ2Lk",
              "_method" => "PUT",
              "col1-22" => "22",
              "col1-26" => "26",
              "col2-23" => "23",
              "col2-24" => "24",
              "col3-24" => "24",
              "col3-25" => "25"
             );

    foreach ($array as $key => $value) {
        $result = substr($key, 0, 3);
        if( $result == 'col' ){
            $exp = explode('-', $key);
            echo "<pre>";
            print_r($exp);
            echo "</pre>";
        }
    }        
?>

I hope it helps

  • 1

    You could put what index is the -. strpos

0

A solution using Laravel’s Ollection

$array = array(
            "_token" => "NZ68b8h3L560aUc6DXBeb8Myb4JFD0hSgfSpJ2Lk",
            "_method" => "PUT",
            "col1-22" => "22",
            "col1-26" => "26",
            "col2-23" => "23",
            "col2-24" => "24",
            "col3-24" => "24",
            "col3-25" => "25"
           );

        $keys=collect($array)->filter(function ($value, $key) {
            return Str::startsWith($key, 'col'); }
        )->keys();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.