cakephp extract array

Solutions on MaxInterview for cakephp extract array by the best coders in the world

showing results for - "cakephp extract array"
Elissa
12 Feb 2018
1// input
2$data = Array(
3    [0] => Array (
4            [Product] => Array (
5                    [id] => 5
6                    [product_number] => RT001C
7                ))
8    [1] => Array (
9            [Product] => Array  (
10                    [id] => 7
11                    [product_number] => RC001C 
12                ))
13);
14
15$data = Hash::extract($data, '{n}.Product');
16// => output
17$data = Array(
18  	[0]	=> Array (
19      	[id] => 5
20      	[product_number] => RT001C
21    )
22    [1] => Array (
23     	[id] => 7
24      	[product_number] => RC001C 
25    )
26);
27
Laura
27 Jan 2019
1According to the documentation: "Tokens are composed of two groups. Expressions, are used to traverse the array data, while matchers are used to qualify elements.".
2
3{n}[rating>-1] is considered one token. 
4{n} is the expression which filters the array keys, in this case the key must be numeric. 
5[rating>-1] is the matcher which filters the array elements, in this case the element must be an array that contains a key named rating and an associated value that is greater than -1. 
6Once you have the array element then you can get the rating.
7
8$ratings = array(
9  'Rating' => array(
10    array(
11      'id' => 4,
12      'rating' => -1
13    ),
14    array(
15      'id' => 14,
16      'rating' => 9.7
17    ),
18    array(
19      'id' => 26,
20      'rating' => 9.55
21    )
22  )
23);
24print_r( Hash::extract($ratings, 'Rating.{n}[rating>-1].rating') );
25
26Results in:
27
28Array ( [0] => 9.7 [1] => 9.55 )
29