0

I've got an array of arrays called $jsonvpr. When trying to pull a preg_grep like preg_grep("/Jake.*.Peavy/i", $jsonvpr) it returns Notice: Array to string conversion in /var/www/html/crawlnew.php on line 310 a bunch of times (I supose once per entry on the array) and it don't return anything. I suspect the problem is that there are not just arrays, but arrays of arrays, and that's why it isn't worked as I wanted, but I don't know how should I do. Please, anyone can point me on the right direction?

A sample of how the array would be:

array(102) {
  [0]=>
  array(8) {
    [0]=>
    string(4) "Rank"
    [1]=>
    string(6) "Player"
    [2]=>
    string(13) "Games Started"
    [3]=>
    string(16) "Excellent Starts"
    [4]=>
    string(14) "Neutral Starts"
    [5]=>
    string(11) "Poor Starts"
    [6]=>
    string(12) "Volatility %"
    [7]=>
    string(3) "VPR"
  }
  [1]=>
  array(8) {
    [0]=>
    string(0) ""
    [1]=>
    string(73) "Clayton Kershaw"
    [2]=>
    string(0) ""
    [3]=>
    string(0) ""
    [4]=>
    string(0) ""
    [5]=>
    string(0) ""
    [6]=>
    string(0) ""
    [7]=>
    string(0) ""
  }
  [2]=>
  array(8) {
    [0]=>
    string(0) ""
    [1]=>
    string(73) "Felix Hernandez"
    [2]=>
    string(0) ""
    [3]=>
    string(0) ""
    [4]=>
    string(0) ""
    [5]=>
    string(0) ""
    [6]=>
    string(0) ""
    [7]=>
    string(0) ""
  }
  [...]
  • Your array is multidimensional! – Rizier123 Apr 17 '15 at 07:03
  • there for dont use multidimensional array. – niyou Apr 17 '15 at 07:03
  • what exactly do you want to return back, the whole array entry or just a name? – Nikos M. Apr 17 '15 at 07:06
  • The whole array entry. In instance, I'll need it to find by name with the regex, and when it finds it, I need to know what entry is it, in order to beeing able to modify its stats (Rank, Player, Games Started...). For example, if the regex was `/Clayton.*.Kershaw/i` I'll need to have returned the value 1, so I know that Clayton Kershaw is inside `$jsonvpr[1]` – Cornezuelo del Centeno Apr 17 '15 at 07:09

1 Answers1

0

You could get all the names into an array first and preg_grep over that:

$names = array_column($jsonvpr, 1);
$foundElements = preg_grep("/Jake.*.Peavy/i", $names);

Where the second argument of array_column, 1, is the index of the array element you want to match.

WesleyE
  • 1,384
  • 1
  • 12
  • 29
  • Thank you, that's actually pretty clever! I made a different aproach though, but you put me on the track! What I did was: `foreach($jsonvpr as $result) array_push($vprnames, $result[1]);` and then `$pitcherpos = preg_grep($regex, $vprnames);` – Cornezuelo del Centeno Apr 17 '15 at 07:23