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);
Related
[original:protected] => Array
(
[user_id] => 65751
[social_id] =>
[parent_id] =>
[org_id] => 1
[type] => 3
[s_id] => 1
[role_id] => 0
[active] => 1
[name] => RX
[first_name] => JJ
[last_name] => DKL
[email] => first#testmail.com
[secondary_email] =>
[username] => cLvcyUr2
)
[1] => User Object
(
[user_id] => 82197
[social_id] =>
[parent_id] =>
[org_id] => 1
[type] => 2
[s_id] => 1
[role_id] => 0
[active] => 1
[name] => sec
[first_name] => XX
[last_name] => J3
[email] => first#testmail.com
[secondary_email] =>
[username] => VfTqXyvJ
)
How to transform the array data mean to keep only two email and username rest should remove
Array (
[0] => Array (
[email] => first#testmail.com
[username] => cLvcyUr2
)
[1] => Array (
[email] => first#testmail.com
[username] => VfTqXyvJ
)
)
How could this possible i do not to unset data one by one it should automatically unset and pick only two value
Did you try something like that :
$index = array_search('user_id', $array);
unset($array[$index]);
Do that for all key you want to remove.
$newArray = [];
foreach ($oldArray as $item) {
$arr = [];
$arr['email'] = $item->email;
$arr['username'] = $item->username;
$newArray[] = $arr;
}
Why not create an empty array and just copy the values you need? Trying to unset everything in your array is a lot of work and is not really maintainable, out of experience.
Note that your pasted code contains both an array and object, but your question is about an array only.
$newArray = [];
foreach ($oldArray as $item) {
$newArray[] = [
'email' => $item->email,
'username' => $item->username
]; // If $item is object
$newArray[] = [
'email' => $item['email'],
'username' => $item['username']
]; // If $item is array
}
var_dump($newArray);
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);
I have a array that looks like this
Array
(
[11] => Array
(
[1] => Array
(
[type] => OPT
[panel] => 1
[loop] => 1
[number] => 1
[zone] => 1
[value] => 38
)
)
[12] => Array
(
[1] => Array
(
[type] => OPT
[panel] => 1
[loop] => 2
[number] => 1
[zone] => 19
[value] => 40
)
)
)
I want to delete the first dimension so that it looks like this
Array
(
[0] => Array
(
[type] => OPT
[panel] => 1
[loop] => 1
[number] => 1
[zone] => 1
[value] => 38
)
[1] => Array
(
[type] => OPT
[panel] => 1
[loop] => 2
[number] => 1
[zone] => 19
[value] => 40
)
)
How do I do that?
Sorry but I have to put in some test or the compiler won't let me post the code.
You can simply use call_user_func_array as
$result = call_user_func_array('array_merge',$your_array);
Demo
$short = array();
foreach($long as $k=>$v) {
$short[] = $array[$k][0];
}
var_dump($short);
provided that you have only one element in 2nd level
This should work for you:
Just go through each sub array and array_merge() the arrays with your $result together, e.g.
<?php
$result = [];
foreach($arr as $v)
$result = array_merge($result, $v);
print_r($result);
?>
Demo
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 have the following code
$List2 = json_decode(file_get_contents("https://___URL____&format=json"),true);
the following works perfectly
echo '<pre>';print_r($List2);echo '</pre>';
and produces e.g.
Array
(
[0] => Array
(
[uid] => 123456
[name] => John Williams
[pic_square] => http://nc4/565228_799523_q.jpg
[birthday_date] => 07/31/1987
)
[1] => Array
(
[uid] => 123789
[name] => Jane Thompson
[pic_square] => http://profile.ak.fbcdn.net/785505233_1702140670_q.jpg
[birthday_date] => 07/31/1983
)
[2] => Array
(
[uid] => 456789
[name] => John Gaffney
[pic_square] => http://profet/hprofile-ak-snc4/3717297628_q.jpg
[birthday_date] => 07/31/1965
)
[3] => Array
(
[uid] => 987654
[name] => Johnny Illand
[pic_square] => http://c4/41766_14329_q.jpg
[birthday_date] => 07/31/1958
)
I want to run a foreach to print the result somewhat neater obviously, so i'm trying the following:
$data = $List2['data'] ;
foreach ($data as $nr => $friends){
echo $friends[name].' - ';
}
But I get
Warning: Invalid argument supplied for foreach()
I'm stumped and it's probably something easy!
Try with:
foreach ($List2 as $element)
echo $element[name].' - ';
In this example the 'data' key does not exists
$data = $List2;