I have an array given below...
Array
(
[0] => Array
(
[u] => Array
(
[id] => 396
[first_name] => Gyan
[last_name] => sharma
[email] => gyan#gmail.com
[phone_number] =>
)
)
[1] => Array
(
[u] => Array
(
[id] => 589
[first_name] => deep
[last_name] => sharma
[email] => deep#gmail.com
[phone_number] =>
)
)
)
I just want to remove the [u] from each array, Like array given below.
Array
(
[0] => Array
(
[id] => 396
[first_name] => Gyan
[last_name] => sharma
[email] => gyan#gmail.com
[phone_number] =>
)
[1] => Array
(
[id] => 589
[first_name] => deep
[last_name] => sharma
[email] => deep#gmail.com
[phone_number] =>
)
)
I can do this by foreach() loop, but it is lengthy process..
Can anyone tell me the shortest way for this.
Help me.
Thanks in Advance.
You have to use array_column() like below:-
$array = array_column($array,'u');
Output:- https://eval.in/833258
Without foreach :
$new_array = array_map(function($element){
return $element['u'];
},$old_array);
var_dump($new_array);
With foreach:
$new_array = [];
foreach($old_array as $value){
$new_array[] = $value['u']
}
var_dump($new_array)
$old_array is your array and $new_array is array you want
You can use array_map and array_shift for this.
$a[0]['u']['name'] = "a";
$a[0]['u']['id'] = "a";
$a[1]['u']['name'] = "a";
$a[1]['u']['id'] = "a";
$a = array_map('array_shift', $a);
print_r($a,1);
Here is the working example
Related
I don't know How to get "name" value from all array ?
Any one please help me
I have some array like this
Array
(
[0] => Array
(
[name] => Jon
[phone] =>
[relation] => wife
[age] => 43
[relative_education] => 4
)
[1] => Array
(
[name] => John
[phone] => 123456789
[relation] => son
[age] => 24
[relative_education] => 10
)
[2] => Array
(
[name] => Amy
[phone] => 456789123
[relation] => Son
[age] => 21
[relative_education] => 12
)
)
Thanks in advance.
Try this
$name = array_column($data, 'name');
print_r($name);
$names = array_map(function($user) {
return $user['name'];
}, $users);
You can loop the array to get the values.
If your array is $arr then use the below code to get the values
//$arr = YOUR ARRAY
$names = array();
foreach($arr as $val) {
$names[] = $val['name'];
}
print_r($names);
This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 5 months ago.
I need to convert a PHP array that I'm getting from a form submission, so that I can use it more usefully in a db.
Array
(
[first_name] => Array
(
[0] => Ben
[1] => Tom
[2] => Sarah
)
[last_name] => Array
(
[0] => Wills
[1] => Main
[2] => Bliss
)
[email] => Array
(
[0] => ben.wills#argh.com
[1] => tommain#argh.com
[2] => sbliss#argh.com
)
)
to:
Array
(
[0] => Array
(
[first_name] => Ben
[last_name] => Wills
[email] => ben.wills#argh.com
)
[1] => Array
(
[first_name] => Tom
[last_name] => Main
[email] => tommain#argh.com
)
[2] => Array
(
[first_name] => Sarah
[last_name] => Bliss
[email] => sbliss#argh.com
)
)
How can I change the values' key paths so that the first level keys and the second level keys are swapped?
The solution using array_keys, array_values, array_map, call_user_func_array and array_combine functions:
$keys = array_keys($arr); // supposing $arr is your initial array
$data = call_user_func_array("array_map", array_merge([null], array_values($arr)));
$result = array_map(function($v) use($keys){
return array_combine($keys, $v);
}, $data);
print_r($result);
The output:
Array
(
[0] => Array
(
[first_name] => Ben
[last_name] => Wills
[email] => ben.wills#argh.com
)
[1] => Array
(
[first_name] => Tom
[last_name] => Main
[email] => tommain#argh.com
)
[2] => Array
(
[first_name] => Sarah
[last_name] => Bliss
[email] => sbliss#argh.com
)
)
Use the below code. Hope at least this gives some idea how to proceed :)
$array = array(
'first_name' => array('Ben','Tom','Sarah'),
'last_name' => array('Wills','Main','Bliss'),
'email' => array('ben.wills#argh.com','tommain#argh.com','sbliss#argh.com')
);
// loop the array
foreach($array as $key=>$value){
foreach($value as $k=>$v){
// use the first loop key here
$new_array[$k][$key] = $v;
}
}
print_r($new_array);
Out Put:
Array
(
[0] => Array
(
[first_name] => Ben
[last_name] => Wills
[email] => ben.wills#argh.com
)
[1] => Array
(
[first_name] => Tom
[last_name] => Main
[email] => tommain#argh.com
)
[2] => Array
(
[first_name] => Sarah
[last_name] => Bliss
[email] => sbliss#argh.com
)
)
Doing a transform while retaining the key names can be achieved quite easily using PHP's MultipleIterator
$data = array(
'first_name' => array(
0 => 'Ben',
1 => 'Tom',
2 => 'Sarah',
),
'last_name' => array(
0 => 'Wills',
1 => 'Main',
2 => 'Bliss',
),
'email' => array(
0 => 'ben.wills#argh.com',
1 => 'tommain#argh.com',
2 => 'sbliss#argh.com',
),
);
$mi = new MultipleIterator(MultipleIterator::MIT_NEED_ALL | MultipleIterator::MIT_KEYS_ASSOC);
foreach($data as $key => $column) {
$mi->attachIterator(new ArrayIterator($column), $key);
}
$newData = [];
foreach($mi as $row) {
$newData[] = $row;
}
var_dump($newData);
Demo
I have an array $people. When I do print_r($people), I get the following results:
[people] => Array
(
[500] => Array
(
[firstName] => Fred
[age] => 19
)
[501] => Array
(
[firstName] => Bob
[age] => 12
)
[502] => Array
(
[firstName] => Steve
[age] => 52
)
)
I want to change all the keys to look more "normal", starting at 0, then 1, 2 etc. How can I achieve this? To clarify, I want the resulting array to look like this:
[people] => Array
(
[0] => Array
(
[firstName] => Fred
[age] => 19
)
[1] => Array
(
[firstName] => Bob
[age] => 12
)
[2] => Array
(
[firstName] => Steve
[age] => 52
)
)
The built-in function array_values() will take only the values from an array, ignoring the keys and instead returning the array renumbered from zero.
$people = array_values($people);
Simply like this:
foreach ($people as $value) {
$people_new[] = $value;
}
Try this
$people['people'] = array_values($people['people']);
print_r($people);
I want to merge two arrays into one array as follows,
Array1:
Array
(
[0] => Array
(
[id] => 3
[sku] => KOG456
[cart_id] => 2
[name] => Young Money
[slug] => young-money
[route_id] => 47
[description] =>
This is test song
[excerpt] =>
[saleprice] => 90.00
[related_products] =>
[images] => {"1c6b0883fc94c5f644497ec488cdf8cb":{"filename":"1c6b0883fc94c5f644497ec488cdf8cb.jpg","alt":"Test","caption":"","primary":true}}
[seo_title] =>
[meta] =>
[enabled] => 1
)
)
Array2:
Array
(
[0] => Array
(
[filename] => Beethovens_Symphony_No._9_(Scherzo).wma
[title] => Young Money
[size] => 599.26
)
)
Expected array result is:
Array
(
[0] => Array
(
[id] => 3
[sku] => KOG456
[cart_id] => 2
[name] => Young Money
[slug] => young-money
[route_id] => 47
[description] =>
This is test song
[excerpt] =>
[saleprice] => 90.00
[related_products] =>
[images] => {"1c6b0883fc94c5f644497ec488cdf8cb":{"filename":"1c6b0883fc94c5f644497ec488cdf8cb.jpg","alt":"Test","caption":"","primary":true}}
[seo_title] =>
[meta] =>
[enabled] => 1
[filename] => Beethovens_Symphony_No._9_(Scherzo).wma
[title] => Young Money
[size] => 599.26
)
)
How to merge these array elements into one array element ?
foreach ($origArray as $key => &$subArray)
$subArray += $arrayToBeAdded[$key];
Where $origArray is your array which is to be merged into and $arrayToBeAdded the array you merge into.
User array_merge_recursive():
$final = array_merge_recursive($array1, $array2);
Try this little known overload of the + operator for arrays:
$result = $array1[0] + $array2[0]
Use function array_merge($array1[0], $array2[0]) . Following is the example for the same
$array1 = array(0=>array('1'=>1,'2'=>2,'3'=>3));
$array2 = array(0=>array('4'=>4,'5'=>5,'6'=>6));
$result[0] = array_merge($array1[0],$array2[0]);
echo '<pre>';
print_r($result);
Since you have unique keys, you could use something as simple as the + operator (union)...
For example:
$arr1 = [1=>'testing',2=>'stack',3=>'overflow'];
$arr2 = [4=>'something',5=>'else',6=>'here'];
$arr3 = $arr1 + $arr2;
print_r($arr3);
Results:
Array ( [1] => testing [2] => stack [3] => overflow [4] => something [5] => else [6] => here )
For this php has multiple functions. You can use $arrays = array_combine($array1, $array2);.
PHP.net - array_combine
Hope it helped!
I've been trying this all day!
How would I convert the top multidimensional array into the bottom.
Array (
[0] => Array ( [id] => 34 [email] => a#example.com )
[1] => Array ( [id] => 34 [email] => b#example.com )
[2] => Array ( [id] => 33 [email] => c#example.com )
[3] => Array ( [id] => 33 [email] => d#example.com )
[4] => Array ( [id] => 33 [email] => e#example.com )
)
Array (
[0]=>Array ([id] => 34 [email] => Array ([0]=> a#example.com [1]=>b#example.com )
[1]=>Array ([id] => 33 [email] => Array ([0]=> c#example.com [1]=>d#example.com [2]=>e#example.com)
)
Many thanks.
$new_array = array();
foreach ($orig_array as $child) {
$new_array[$child['id']][] = $child['email'];
}
$final_array = array();
foreach($new_array as $child) {
$final_array[] = $child;
}
The first loop produces an array keyed off the id fields, and simply pushes each email address onto it. The second loop then takes that intermediate array and wraps another array around it for the 0,1,etc... keys.
Would not just using keys in order to store IDs be an easier way to do that? Like this:
Array (
[34]=>Array ([email] => Array ([0]=> a#example.com [1]=>b#example.com )
[33]=>Array ([email] => Array ([0]=> c#example.com [1]=>d#example.com [2]=>e#example.com)
)
Then grouping emails would become a trivial task.