Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve multiple array values with extract

Tags:

cakephp

array(
    [0]=>
        [index1]=>something
        [index2]=>something else
        [index3]=>something more
    [1]=>
        [index1]=>something
        [index2]=>something else
        [index3]=>something more
    [2]=>
        [index1]=>something
        [index2]=>something else
        [index3]=>something more
)

EDIT: So I would like to retrieve the following:

array(
    [0]=>
        [index1]=>something
        [index2]=>something else
    [1]=>
        [index1]=>something
        [index2]=>something else
    [2]=>
        [index1]=>something
        [index2]=>something else
)

How do I get multiple indexes of the array using the Set::extract function in cakephp?

This retrieves one value:

Set::extract($array, '{n}.index1');

but I would like to get multiple values ... say, index1 and index2.

I tried statements like the following, to no avail.

Set::extract($array, '[{n}.index1, {n}.index2']);

EDIT

    $__pages = Hash::merge(
                    Hash::extract($pages, 'pages.{n}.id'),
                    Hash::extract($pages, 'pages.{n}.title')
                    );
    pr($__pages);

Output:

Array
(
    [0] => 2
    [1] => 4
    [2] => 104
    [3] => Sample Page
    [4] => about us
    [5] => Services
)

That doesn't really help me since I still need the association like so:

Array(
   [2] => Sample Page
   [4] => About us
   [104] => Services
)

I would even be happy with :

Array(
   Array(id => 2, title => Sample Page)
   Array(id => 4, title => About Us)
   Array(id => 104, title => Services)
)

ANSWER thecodeparadox's answer works for the test code that I provided. Here is the real life code in case someone stumbles here. In the book it states, "any string literal enclosed in brackets besides {n} and {s}) is interpreted as a regular expression." This line seemed to be hidden and not very blatant. So knowing this, I simply used regex rules to retrieve the data I needed. I have an array that pulled wordpress posts from an api, I needed to narrow down the results to id, title.

array(
   posts=>
      0=>
         id => 3
         slug => sample-page
         type => page
         title => Sample Page
         //...and so on 

      1=>
         id => 7
         slug => sample-page-2
         type => page
         title => Sample Page 2
         //...and so on 

To retrieve just the id and title I added the following line.

pr(Set::classicExtract($pages, 'pages.{n}.{(id|title)}'));

this gave me:

array(
   posts=>
      0=>
         id => 3
         title => Sample Page

      1=>
         id => 7
         title => Sample Page 2

DOCUMENTATION: Book

like image 721
Tim Joyce Avatar asked Nov 27 '12 14:11

Tim Joyce


People also ask

How do I extract multiple values from an array?

To extract only a subset of the array, use the array_slice( ) function: $subset = array_slice (array , offset , length ); The array_slice( ) function returns a new array consisting of a consecutive series of values from the original array.

How to extract array value in php?

PHP | extract() Function. The extract() Function is an inbuilt function in PHP. The extract() function does array to variable conversion. That is it converts array keys into variable names and array values into variable value.


1 Answers

$arr = array(
            array(
                'index1'=>'something',
                'index2'=>'something else',
                'index3'=>'something more',
            ),
            array(
                'index1'=>'something',
                'index2'=>'something else',
                'index3'=>'something more',
            ),
            array(
                'index1'=>'something',
                'index2'=>'something else',
                'index3'=>'something more',
            )
        );

$output = Set::classicExtract($arr, '{n}.{index[1-2]}');

print_r($output);

// output


Array
(
    [0] => Array
        (
            [index1] => something
            [index2] => something else
        )

    [1] => Array
        (
            [index1] => something
            [index2] => something else
        )

    [2] => Array
        (
            [index1] => something
            [index2] => something else
        )

)
like image 175
thecodeparadox Avatar answered Sep 25 '22 12:09

thecodeparadox