Get column values from an associative array in php - php

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??

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);

You can achieve your (edited) question like this:
$new_array = array();
foreach($records as $record)
$new_array[] = array('first_Name'=>$records['first_name']);

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);

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
)
)

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.

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

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

Related

PHP : How to make parallel arrays from multidimentional array

I have this type of array
$arr = array(
0 => array(
0 => array(
'name' => 'test1',
'country' => 'abc'
)
1 => array(
'name' => 'test2',
'country' => 'xyz'
)
)
1 => array(
'name' => 'test3',
'country' => 'pqr'
)
);
How can I make all arrays as parallel arrays. So that all sub arrays are parallel to each other without using any loop.
Like this
$arr = array(
0 => array(
'name' => 'test1',
'country' => 'abc'
)
1 => array(
'name' => 'test2',
'country' => 'xyz'
)
2 => array(
'name' => 'test3',
'country' => 'pqr'
)
);
Any help is much appreciated. !
A dynamic version of Nigel's code would be to loop the array and merge each subarray.
$new = [];
foreach($arr as $subarr){
$new = array_merge($new, $subarr);
}
var_dump($new);
https://3v4l.org/np2ZD
You could simply merge the arrays...
$out = array_merge($arr[0], [$arr[1]]);
print_r($out);
Which gives...
Array
(
[0] => Array
(
[name] => test1
[country] => abc
)
[1] => Array
(
[name] => test2
[country] => xyz
)
[2] => Array
(
[name] => test3
[country] => pqr
)
)

How to flatten (compress) multi-dimensional array into one-dimensional array in PHP?

I got this array. I want all the other 3 array come into this [0] => Array. Don't want unique value just want to merge all array flat in to [0] => Array.
Array
(
[0] => Array
(
[0] => Array
(
[Campaign] => xxx
[Phone] => 111
[State] => cd
)
)
[1] => Array
(
[0] => Array
(
[Campaign] => zxxxzx
[Phone] => 111111
[State] => zxxx
)
)
[2] => Array
(
[0] => Array
(
[Campaign] => aaaa
[Phone] => 111
[State] => Csd
)
)
[3] => Array
(
[0] => Array
(
[Campaign] => sasa
[Phone] => 111
[State] => asas
)
)
)
This is another example of how important the naming is. What you are working with is basically:
$recordsGroups = array(
// first group:
array(
// record 1:
array(
'key1' => 'val1',
'key2' => 'val2',
),
// record 2:
array(
'key1' => 'aaa',
'key2' => 'bbb',
),
),
// 2nd group:
array(
// record 3:
array(
'key1' => 'ccc',
'key2' => 'ddd',
),
),
);
And what you are probably trying to do is:
$records = array();
foreach ($recordsGroups as $group)
foreach ($group as $record)
$records[] = $record;
Which will give you:
$records = array(
// record 1:
array(
'key1' => 'val1',
'key2' => 'val2',
),
// record 2:
array(
'key1' => 'aaa',
'key2' => 'bbb',
),
// record 3:
array(
'key1' => 'ccc',
'key2' => 'ddd',
),
);
This should do nicely:
$array = call_user_func_array('array_merge', $array);
Or Argument unpacking via ... (splat operator):
$array = array_merge(...$array);
Because arrays can't have duplicate keys, the best that this multi-dim array can be condensed is down to an indexed array of associative arrays. array_column() will make quick work of this task.
Code: (Demo)
$array=[
[
['Campaign'=>'xxx','Phone'=>'111','State'=>'cd']
],
[
['Campaign'=>'zxxxzx','Phone'=>'111111','State'=>'zxxx']
],
[
['Campaign'=>'aaaa','Phone'=>'111','State'=>'Csd']
],
[
['Campaign'=>'sasa','Phone'=>'111','State'=>'asas']
]
];
var_export(array_column($array,0));
Output:
array (
0 =>
array (
'Campaign' => 'xxx',
'Phone' => '111',
'State' => 'cd',
),
1 =>
array (
'Campaign' => 'zxxxzx',
'Phone' => '111111',
'State' => 'zxxx',
),
2 =>
array (
'Campaign' => 'aaaa',
'Phone' => '111',
'State' => 'Csd',
),
3 =>
array (
'Campaign' => 'sasa',
'Phone' => '111',
'State' => 'asas',
),
)

convert multidimensional array to single dimension

convert multidimensional array to single dimension.
I have a multidimensional array like this..PHP using array finctions
Array
(
[0] => Array
(
[0] => Name1
[1] => valueOfName1
)
[0] => Array
(
[0] => Name2
[1] => valueOfName2
)
[2] => Array
(
[0] => Name3
[1] => valueOfName3
)
[3] => Array
(
[0] =>
)
[4] => Array
(
[0] => Name4
[1] => valueOfName4
)
[5] => Array
(
[0] =>
)
);
i want output like this..unsing any of function fo array
Array
(
Name1 => valueOfName1
Name2 => valueOfName2
Name3 => valueOfName3
Name4 => valueOfName4
)
Try this code:
$newArr = array();
foreach($mainArr as $key=>$value) {
if(isset($value[0]) && $value[0]!= '' && isset($value[1]) && $value[1] != '') {
$newArr[$value[0]] = $value[1];
}
}
print_r($newArr);
its simple... first try something befor you post a question...
$people = array (
"1" => array (
"0" => "greenspan",
"1" => 32
),
"2" => array (
"0" => "doe",
"1" => 52
)
);
$new_people = array();
while(list($person, $person_array) = each($people))
{
while(list($person_attribute, $value) = each($person_array))
{
$new_people[$person_attribute] = $value;
}
}
print_r($new_people);
Try out This Example:
<?php // Array representing a possible record set returned from a database
$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',
) );
$last_names = array_column($records, 'last_name', 'id');
print_r($last_names); ?>
Output :
Array (
[2135] => Doe
[3245] => Smith
[5342] => Jones
[5623] => Doe )
Hope this might be useful to you !!!

how get each single column data from php multidimensional array?

how get each single column data from php multidimensional into single column array?
like $_test = = array(
array(
'id' => 1001,
'first_name' => 'kalpesh',
'last_name' => 'gamit',
),
array(
'id' => 1002,
'first_name' => 'kartik',
'last_name' => 'patel',
),
array(
'id' => 2002,
'first_name' => 'smith',
'last_name' => 'Jones',
),
array(
'id' => 4004,
'first_name' => 'patel',
'last_name' => 'Doe',
)
);
want result like id OR first_name only without use loop like while, foreach, for etc
Array
(
[0] => 1001
[1] => 1002
[2] => 2002
[3] => 4004
)
update your script like below and check it please but it will work only in PHP 5.5 greater than version only...
$_test = = array(
array(
'id' => 1001,
'first_name' => 'kalpesh',
'last_name' => 'gamit',
),
array(
'id' => 1002,
'first_name' => 'kartik',
'last_name' => 'patel',
),
array(
'id' => 2002,
'first_name' => 'smith',
'last_name' => 'Jones',
),
array(
'id' => 4004,
'first_name' => 'patel',
'last_name' => 'Doe',
)
);
$ids_only = array_column($records, 'id');
print_r($ids_only);
Results for ID
Array
(
[0] => 1001
[1] => 1002
[2] => 2002
[3] => 4004
)
Results for first_name
$first_name = array_column($records, 'id');
print_r($first_name);
Array
(
[0] => Kalpesh
[1] => kartik
[2] => smith
[3] => patel
)
run above php script and please check....
You could use array_map:
$id_list = array_map(function($var) {
return $var['id'];
}, $_test);

PHP Store Key Value from Associative Array into Simple Array

I'm having trouble wrapping my head around this, any help would be GREAT...
I have an array $stores that is structured like so:
Array
(
[0] => Array
(
[id] => 123
[name] => 'Store A'
)
[1] => Array
(
[id] => 345
[name] => 'Store B'
)
[2] => Array
(
[id] => 567
[name] => 'Store C'
)
[3] => Array
(
[id] => 789
[name] => 'Store D'
)
)
I want to extract the 'id' values from this array into a simple array that looks this:
$simple = array(123,345,567,789);
If you use php 5.5+, array_column() is quite useful :
$simple = array_column($yourarray,'id');
http://php.net/array_column
Calimero definitely had the best answer for PHP 5.5+, but if you want the same functionality in prior versions, check this repository: https://github.com/ramsey/array_column . It is written by PHP 5.5 array_column creator itself.
If you can't use array_column, you can use array_map:
$names = array(
array('id' => 123, 'name' => 'A'),
array('id' => 456, 'name' => 'B'),
array('id' => 789, 'name' => 'C'),
);
$ids = array_map(function ($name) {
return $name['id'];
}, $names);
var_dump($ids);
// output
array(3) {
[0] => int(123)
[1] => int(456)
[2] => int(789)
}
You can simply use the following syntax if you are unable to upgrade the php version. In that kind of case use if (!function_exists('array_column')) to prevent re-declaration of the function which may occur on version upgrade.
Description From php.net
array_column() returns the values from a single column of the array, identified by the column_key. Optionally, you may provide an index_key to index the values in the returned array by the values from the index_key column in the input array.
/* Function array_column equivalent to php's array_column */
if (!function_exists('array_column'))
{
function array_column(array $array, $column_key, $index_key = NULL)
{
if (isset($array))
{
$return = array();
foreach ($array as $a)
{
if ($index_key)
{
if (!isset($a[$index_key]))
{
return array();
}
else
{
$return[$a[$index_key]] = $a[$column_key];
}
}
else
{
$return[] = $a[$column_key];
}
}
return $return;
}
return array();
}
}
Here are some examples taken from PHP.NET
<?php
$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',
)
);
$first_names = array_column($records, 'first_name');
print_r($first_names);
?>
It will give below output:
Array
(
[0] => John
[1] => Sally
[2] => Jane
[3] => Peter
)
Get column of last names from recordset, indexed by the "id" column
<?php
$last_names = array_column($records, 'last_name', 'id');
print_r($last_names);
?>
It will give you output as below:
Array
(
[2135] => Doe
[3245] => Smith
[5342] => Jones
[5623] => Doe
)
$simple = [];
foreach ($stores as $store){
$simple[] = $store['id'];
}

Categories