Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 10 months ago.
Improve this question
I have an array like this
$users = array(
[0] => array('Id' => 3, 'Name' => 'Bob'),
[1] => array('Id' => 8, 'Name' => 'Alice'),
)
and I want to pull the Ids 'up' one level so that the final array is:
$usersById = array(
[3] => array('Id' => 3, 'Name' => 'Bob'),
[8] => array('Id' => 8, 'Name' => 'Alice'),
)
The Id values are unique.
Is there a native PHP way to do this? The code I'm currently using is:
$usersById = array();
foreach ($users as $key => $value)
{
$usersById[$value['Id']] = $value;
}
This works, but is not terribly elegant.
Modern answer (requires PHP 5.5)
The new function array_column is very versatile and one of the things it can do is exactly this type of reindexing:
// second parameter is null means we 're just going to reindex the input
$usersById = array_column($users, null, 'Id');
Original answer (for earlier PHP versions)
You need to fetch the ids from the sub-arrays with array_map, then create a new array with array_combine:
$ids = array_map(function($user) { return $user['Id']; }, $users);
$users = array_combine($ids, $users);
The code above requires PHP >= 5.3 for the anonymous function syntax, but you can also do the same (albeit it will look a bit uglier) with create_function which only requires PHP >= 4.0.1:
$ids = array_map(create_function('$user', 'return $user["Id"];'), $users);
$users = array_combine($ids, $users);
See it in action.
You could use the array_reduce() function, like:
$usersById = array_reduce($users, function ($reduced, $current) {
$reduced[$current['Id']] = $current;
return $reduced;
});
However, it's no more elegant than a foreach.
I think using foreach is much more elegant. Maybe you just only want to write it differently:
$keyed = array();
foreach($users as $w) $keyed[$w['Id']] = $w;
In case you want to replace the existing array, foreach is not that flexible indeed. But maybe the following is some sort of alternative:
$users = function($key) use ($users)
{
foreach($users as $v) $keys[] = $v[$key];
return array_combine($keys, $users);
};
$users = $users('Id');
It allows the callback to accept parameters, e.g. the name of the key which should be used to create the new keys from.
And one more variant using array_walk:
$usersById = array();
array_walk($users, function($val) use (&$usersById) {
$usersById[$val['Id']] = $val;
});
Related
This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 9 months ago.
I have this array. I want to get the array values to a same array,how can I achieve that?
Array
(
[0] => Array
(
[referrer_id] => usr157
)
[1] => Array
(
[referrer_id] => usr42
)
)
I want this array to be
array("usr157", "usr42")
use array_walk_recursive to achieve the result as follows
<?php
$main = [];
$ref =
[
[
"referrer_id" => "usr157"
],
[
"referrer_id" => "usr42"
]
];
array_walk_recursive($ref, function ($item, $key) use(&$main) {
$main[] = $item;
} );
print_r($main);
You can check that out here
You can just access the array components like this:
// The next line just recreates your example array into a variable called $x:
$x = array(array('referrer_id' => 'usr157'), array('referrer_id' => 'usr42'));
$result = array($x[0]['referrer_id'], $x[1]['referrer_id']);
print_r($result); //print the result for correctness checking
$result will be the output array you wanted.
Using $x[0], you refer the first element of your input array (and hence, $x[1] the second one, ...). Adding ['referrer_id'] will access its referrer_id key. The surrounding array(...) puts the values into an own array.
You can "automate" the whole thing in case you have a bigger input array using a loop.
You may use array_column to achieve that
$flatten = array_column($array, 'referrer_id');
You can also use array_map and array_values together.
$array = [
[
"referrer_id" => "usr157"
],
[
"referrer_id" => "usr42"
]
];
$flatten = array_map(function($item) {
return array_values($item)[0];
}, $array);
var_dump($flatten);
Also you can use the one-liner if you're using latest version of php that support arrow function
$flatten = array_map(fn($item) => array_values($item)[0], $array);
Or without array_values, you may specify the key
$flatten = array_map(fn($item) => $item['referrer_id'], $array);
You can see the demo here
This question already has answers here:
Merge row data from multiple arrays
(6 answers)
Closed 4 months ago.
How can I combine 2 arrays ... :
$array_1 = [['title' => 'Google'], ['title' => 'Bing']];
$array_2 = [['link' => 'www.example1.com'], ['link' => 'www.example2.com']];
In order to get ... :
$array_3 = [
['title' => 'Google', 'link' => 'www.example1.com'],
['title' => 'Bing', 'link' => 'www.example2.com']
];
I guess $array_3 should be structured the following way in order to get :
Final result:
Google - See website
Bing - See website
The function to get the final result:
function site_and_link($array_3) {
foreach ($array_3 as $a) {
echo $a['title'] . " - See website</br>";
}
}
What is the missing step to arrange $array_3?
You can use a simple foreach loop and array_merge to merge both subarrays.
<?php
$result = [];
foreach($array_1 as $index => $val){
$result[] = array_merge($val,$array_2[$index]);
}
print_r($result);
It is indirect programming to use a loop to merge-transpose your array data, then another loop to print to screen.
Ideally, you should try to merge these structures earlier in your code if possible (I don't know where these datasets are coming from, so I cannot advise.)
Otherwise, leave the two arrays unmerged and just write a single loop to print to screen. Because the two arrays are expected to relate to each other by their indexes, there will be no risk of generating Notices.
Since I am typing this out, I'll take the opportunity to reveal a couple of useful tricks:
You can unpack your single-element subarrays by using array syntax with a static key pointing to the targeted variable in the foreach().
Using printf() can help to cut down on line bloat/obfuscation caused by concatenation/interpolation. By writing placeholders (%s) into the string and then passing values for those placeholders in the trailing arguments, readablity is often improved.
Code: (Demo)
$sites = [['title' => 'Google'], ['title' => 'Bing']];
$links = [['link' => 'www.example1.com'], ['link' => 'www.example2.com']];
foreach ($sites as $index => ['title' => $title]) {
printf(
'%s - See website</br>',
$title,
$links[$index]['link']
);
}
Output:
Google - See website</br>
Bing - See website</br>
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
hi there I designed a blog page....per page 10 artilcs!
I want return biggest time (stamptime) for each 10 articls (like publish up time,publish down time,created,modifed time) to create meta tag.
so I have a list of 10 artiles (each article is an array with all parameters like date,content,title,...) that I saved them in $list array.
like this:
$list = array ( array('id' => '1', 'modifed ' => '123123123' ...
I want return biggest value...the articls are mixed.
I use this code:
$data['created'] = array_reduce($list, function ($a, $b) {
return #$a['created'] > $b['created'] ? $a : $b ;
$data['modified'] = array_reduce($list, function ($a, $b) {
return #$a['modified'] > $b['modified'] ? $a : $b ;
$data['publish_up'] = array_reduce($list, function ($a, $b) {
return #$a['publish_up'] > $b['publish_up'] ? $a : $b ;
$data['publish_down'] = array_reduce($list, function ($a, $b) {
return #$a['publish_down'] > $b['publish_down'] ? $a : $b ;
My only worry is it maybe has a bad effect in my loading page.
do you think this code is Optimized?
Here's my solution. But as Sven said, you need to be more specific.
//fake data
$data = array(
array('id' => 1, 'upd' => 111, 'created' => 333),
array('id' => 2, 'upd' => 1203, 'created' => 43),
array('id' => 3, 'upd' => 144, 'created' => 533),
);
// here, I'm first computing the max of each column
// I place the results in an array and returns ultimately the max
echo max(
array(
max(array_column($data, 'upd')),
max(array_column($data, 'created'))
)
);
// here, it returns 1203, as there is an element "upd" with value 1203
If you do not have php 5.5, you have the following options:
use a php implementation that matches the behaviour of php 5.5 : https://github.com/ramsey/array_column/blob/master/src/array_column.php
upgrade to php 5.5 ;)
implements the function with map
The option 3 leads to the following code:
echo max(
array(
max(array_map(function($arr){return $arr['upd'];}, $data)),
max(array_map(function($arr){return $arr['created'];}, $data)),
)
);
Using the numbers from $ids, I want to pull the data from $nuts.
So for example:
$ids = [0,3,5]; // 0 calories, 3 sugar, 5 fat
$nuts = [
'calories' => 'cal',
'protein' => 'pro',
'carbohydrate' => 'car',
'sugar' => 'sug',
'fiber' => 'fib',
'fat' => 'fat',
];
$returnData = [
'calories' => 'cal',
'sugar' => 'sug',
'fat' => 'fat',
];
I could loop through each $ids number with a foreach(); but I'm curious to see if there is a better method than this?
$newNuts = array_values(array_flip($nuts));
foreach($ids as $i)
$returnData[$newNuts[$i]] = $nuts[$newNuts[$i]];
I did some work and realized, you don't need array_flip, array_values is fine.
$num_nuts = array_values ($nuts);
for ($z=0; $z<sizeof($ids); $z++) {
echo $num_nuts[$ids[$z]];
}
Just 1 more line of code, but I think it does the job. I think mine is going to be faster because the array_flip basically exchanges all keys with their associated values in an array, which is not what I am doing. It's actually one less pain.
I am simply converting the original array to a new one by index and simply looping upon it. Also, not the elegant way to use the power of PHP available to us, but works just fine. array_flip is O(n), but I think better not use it for larger data-sets.
How about a simple array_slice?
$result = array();
foreach ($ids as $i) {
$result += array_slice($nuts, $i, 1, true);
}
No need to create a copy of the array.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
The following is a print_r of an array converted from an XML document:
Array
(
[layer1] => Array
(
[item] => Array
(
[item-id] => 1886731
[item-name] => Bad Dog
[category] => pets
[link] = http://www.baddog.com/
)
[total-matched] => 1
)
)
For the above array, sizeof($myarray[layer1][item]) should return 1, but it returns 4. I get the correct number of "item" items if there are more than one of them. The same error happens regardless of whether I use "sizeof" or "count". How do I get PHP to return "1" when there is only one item?
Consequently, if there is one item, I can't access item-name using array[layer1][item][0][item-name], I have to use array[layer1][item][item-name].
There is a world of difference between:
$array1 = array(
'layer1' => array(
'item' => array(
'0' => array(
'item-id' => 123123,
'item-name' => 'Bad Dog',
'category' => 'pets',
'link' => 'http://www.baddog.com/',
),
)
)
);
and:
$array2 = array(
'layer1' => array(
'item' => array(
'item-id' => 123123,
'item-name' => 'Bad Dog',
'category' => 'pets',
'link' => 'http://www.baddog.com/',
)
)
);
Basically they are different structures, and PHP is correct to return the following:
count($array1['layer1']['item']);
/// = 1 (count of items in the array at that point)
count($array2['layer1']['item']);
/// = 4 (count of items in the array at that point)
If you wish to get a count for ['layer1']['item'] that makes sense to your app, you will always need ['layer1']['item'] to be an array containing multiple array structures... i.e. like $array1 above. This is the reason why I ask what is generating your array structure because - whatever it is - it is basically intelligently stacking your array data depending on the number of items, which is something you don't want.
Code that can cause an array to stack in this manner normally looks similar to the following:
/// $array = array(); /// this is implied (should be set elsewhere)
$key = 'test';
$newval = 'value';
/// if we are an array, add a new item to the array
if ( is_array($array[$key]) ) {
$array[$key][] = $newval;
}
/// if we have a previous non-array value, convert to an array
/// containing the previous and new value
else if ( isset($array[$key]) ) {
$array[$key] = array($array[$key],$newval);
}
/// if nothing is set, set the $newval
else {
$array[$key] = $newval;
}
Basically if you keep calling the above code, and tracing after each run, you will see the following structure build up:
$array == 'value';
then
$array == array(0 => 'value', 1 => 'value');
then
$array == array(0 => 'value', 1 => 'value', 2 => 'value');
It's the first step in this process that is causing the problem, the $array = 'value'; bit. If you modify the code slightly you can get rid of this:
/// $array = array(); /// this is implied (should be set elsewhere)
$key = 'test';
$newval = 'value';
/// if we are an array, add a new item to the array
if ( is_array($array[$key]) ) {
$array[$key][] = $newval;
}
/// if nothing is set, set the $newval as part of an subarray
else {
$array[$key] = array($newval);
}
As you can see all I've done is delete the itermediate if statement, and made sure when we discover no initial value is set, that we always create an array. The above will create a structure you can always count and know the number of items you pushed on to the array.
$array == array(0 => 'value');
then
$array == array(0 => 'value', 1 => 'value');
then
$array == array(0 => 'value', 1 => 'value', 2 => 'value');
update
Ah, I thought so. So the array is generated from XML. In this case I gather you are using a predefined library to do this so modifying the code is out of the question. So as others have already stated, your best bet is to use one of the many XML parsing libraries available to PHP:
http://www.uk.php.net/simplexml
http://www.uk.php.net/dom
When using these systems you retain more of an object structure which should be easier to count. Both the above also support xpath notation which can allow you to count items without even having to grab hold of any of the data.
update 2
Out of the function you've given, this is the part that is causing your arrays to stack in the manner that is causing the problem:
$children = array();
$first = true;
foreach($xml->children() as $elementName => $child){
$value = simpleXMLToArray($child,$attributesKey, $childrenKey,$valueKey);
if(isset($children[$elementName])){
if(is_array($children[$elementName])){
if($first){
$temp = $children[$elementName];
unset($children[$elementName]);
$children[$elementName][] = $temp;
$first=false;
}
$children[$elementName][] = $value;
}else{
$children[$elementName] = array($children[$elementName],$value);
}
}
else{
$children[$elementName] = $value;
}
}
The modification would be:
$children = array();
foreach($xml->children() as $elementName => $child){
$value = simpleXMLToArray($child,$attributesKey, $childrenKey,$valueKey);
if(isset($children[$elementName])){
if(is_array($children[$elementName])){
$children[$elementName][] = $value;
}
}
else{
$children[$elementName] = array($value);
}
}
That should stop your arrays from stacking... however if you have any other part of your code that was relying on the previous structure, this change may break that code.