how get each single column data from php multidimensional array? - php

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

Related

Finding duplicate values in a multidimensional array for Search Method

My Code was :
$data = array();
foreach ($table as $key => $var) {
$data[] = ['id' => $var->id, 'value' => $var->designation];
}
My Data array should be like this
array (
0 => array (
'id' => 27,
'value' => 'laravel',
),
1 => array (
'id' => 1,
'value' => 'laravel tester',
),
2 => array (
'id' => 22,
'value' => 'laravel developer',
),
3 => array (
'id' => 23,
'value' => 'laravel casts',
),
4 => array (
'id' => 24,
'value' => 'laravel developer',
),
)
I need only one value i tried all the php core library function output:
array (
0 =>
array (
'id' => 27,
'value' => 'laravel',
),
1 => array (
'id' => 1,
'value' => 'laravel tester',
),
2 => array (
'id' => 23,
'value' => 'laravel casts',
),
3 => array (
'id' => 24,
'value' => 'laravel developer',
),
)
Based on the name only i need to remove duplicate bacause in my search bar it shows repeated mode.
You can use array_unique, wich saves indexes, and get the result by array_intersect_key
$temp = array_unique(array_column($arr, 'value'));
$res = array_intersect_key($arr, $temp);
print_r($res);

Multidimensional indexed array to associative array depending on column value

I have a multidimensional indexed array. Each element is an associative array with an id column which is unique between elements (its value never repeats within the array).
[indexed] =>Array
(
[0] => Array
(
[id] => john
[name] => John
[age] => 29
),
[1] => Array
(
[id] => peter
[name] => Peter
[age] => 30
),
[2] => Array
(
[id] => harry
[name] => Harry
[age] => 19
)
)
My goal is to convert this array into a multidimensional associative array, indexed by id values.
[indexed] =>Array
(
[john] => Array
(
[id] => john
[name] => John
[age] => 29
),
[peter] => Array
(
[id] => peter
[name] => Peter
[age] => 30
),
[harry] => Array
(
[id] => harry
[name] => Harry
[age] => 19
)
)
My best attempt so far is to loop over array elements and manually create the final array.
$associative = array();
foreach($indexed as $key=>$val) $associative[$val['id']] = $val;
I think it's not the most elegant solution. Is it possible to obtain the same result with built-in (more efficient) functions?
The truth is php DOES offer a single, native function that allows you to replace the outer indexes with the values of a single column. The "magic" is in the 2nd parameter which tells php not to touch the subarray values when assigning the new keys.
Code: (Demo)
$indexed = [
['id' => 'john', 'name' => 'John', 'age' => 29],
['id' => 'peter', 'name' => 'Peter', 'age' => 30],
['id' => 'harry', 'name' => 'Harry', 'age' => 19],
];
var_export(array_column($indexed, null, 'id'));
Output:
array (
'john' =>
array (
'id' => 'john',
'name' => 'John',
'age' => 29,
),
'peter' =>
array (
'id' => 'peter',
'name' => 'Peter',
'age' => 30,
),
'harry' =>
array (
'id' => 'harry',
'name' => 'Harry',
'age' => 19,
),
)
This even works on an array of objects. The end result is an array of objects with new, associative first-level keys. (Demo)
$indexed = [
(object)['id' => 'john', 'name' => 'John', 'age' => 29],
(object)['id' => 'peter', 'name' => 'Peter', 'age' => 30],
(object)['id' => 'harry', 'name' => 'Harry', 'age' => 19],
];
var_export(array_column($indexed, null, 'id'));
Output:
array (
'john' =>
(object) array(
'id' => 'john',
'name' => 'John',
'age' => 29,
),
'peter' =>
(object) array(
'id' => 'peter',
'name' => 'Peter',
'age' => 30,
),
'harry' =>
(object) array(
'id' => 'harry',
'name' => 'Harry',
'age' => 19,
),
)
Here is another way of doing it (assuming $arr is your original array):
$associative = array_combine(array_map(function($item) { return $item['id']; }, $arr), $arr);
But I think using a foreach is still shorter and more readable compare to this.

PHP merge associative arrays by replacing same keys and adding new

I have an array of variable size structured like this (categories is only one of the keys inside data):
print_r($json[123]["data"]["categories"]);
array(
array(
'id' => '2',
'description' => 'Single-player'
),
array(
'id' => '1',
'description' => 'Multi-player'
),
array(
'id' => '9',
'description' => 'Co-op'
),
array(
'id' => '22',
'description' => 'Steam Achievements'
),
array(
'id' => '28',
'description' => 'Full controller support'
)
)
print_r($json[456]["data"]["categories"]);
array(
array(
'id' => '21',
'description' => 'Downloadable Content'
),
array(
'id' => '1',
'description' => 'Multi-player'
)
)
Now, I want to merge these sub-arrays (they can be in variable number) and have all keys added and replaced. I've tried array_merge but it replaces the keys without adding new ones.
In this case I need to obtain this array:
print_r($merged["data"]["categories"]);
array(
array(
'id' => '2',
'description' => 'Single-player'
),
array(
'id' => '1',
'description' => 'Multi-player'
),
array(
'id' => '9',
'description' => 'Co-op'
),
array(
'id' => '22',
'description' => 'Steam Achievements'
),
array(
'id' => '28',
'description' => 'Full controller support'
),
array(
'id' => '21',
'description' => 'Downloadable Content'
)
)
Any help?
Edit:
I think I didn't expressed myself well enough. $json[$id]["data"] has multiple keys I want to merge (categories is just an example). Also the number of $json[$id] keys is variable
Edit2:
The arrays can have duplicate values, and the depth of the keys can be variable. I need to get something like array_merge_recursive() but with same values replaced.
Edit3:
This is the current array. http://pastebin.com/7x7KaAVM I need to merge all keys that have sub-arrays
Try below code:
$json = array(
'123' => array('data' => array('categories' => array(
array(
'id' => '2',
'description' => 'Single-player'
),
array(
'id' => '1',
'description' => 'Multi-player'
),
array(
'id' => '9',
'description' => 'Co-op'
),
array(
'id' => '22',
'description' => 'Steam Achievements'
),
array(
'id' => '28',
'description' => 'Full controller support'
)
))
),
'456' => array('data' => array('categories' => array(
array(
'id' => '21',
'description' => 'Downloadable Content'
)
))
),
);
//print_r($json);
$merged = array();
foreach($json as $j1)
{
foreach($j1 as $j2)
{
foreach($j2 as $key => $j3)
{
foreach($j3 as $j4)
{
$merged[$key][] = $j4;
}
}
}
}
print_r($merged);
Result:
Array
(
[categories] => Array
(
[0] => Array
(
[id] => 2
[description] => Single-player
)
[1] => Array
(
[id] => 1
[description] => Multi-player
)
[2] => Array
(
[id] => 9
[description] => Co-op
)
[3] => Array
(
[id] => 22
[description] => Steam Achievements
)
[4] => Array
(
[id] => 28
[description] => Full controller support
)
[5] => Array
(
[id] => 21
[description] => Downloadable Content
)
)
)
Demo:
http://3v4l.org/X61bE#v430
Try this . To generalize I have added some more arrays.
<?php
$merged_array = array();
$final_array = array();
$json[123]["data"]["categories"] = array(
array(
'id' => '2',
'description' => 'Single-player'
),
array(
'id' => '1',
'description' => 'Multi-player'
),
array(
'id' => '9',
'description' => 'Co-op'
),
array(
'id' => '22',
'description' => 'Steam Achievements'
),
array(
'id' => '28',
'description' => 'Full controller support'
)
);
$json[456]["data"]["categories"] = array(
array(
'id' => '21',
'description' => 'Downloadable Content'
)
);
$json[786]["data"]["categories"] = array(
array(
'id' => '31',
'description' => 'Downloadable Content'
)
);
$json[058]["data"]["categories"] = array(
array(
'id' => '41',
'description' => 'Downloadable Content'
)
);
foreach($json as $key=>$value){
array_push($merged_array,$json[$key]["data"]["categories"]);
}
foreach($merged_array as $value){
foreach($value as $val){
array_push($final_array,$val);
}
}
print_r($final_array);
?>
RESULT
Array
(
[0] => Array
(
[id] => 2
[description] => Single-player
)
[1] => Array
(
[id] => 1
[description] => Multi-player
)
[2] => Array
(
[id] => 9
[description] => Co-op
)
[3] => Array
(
[id] => 22
[description] => Steam Achievements
)
[4] => Array
(
[id] => 28
[description] => Full controller support
)
[5] => Array
(
[id] => 21
[description] => Downloadable Content
)
[6] => Array
(
[id] => 31
[description] => Downloadable Content
)
[7] => Array
(
[id] => 41
[description] => Downloadable Content
)
)

Get column values from an associative array in 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.

Cakephp how to set array values so i can insert two records at same time on my DB?

After fill a form with two adresses: Billing address and personal address(using FormHelper):
...
<div><?php echo $this->Form->input('Contact.1.name'); ?></div></td>
<td> <div>name1</div>
<div><?php echo $this->Form->input('Contact.2.name'); ?></div></td>
...
, i get this array:
'Contact' => array(
(int) 1 => array(
'name' => 'asdf',
'nif' => '123123123',
'address' => 'pcsa',
'zipcode' => '1234',
'street' => 'das'
),
(int) 2 => array(
'name' => 'fsad',
'nif' => '321321321',
'address' => 'asdp',
'zipcode' => '1234',
'street' => 'fas'
)
)
But before i save this array to send data for DB i need to set manually user_id=32 (for example).
Can you give me any suggestion how to do that? so i can get this result:
'Contact' => array(
(int) 1 => array(
'name' => 'asdf',
'nif' => '123123123',
'address' => 'pcsa',
'zipcode' => '1234',
'street' => 'das',
'user_id' => 32 <----------------
),
(int) 2 => array(
'name' => 'fsad',
'nif' => '321321321',
'address' => 'asdp',
'zipcode' => '1234',
'street' => 'fas',
'user_id' => 39 <---------------
)
)
My purpose here is to save data from a form to a table "contacts" for the same user_id. for that reason i can't use user_id as a input form, i need to do that after form and before inserte on my DB. For that reason, how can i set this array with user_id?
Checkout the CakePHP Hash library you can do stuff like
$this->request->data = Hash::insert($this->request->data, 'Contact.{n}.user_id', 123);
Example
$contacts = array(
'Contact' => array(
array(
'name' => 'asdf',
'nif' => '123123123',
'address' => 'pcsa',
'zipcode' => '1234',
'street' => 'das'
),
array(
'name' => 'fsad',
'nif' => '321321321',
'address' => 'asdp',
'zipcode' => '1234',
'street' => 'fas'
)
)
);
$contacts = HASH::insert($contacts, 'Contact.{n}.user_id', 123);
pr($contacts);
Output
Array
(
[Contact] => Array
(
[0] => Array
(
[name] => asdf
[nif] => 123123123
[address] => pcsa
[zipcode] => 1234
[street] => das
[user_id] => 123
)
[1] => Array
(
[name] => fsad
[nif] => 321321321
[address] => asdp
[zipcode] => 1234
[street] => fas
[user_id] => 123
)
)
)
Try this
foreach ($this->request->data['Contact'] as &$contact) {
$contact['user_id'] = 32;
}
thanks a lot for your suggestions, but this is the final solution for me.
$this->request->data['Contact'][1]['user_id'] = 32;
$this->request->data['Contact'][2]['user_id'] = 39;
Simply as that, i didn't see it because i was debugging wrong.

Categories