tags:

views:

110

answers:

6

I have an array of arrays, with the following structure:

array(array('page' => 'page1', 'name' => 'pagename1')
      array('page' => 'page2', 'name' => 'pagename2')
      array('page' => 'page3', 'name' => 'pagename3'))

Is there a built-in function that will return a new array with just the values of the 'name' keys? so I'd get:

array('pagename1', 'pagename2', 'pagename3')
A: 

Do you mean array_values?

Edit: Thanks for the comment-less downvote. If whoever had taken the time to actually look at the documentation for the function, you'd have seen the first comment that gives an example of writing your own array_values_recursive function. Combine that with an array_walk and you'd have a pretty nifty function that wouldn't have the 'name' column hardcoded into it. Sheesh.

Polygraf
I voted you up again, but really has this now become a competition to come up with the most complex solution? :)
Anti Veeranna
Thanks. :) Hopefully this isn't a competition, just a little annoyed that this happens a lot when I try steering people in a direction instead of writing out the solution for them..
Polygraf
+4  A: 

Why does it have to be a built in function? :) No, there is none, write your own.

Here is a nice and easy one, as opposed to others in this thread.

$namearray = array();

foreach ($array as $item) {
    $namearray[] = $item['name'];
}
Anti Veeranna
Okay, given your recent edit ('easy one, as opposed to others') I'll bite ;) If you turn your code into a real function as per the original request, it will be about as 'complex' as the the `array_map()` or `array_walk()` examples below, especially if you'd care to add an execution example.
Henrik Opel
Yeah I think mine is the most elegant given PHP's features and the first complete example. :)
fuentesjr
Hi, thanks. All I actually asked in my question was if there is a built-in function - I don't like to reinvent the wheel. You gave me an answer to that, and your solution is basically what I would have written.
Skilldrick
+3  A: 

Here's a functional way of doing it:

$data = array(
            array('page' => 'page1', 'name' => 'pagename1'),
            array('page' => 'page2', 'name' => 'pagename2'),
            array('page' => 'page3', 'name' => 'pagename3'));

$result = array_map(create_function('$arr', 'return $arr["name"];'), $data);
print_r($result);
fuentesjr
I like this, and the lambda in it fits nicely with my brain at the moment, as I've been learning Scheme. +1
Skilldrick
+2  A: 

Similar to fuentesjrs solution, but a bit more generic using array_walk() with a custom callback:

// Define the callback
function extract_named_sub_elements(&$item, $key, $name) {
  $item = $item[$name];
}

// Test data
$original = array(
  array('page' => 'page1', 'name' => 'pagename1'),
  array('page' => 'page2', 'name' => 'pagename2'),
  array('page' => 'page3', 'name' => 'pagename3'),
);

// Use a copy, as array_walk() operates directly on the passed in array
$copy = $original;

// Substitute 'name' with whatever element you want to extract, e.g. 'page'
array_walk($copy, 'extract_named_sub_elements', 'name');

print_r($copy);
Henrik Opel
A: 

You can extend the ArrayIterator class and override the method mixed current(void).

class Foo extends ArrayIterator {
  protected $index;
  public function __construct($array, $index) {
    parent::__construct($array);
    $this->index = $index;
  }

  public function current() {
    $c = parent::current();
    return isset($c[$this->index]) ? $c[$this->index] : null;
  }
}

$a = array(
  array('page' => 'page1', 'name' => 'pagename1'),
  array('name' => '---'),
  array('page' => 'page2', 'name' => 'pagename2'),
  array('page' => 'page3', 'name' => 'pagename3')
);

$f = new Foo($a, 'page');
foreach($f as $e) {
  echo $e, "\n";
}

prints

page1

page2
page3
VolkerK
+1  A: 

I don't think there is any need to have a built in function for this. There may be an array in your those array.

$samples=array(
            array('page' => 'page1', 'name' => 'pagename1'),
            array('page' => 'page2', 'name' => 'pagename2'),
            array('page' => 'page3', 'name' => 'pagename3')
            );

$output1=array();
$output2=array();
foreach($samples as $sample){
    array_push($output1,$sample['name']);
    $output2[]=array_splice($sample,1);

}

print_r($output1);
print_r($output2);

in $output1 is the output what you want if you want only to remove the 'page' indexing' part then $output2.

if you need all the values from the that array and indexes numerically the array then you can use

$array_1=array_values($samples);

but what i understand, you didn't want this.

Imrul