0

I've got the following Array...

$array = array(
"1" => "0",
"2" => "5",
"5" => "7",
"1C" => "0",
"2C" => "5",
"5C" => "10",
"1S" => "15",
"5S" => "20"
);

Is there an easy way to shorten the array to only show the key's that contain C? Or split the array where 1 array has the C's and 1 array has the S's and 1 array for just numbers. I don't mind if the new array(s) have different names.

The number of repetitions for just numbers, C's, and S's will be different based on information returned from database. So I cannot use array_slice amd array_chunk in the default of how they were designed to perform.

I know I can use a foreach loop to loop through the key => value pairs and us strpos() to find C, S, and just numbers; however, I was hoping there may be a better way via an array function.

KDJ
  • 292
  • 1
  • 15
  • use `array_filter` or something like that with some regular expression – ArtOsi Jun 11 '18 at 12:39
  • Yes, have you looked into the `foreach(){}` loop? Have you tried anything yet? Please explain your failed attempts. – MonkeyZeus Jun 11 '18 at 12:39
  • @MonkeyZeus - I know I can do a foreach loop and use strpos() on the return; however, I was hoping there was a better method than that via an array function. – KDJ Jun 11 '18 at 12:40
  • Possible duplicate of [How to make this multidimensional array into three specified parts?](https://stackoverflow.com/questions/50747240/how-to-make-this-multidimensional-array-into-three-specified-parts) – MonkeyZeus Jun 11 '18 at 12:41
  • It's in your best interest to show that you've actually tried something rather than asking for people to spoon-feed you code... – MonkeyZeus Jun 11 '18 at 12:43

4 Answers4

1

You can use array_filter. As example:

$array = array(
"1" => "0",
"2" => "5",
"5" => "7",
"1C" => "0",
"2C" => "5",
"5C" => "10",
"1S" => "15",
"5S" => "20"
);
$arrayWithC = array_filter($array, function($key) {
    return strpos($key, 'C') !== false;
}, ARRAY_FILTER_USE_KEY);

In this case ARRAY_FILTER_USE_KEY flag used for passing into callback function only keys of array.

Artem Ilchenko
  • 1,050
  • 9
  • 20
  • okay awesome. Now once I do that with C and S; what would the return look like for just numbers? `return (is_numeric($key) !== false` ??? – KDJ Jun 11 '18 at 12:45
  • 1
    you can use any condition in callback function and in result array will get only values for which function returned true – Artem Ilchenko Jun 11 '18 at 12:47
1

Here is one option that is looping and may or may not give a array structure that is simpler to use.
You decide, I think I would want this but it depends on how you use it.

Foreach($array as $key => $item){
    If(!is_numeric(substr($key, -1))){
        $new[substr($key, -1)][substr($key, 0,1)] = $item;
    }Else{
        $new[] = $item;
    }
}

Var_dump($new);

It places the normal in the main array then subarrays with the S and C items

https://3v4l.org/Aj9ln

Andreas
  • 23,610
  • 6
  • 30
  • 62
  • this actually ended up being the best option for what I was doing. It allowed me to group all of the values into unique subarrays and then sort them how I needed to. Perfect example. – KDJ Jun 11 '18 at 15:12
  • Glad you liked it! – Andreas Jun 11 '18 at 15:31
1

You might use array_walk and check if the key contains C:

$array = array("1" => "0", "2" => "5", "5" => "7", "1C" => "0", "2C" => "5", "5C" => "10", "1S" => "15", "5S" => "20");
$keyContainsC = [];
array_walk($array, function ($value, $key) use (&$keyContainsC) {
    if (strpos($key, 'C') !== false) $keyContainsC[$key] = $value;
});

print_r($keyContainsC);

Or create an array where you could access the filtered arrays by key:

array_walk($array, function ($value, $key) use (&$result) {
    if (strpos($key, 'C') !== false) $result['C'][$key] = $value;
    if (strpos($key, 'S') !== false) $result['S'][$key] = $value;
    if (strpos($key, 'C') === false && strpos($key, 'S') === false) $result['NR'][$key] = $value;
});

print_r($result['S']);

Test

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
1

You could do something like this:

$array = array(
  "1" => "0",
  "2" => "5",
  "5" => "7",
  "1C" => "0",
  "2C" => "5",
  "5C" => "10",
  "1S" => "15",
  "5S" => "20"
);

# Define how you want to split your array
$keySplitingData = [
  "C" => function($key){
    return strpos($key, 'C') !== false;
  },
  "S" => function($key){
    return strpos($key, 'S') !== false;
  },
  "numbers" => function($key){
    return is_numeric($key);
  },
];

foreach($keySplitingData as $k => $fn){
   ${"array_with_" . $k} = array_filter($array, $fn, ARRAY_FILTER_USE_KEY);
}

var_dump($array_with_C); /* =>
array(3) {
  ["1C"]=>
  string(1) "0"
  ["2C"]=>
   string(1) "5"
  ["5C"]=>
  string(2) "10"
}
*/
Oli Crt
  • 1,123
  • 1
  • 11
  • 13