Get all value from array - php

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

Related

Set some array value as key of an array

I have tried this line of code to display an array:
foreach($users_array as $value){
echo "<pre>";
print_r($value);
}
Which display this kind of array.
Array
(
[auto_id] => 45
[id] => 20151116
[name] => Peter 2
[department] =>
[position] =>
[rate] => 300
[date_added] => 2017-07-26 09:31:44
)
Array
(
[auto_id] => 80
[id] => 20160410
[name] => John 2
[department] =>
[position] =>
[rate] => 400
[date_added] => 2017-07-26 09:31:48
)
Now what I wanted to do is to make the id of employee to be the key of an array and make them as one multi-dimensional array.
Example output should be like this:
Array
(
[20151116] => Array
(
[auto_id] => 45
[id] => 20151116
[name] => Peter 2
[department] =>
[position] =>
[rate] => 300
[date_added] => 2017-07-26 09:31:44
)
[20160410] => Array
(
[auto_id] => 80
[id] => 20160410
[name] => John 2
[department] =>
[position] =>
[rate] => 400
[date_added] => 2017-07-26 09:31:48
)
)
Any help is appreciated. Thank you.
It's probably easiest to make a new array the you output directly, and loop over the existing array, setting the id as an index of the new array:
<?php
$newArray = array();
foreach($users_array as $value) {
$newArray[$value["id"]] = $value;
print_r($newArray);
}
Hope this helps! :)
Here you need to change
$final = array();
foreach($users_array as $value){
$final[$value["id"]] = $value;
}
echo "<pre>";
print_r($final);
Use the function array_column() and array_combine(), like this:
$employee_id = array_column($users_array,'id');
$users_array = array_combine($employee_id,$users_array);

PHP array merge, remove [u]

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

In PHP, how can I iterate through an array and change the key dynamically?

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

changing the key description in php array issue

I have an array like the one below
Array
(
[0] => Array
(
[name] => Alex
[age] => 30
[place] => Texas
)
[1] => Array
(
[name] => Larry
[age] => 28
[place] => Memphis
)
)
How would I change the key names? Like "name" to "firstname", "age" to "years", "place" to "address"?
Use a foreach loop to iterate over your array, and then use array_combine in conjunction with array_values() to create the new array:
$keys = array('firstname', 'years', 'address');
foreach ($array as & $subarr) {
$subarr = array_combine($keys, array_values($subarr));
}
print_r($array);
Output:
Array
(
[0] => Array
(
[firstname] => Alex
[years] => 30
[address] => Texas
)
[1] => Array
(
[firstname] => Larry
[years] => 28
[address] => Memphis
)
)
Online demo
array_map is your friend,
$users = array_map(function($user) {
return array(
'firstname' => $user['name'],
'years' => $user['age'],
'location' => $user['place']
);
}, $users);
DEMO.
I believe that the only way to do this is to create a new array and assign each value with old key to value with new key.
<?php
//$originalArray is array from the question.
for($i=0; $i<=count($originalArray); $i++){
$originalArray[$i] = rekeyArray($originalArray[$i]);
}
function rekeyArray($a){
$result = array();
if(isset($a['name']))
$result['firstname'] = $a['name'];
if(isset($a['age']))
$result['years'] = $a['age'];
if(isset($a['place']))
$result['address'] = $a['place'];
return $result;
}
?>

Array in array with foreach

I have a new PHP problem. I have 2 arrays, and I want a third array which is a combination of the first 2. The first array, $arr1, is something like this:
Array (
[0] => name [1] => age
)
The second array, $arr2, is something like this:
Array (
[0] => Array( [0] => Dave [1] => 20 )
[1] => Array( [0] => Steve [1] => 25 )
[2] => Array( [0] => Ace [1] => 23 )
)
And my idea is to create a new array, called $arr3, which should like this:
Array (
[0] => Array( [name] => Dave [age] => 20 )
[1] => Array( [name] => Steve [age] => 25 )
[2] => Array( [name] => Ace [age] => 23 )
)
Can anyone tell me how to do this?
$arr3 = array();
foreach ($arr2 as $person) {
$arr3[] = array_combine($arr1, $person);
}
foreach($arr2 as $subArray){
foreach($subArray as $i=>$val){
$arr3[$arr1[$i]] = $val;
}
}

Categories