简体   繁体   中英

Find values from date in array

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM