0

So i have been reading about array_search, array_values & array_keys but i cannot figure out how to search my array and put founded values in a new array.

I have allYearData array.
It looks likt this:

Array ( [0] => Array ( [id] => 7811 [objekt_element] => 23050-121-1_3105 [objekt_nr] => 23050-121-1 [element_nr] => 3105 [vart] => B.Avf [vem] => Blå [anteckn] => [datum] => 2015-01-29 18:00:19 )

Now i´d like to split this array in part by dates.
so, search in the "key" "datum" and find all "values" ecual to: "2015-01-%"
Put it in array: "janData".

So i tried this:

$janElementCount = array_search("2015-01-%", $allYearData);
print_r($janElementCount);

This won´t get me anything at all. How come?

1
  • 2
    Google: PHP preg_grep() Commented Oct 13, 2015 at 11:25

1 Answer 1

1

array_search() match exact value you are looking for PHP preg_grep()

$janElementCount = preg_grep("/^2015-01-.*$/", $allYearData[0]);

Note: Because your array is at [0] position so pass $allYearData[0].

See working DEMO

Sign up to request clarification or add additional context in comments.

9 Comments

Thank you. Regarding to the manual this looks like what i need. But is the "charaters" correct? because i get no hits... hmm...
Updated my answer try $janElementCount = preg_grep("/^2015-01-.*$/", $allYearData[0]);.
Ok. Now it works a bit ;) preg_grep("/v(\w+)/", $allYearData[0]); finds me [vart] => B.Avf. But: "/^2015-01-.*$/" finds me nothing.
But for me its giving correct output. I have added demo in answer.
No its not good approach to query 12 times. Always better approach to query minimum times for getting desired data. In your case querying one time and manipulating data is best then querying multiple times.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.