I have the following array (contents retrieved from a DB):
array (size=4)
0 =>
array (size=2)
'number' => string 'one' (length=3)
'name' => string 'Billy' (length=5)
1 =>
array (size=2)
'number' => string 'two' (length=3)
'name' => string 'Mariah' (length=6)
2 =>
array (size=2)
'number' => string 'three' (length=5)
'name' => string 'Cindy' (length=5)
3 =>
array (size=2)
'number' => string 'four' (length=4)
'name' => string 'Daniel' (length=6)
I need to create an array as follows:
$info = array('one' => 'Billy', 'two' => 'Mariah', 'three' => 'Cindy', 'four' => 'Daniel');
I used a foreach loop to construct the desired array:
$info = array();
foreach ($result as $row) {
$info[] = array($row['number'] => $row['name']);
}
and a var_dump() gives me this instead:
array (size=4)
0 =>
array (size=1)
'one' => string 'Billy' (length=5)
1 =>
array (size=1)
'two' => string 'Mariah' (length=6)
2 =>
array (size=1)
'three' => string 'Cindy' (length=5)
3 =>
array (size=1)
'four' => string 'Daniel' (length=6)
How can I achieve the desired array in PHP? Thanks for your help in advance.
You were close.
$info = array();
foreach ($result as $row) {
$info[$row['number']] =$row['name'];
}
With PHP >= 5.5.0 it's much easier:
$info = array_column($result, 'name', 'number');
Or use the PHP Implementation of array_column()
Related
I need to show a page with links from my database
array (size=5)
0 =>
array (size=3)
'id' => string '1' (length=1)
'name' => string 'apple' (length=5)
'url' => string 'http://iphone7' (length=14)
1 =>
array (size=3)
'id' => string '2' (length=1)
'name' => string 'samsung' (length=7)
'url' => string 'http://samsung' (length=14)
2 =>
array (size=3)
'id' => string '3' (length=1)
'name' => string 'xiaom' (length=5)
'url' => string 'http://xiaomi' (length=13)
3 =>
array (size=3)
'id' => string '6' (length=1)
'name' => string 'sony' (length=4)
'url' => string 'http://sony' (length=11)
4 =>
array (size=3)
'id' => string '7' (length=1)
'name' => string 'nokia' (length=5)
'url' => string 'http://nokia' (length=12)
like this
A
apple <-- it's a link name with url
N
nokia
S
samsung
sony
X
xaomi
in alphabet order with letter (capital)
I know how to do it with ordinary array (just names)
$result = array();
$previous = null;
$i = 0;
foreach ($this->model as $value) {
$firstLetter = substr($value, 0, 1);
if ($previous !== $firstLetter) {
$result[$i] = mb_convert_case($firstLetter, MB_CASE_UPPER, "UTF-
8");
$i++;
}
$result[$i] = $value;
$previous = $firstLetter;
$i++;
}
but i don't understand how can i do it with multidimensional array.
i can sort firstly by "name" attribute, but how to get a first letter to put in new array
[
"A" = > array ('name' => apple,'url' => 'http://...'),
"S" => array ('name' => samsung, 'url' => 'http://...')
]
to show it with foreach operator
I use this function for getting firstletter
$result = array();
$previous = null;
$i = 0;
foreach ($model as $value => $key) {
$firstLetter = substr($key["name"], 0, 1);
if ($previous !== $firstLetter) {
$result[$firstLetter] = $firstLetter;
$result[$firstLetter[$i]] = $key;
}
// something must be here to assign secons array to
$result[$firstLetter
$previous = $firstLetter;
}
and i get
array (size=4)
'a' =>
array (size=3)
'id' => string '1' (length=1)
'name' => string 'apple' (length=5)
'url' => string 'http://iphone7' (length=14)
'n' =>
array (size=3)
'id' => string '7' (length=1)
'name' => string 'nokia' (length=5)
'url' => string 'http://nokia' (length=12)
's' =>
array (size=3)
'id' => string '2' (length=1)
'name' => string 'samsung' (length=7)
'url' => string 'http://samsung' (length=14)
'x' =>
array (size=3)
'id' => string '3' (length=1)
'name' => string 'xiaom' (length=5)
'url' => string 'http://xiaomi' (length=13)
You can use array_multisort
array_multisort( array_column($yourArray, "name"), SORT_ASC, $yourArray );
I have an array based query into MySQL.
If the array that come like this:
array (size=6)
0 =>
array (size=2)
'name' => string 'Login' (length=5)
'y' => string '1' (length=1)
1 =>
array (size=2)
'name' => string 'Printer' (length=7)
'y' => string '2' (length=1)
2 =>
array (size=2)
'name' => string 'Monitor' (length=7)
'y' => string '0' (length=1)
3 =>
array (size=2)
'name' => string 'Computer' (length=8)
'y' => string '0' (length=1)
4 =>
array (size=2)
'name' => string 'Network' (length=7)
'y' => string '0' (length=1)
5 =>
array (size=2)
'name' => string 'Lain Lain' (length=9)
'y' => string '0' (length=1)
How can I make all key 'y' as integer ?
you can use array_map to handle all elements in array
$arr = [
['x' => 'str', 'y' => '1'],
['x' => 'str', 'y' => '2'],
['x' => 'str', 'y' => '3']
];
print_r(array_map(function($el) {
$el['y'] = intval($el['y']);
return $el;
}, $arr));
You can iterate over the array and see if y holds a numeric value and if it does, cast it as an integer:
foreach ($array as $item) {
if (is_numeric($item['y'])) {
$item['y'] = intval($item['y']);
}
}
See a working example here: https://3v4l.org/ABDof
With foreach:
foreach($myArray as $k=>$v){ //iterate in each array
$myArray[$k]['y'] = intval($myArray[$k]['y']); //re-set value type
}
Example
I have this result from var_dump for a multidimensional array:
array (size=6)
'sambalpur.in.net' =>
array (size=2)
'classkey' => string 'indotnet' (length=8)
'status' => string 'available' (length=9)
'sambalpur.com' =>
array (size=2)
'classkey' => string 'domcno' (length=6)
'status' => string 'regthroughothers' (length=16)
'sambalpur.info' =>
array (size=2)
'classkey' => string 'dominfo' (length=7)
'status' => string 'regthroughothers' (length=16)
'sambalpur.net' =>
array (size=2)
'classkey' => string 'dotnet' (length=6)
'status' => string 'regthroughothers' (length=16)
'sambalpur.biz' =>
array (size=2)
'classkey' => string 'dombiz' (length=6)
'status' => string 'available' (length=9)
'sambalpur.in' =>
array (size=2)
'classkey' => string 'dotin' (length=5)
'status' => string 'regthroughothers' (length=16)
Now say I want to shift this specific array to the beginning of array:
'sambalpur.biz' =>
array (size=2)
'classkey' => string 'dombiz' (length=6)
'status' => string 'available' (length=9)
I have tried:
array_unshift($array,array('sambalpur.biz'));
But what I am getting is like this:
array (size=7)
0 =>
array (size=1)
0 => string 'sambalpur.biz' (length=13)
'sambalpur.in.net' =>
array (size=2)
'classkey' => string 'indotnet' (length=8)
'status' => string 'available' (length=9)
'sambalpur.com' =>
array (size=2)
'classkey' => string 'domcno' (length=6)
'status' => string 'regthroughothers' (length=16)
'sambalpur.info' =>
array (size=2)
'classkey' => string 'dominfo' (length=7)
'status' => string 'regthroughothers' (length=16)
'sambalpur.net' =>
array (size=2)
'classkey' => string 'dotnet' (length=6)
'status' => string 'regthroughothers' (length=16)
'sambalpur.biz' =>
array (size=2)
'classkey' => string 'dombiz' (length=6)
'status' => string 'available' (length=9)
'sambalpur.in' =>
array (size=2)
'classkey' => string 'dotin' (length=5)
'status' => string 'regthroughothers' (length=16)
What is the correct way to shift the array?
I thought I had done this before but couldn't find a duplicate:
$array = array_splice($array,
array_search('sambalpur.biz', array_keys($array)), 1) + $array;
Get a numerically indexed array of the keys with array_keys()
Search the returned array for sambalpur.biz with array_search()
Use the returned index to cut out that element with array_splice()
Add that to the existing array
Along the same line as Don't Panic:
$array = array_merge(array('sambalpur.biz' => $array['sambalpur.biz']), $array);
No need to unset as the order of insertion dictates which key overwrites the other so this one overwrites the previous one.
You can uksort the array, using the specific key you want to move to the beginning in the comparison function.
$key = 'sambalpur.biz';
uksort($array, function($a, $b) use ($key) {
if ($a == $key) return -1;
if ($b == $key) return 1;
return 0;
});
This should move that item to the beginning without changing the order of the other items.
Another possibility is to remove the child array, and then merge the main array back onto it.
$key = 'sambalpur.biz';
$x = $array[$key];
unset($array[$key]);
$array = array_merge([$key => $x], $array);
The issue seems to stem from array_unshift() reindexing elements you are passing. If you wanted to prepend your second array to your first array though and preserve indexes, you can use the + operator ($firstArray = $secondArray + $firstArray);
I wish to add string keys to my inner PHP arrays. So, I want to convert this:
array (size=2)
0 => array (size=3)
0 => string 'X705' (length=4)
1 => string 'X723' (length=4)
2 => string 'Sue' (length=0)
1 => array (size=3)
0 => string 'X714' (length=4)
1 => string 'X721' (length=4)
2 => string 'John' (length=0)
to this:
array (size=2)
0 =>
array (size=3)
'code1' => string 'X705' (length=4)
'code2' => string 'X723' (length=4)
'name' => string 'Sue' (length=0)
1 =>
array (size=3)
'code1' => string 'X714' (length=4)
'code2' => string 'X721' (length=4)
'name' => string 'John' (length=0)
I think I need to use array_walk but cannot fathom it out. Any help appreciated.
You can use array_map for that purpose:
$newarray = array_map(function($x) {
return array("code1" => $x[0], "code2" => $x[1], "name" => $x[2]);
}, $array);
where $array is your input array.
Start with this:
foreach ($array as $key=>$item) {
$item['code1']=$item[0];
unset($item[0]);
$item['code2']=$item[1];
unset($item[1]);
$item['name']=$item[2];
unset($item[2]);
$array[$key]=$item;
}
I would use array_map() but here's an alternate:
foreach($array as &$v) {
$v = array_combine(array('code1','code2','name'), $v);
}
I have this array:
array (size=3)
0 =>
array (size=2)
'name' => string 'XML' (length=3)
'processer' => string 'XMLp' (length=12)
1 =>
array (size=2)
'name' => string 'XML2' (length=3)
'processer' => string 'XML2pr' (length=12)
2 =>
array (size=2)
'name' => string 'CSV' (length=3)
'processer' => string 'CSVp' (length=12)
Since I dont need all of this, I wasnt this array converted:
$a = array ('XML', 'XML2', 'CSV');
so get by 'name'. How to do this elegantly in php?
$source = array(
0 => array (
'name' =>'A',
'processer' => 'XMLf'),
1 => array (
'name' =>'B',
'processer' => 'XMLp'),
2 => array (
'name' =>'C',
'processer' => 'XMLp')
);
$output = array_map(function ($value) {
return $value['name'];
}, $source);
print_r($output);
You could just loop over it, I don't think there's a much more elegant way:
$a = array();
foreach ($array as $value) {
$a[] = $value ['name'];
}
foreach($array as $a) $new[] = $a['name'];