1

I have an array of form:

$records = array( array( 'id' => 2135, 'first_name' => 'John', 'last_name' => 'Doe', ), array( 'id' => 3245, 'first_name' => 'Sally', 'last_name' => 'Smith', ), array( 'id' => 5342, 'first_name' => 'Jane', 'last_name' => 'Jones', ), array( 'id' => 5623, 'first_name' => 'Peter', 'last_name' => 'Doe', ) ); 

I want output like this :

EDIT:

 Array ( array('first_Name' => John), array('first_Name'=> Sally), array('first_Name'=> Jane), array('first_Name'=> Peter) ); 

Can this be achieved??

3
  • 4
    how can be first_name as multiple index of array ? Commented May 21, 2014 at 6:47
  • that has the same keys, that aint possible Commented May 21, 2014 at 6:49
  • An associative array cant hold the same index key Commented May 21, 2014 at 6:50

7 Answers 7

3

You cannot do this since you can't have duplicate keys in an array.

Here is a related question: How to allow duplicate keys in a PHP array?

This article explains how PHP stores array internally: http://nikic.github.io/2012/03/28/Understanding-PHPs-internal-array-implementation.html.

Answer after your edit:

$arr = array(); foreach($records as $value) { $arr[] = array('first_name' => $value['first_name']); } print_r($arr); 
Sign up to request clarification or add additional context in comments.

5 Comments

+1. Or just: array_walk($records, function ($v, $k) use (&$arr) { $arr[] = ['first_name' => $v['first_name']]; });
And how is this any different from nl-x's answer? Add to that you edited your post after nl-x posted his answer
@asprin Probably my typing speed is slower than nl-x . While i was typing he answered.
@asprin: It's not like there are thousand different ways to achieve this :-)
@asprin For your happiness.It is different now.
2

You can achieve your (edited) question like this:

$new_array = array(); foreach($records as $record) $new_array[] = array('first_Name'=>$records['first_name']); 

Comments

1

try this

$records = array( array( 'id' => 2135, 'first_name' => 'John', 'last_name' => 'Doe', ), array( 'id' => 3245, 'first_name' => 'Sally', 'last_name' => 'Smith', ), array( 'id' => 5342, 'first_name' => 'Jane', 'last_name' => 'Jones', ), array( 'id' => 5623, 'first_name' => 'Peter', 'last_name' => 'Doe', ) ); $tmp = array('first_name' => ''); foreach ($records as &$record) { $record = array_intersect_key($record, $tmp); // or $record = array('first_name' => $record['first_name']); } unset($record); var_dump($records); 

Comments

1

If you want to array with in the array means, surely it will generated with index key, like this

 Array ( '0' => array('first_Name' => John), '1' => array('first_Name'=> Sally), '2' => array('first_Name'=> Jane), '3' => array('first_Name'=> Peter) ); 

Try this one,

 foreach($records as $record) { $name_array[] = Array('first_Name' =>$record['first_name']); } print_r($name_array); 

Your output will be,

Array ( [0] => Array ( [first_Name] => John ) [1] => Array ( [first_Name] => Sally ) [2] => Array ( [first_Name] => Jane ) [3] => Array ( [first_Name] => Peter ) ) 

1 Comment

This is the same as this answer and this one. Not sure if having another one with the same content is going to be helpful.
1

To get your desired output you can use array_map, though you can get similar output with array_column which is new in PHP 5.5.

With array_map (http://3v4l.org/v8Y8Z):

<?php $firstNames = array_map(function($record) { return ['first_name' => $record['first_name']]; }, $records); 

With array_column (http://3v4l.org/ohnGu):

$firstNames = array_column($records, 'first_name'); 

Note: array_column doesn't make subarrays with the first_name key.

Comments

0

you can try this one

$arr_output = array(); foreach($records as $key=>$arr) { $arr_output['id'][] = $arr['id']; $arr_output['first_name'][] = $arr['first_name']; $arr_output['last_name'][] = $arr['last_name']; } print_r($arr_output['first_name']); // display all first names print_r($arr_output); // display complete output array. 

Output :

Array ( [id] => Array ( [0] => 2135 [1] => 3245 [2] => 5342 [3] => 5623 ) [first_name] => Array ( [0] => John [1] => Sally [2] => Jane [3] => Peter ) [last_name] => Array ( [0] => Doe [1] => Smith [2] => Jones [3] => Doe ) ) 

Demo

2 Comments

This is not what the OP is askin
@user2129794 as i commented on your question same key is not possible in array. you can use it as i answered.
0

This is not possible, because you cannot have multiple elements of the same key in an associative array.

5 Comments

Seriously? This constitutes as an answer?
@nl-x and Amal _ I wasn't saying the answer is wrong. What I was referring to is a single sentence being posted as an answer. It could well have been posted as a comment
@asprin : I know that you meant that. So still, why not?
@asprin : according to your link it IS an answer. (see #8 in of your link) It says it should be an answer, and should not be reported as NaA (Not an Answer)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.