3

I have an array that looks

$articles = array([0] => array('title' => 'When ....',
                               'description' => '....', 
                               'created' => '2011-02-21'
                              ), 
                  [1] => array('title' => 'Something ....',
                               'description' => 'When ....', 
                               'created' => '2011-02-21'
                              ),
            );

I want to extract only the titles. Is there anyway to retrieve the titles without using for and foreach loops. I dont mind the title becomes a single string. I was thinking implode the array but that adds description and created.

Toby Allen
  • 10,997
  • 11
  • 73
  • 124
Rick
  • 33
  • 1
  • 3

7 Answers7

11

considering its CakePHP, $titles = Set::extract('/title', $articles);

edit:

http://book.cakephp.org/view/1487/Set

Update:

With CakePHP 2.x Hash has replaced Set.

$titles = Hash::extract($articles, '{n}.title');

dogmatic69
  • 7,574
  • 4
  • 31
  • 49
8

You can use for example array_map, but why do you not want to use Loops? In fact every method, that is able to modify the array in the way you want it, will iterate over it.

function reduce_to_title ($item) {
  return $item['title'];
};
$titles = array_map('reduce_to_title', $articles);

Or since PHP>=5.3

$titles = array_map(function ($item) {
  return $item['title'];
}, $articles);
KingCrunch
  • 128,817
  • 21
  • 151
  • 173
  • if element of array not exist return null element, you can detect `$titles = array_map(function ($item) { return (isset($item['title'])) ? $item['title']: "Empty title";},$articles);` – fermin Feb 24 '11 at 11:28
  • @Shrapnel: As I already said, this is also just an iteration, so yeah, there is no benefit over `foreach`. And second one is called "closure" and is new since PHP5.3. It is as ugly as every anonymous function in nearly every language, that supports anonymous functions. But its short, readable and efficient. – KingCrunch Feb 24 '11 at 12:02
  • @fermin: I (implicitly) assumed, that every inner array is "complete", but of course you are right: You code will also handle "broken" entries. – KingCrunch Feb 24 '11 at 12:05
  • Yes, foreach has no benefit, at least on short arrays, but foreach is short, readable and familiar, nothing wrong with it. So, there is not a single reason to devise something twisted. Nope, it's not short nor readable. It's ugly to put whole function into function parameters list, be honest with yourself. – Your Common Sense Feb 24 '11 at 12:41
  • 2
    Set::extract('/title', $articles); is as short as it gets. – dogmatic69 Feb 24 '11 at 13:05
  • @Shraphnel: If you will learn PHP5.3 some times, you will realize, that it is short and readable :) Since then you can think about, that the iteration in `array_map` is implemented in C, but `foreach` iterates in PHP. Additional, the two (and a half) common concepts "anonymous functions" and "callbacks"/"handler" are "ugly"? I dont think so... they are quite common in many languages ;) – KingCrunch Feb 24 '11 at 13:31
  • @Shrapnel: Also, I said "There is no benefit OVER `foreach`", not "`foreach` has no benefit". – KingCrunch Feb 24 '11 at 13:35
5

you can use this

print_r(array_map('array_shift', $articles));

EDIT :

Assumption : if title is the first element of array.

Gaurav
  • 28,447
  • 8
  • 50
  • 80
  • Assumes that title will always be the first element of each nested array, but a nice, clean, simple solution – Mark Baker Feb 24 '11 at 11:09
2

since 5.5, array_column does exactly what you explained.

$titles = array_column($articles, "title"); // [0=>"When",1=>"Something"]

For more examples check the PHP manual

mhh1422
  • 135
  • 5
0

What about while loops?

reset($articles); while($a = each($articles)){echo $a['value']['title'];}
Or Weinberger
  • 7,332
  • 23
  • 71
  • 116
0

an improved version of KingCrunch's answer:

function get_column(&$array,$column) {
  foreach ($array as $value) {
    $ret[]=$value[$column];
  }
  return $ret;
}

this is pretty universal function and can be put into some library, and then called with just single line, which is shorter than in every other answer:

$titles = get_column($articles, 'title');

Update
However, it seems that cake already have such a function, so, the only proper answer is How to extract particular fields from an array

Community
  • 1
  • 1
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
0

Hey, since you're using CakePHP, why don't you just add title to the fields array in your find()? For example, the following,

$this->Article-find('all', array('fields'=>'Article.title'));

will pull only the titles of all matching Articles in the database.

Nick
  • 1,311
  • 2
  • 10
  • 27