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 );
Related
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 array like below. I want to unset all array having same q_id but merge its name key.
array
0 =>
array
'field_name' => string 'Hindu rastrya' (length=13)
'name' => string '283' (length=3)
'q_id' => string '199' (length=3)
1 =>
array
'field_name' => string 'dharma nirpachya' (length=16)
'name' => string '284' (length=3)
'q_id' => string '199' (length=3)
2 =>
array
'field_name' => string 'j vaye pni hunxa' (length=16)
'name' => string '285' (length=3)
'q_id' => string '199' (length=3)
3 =>
array
'field_name' => string 'Nepal' (length=5)
'name' => string '286' (length=3)
'q_id' => string '200' (length=3)
4 =>
array
'field_name' => string 'India' (length=5)
'name' => string '287' (length=3)
'q_id' => string '200' (length=3)
5 =>
array
'field_name' => string 'China' (length=5)
'name' => string '288' (length=3)
'q_id' => string '200' (length=3)
Expected:
array
0 =>
array
'field_name' => string 'Hindu rastrya' (length=13)
'name' =>
array
0 => string '283' (length=3)
1 => string '284' (length=3)
2 => string '285' (length=3)
'q_id' => string '199' (length=3)
1 =>
array
'field_name' => string 'Nepal' (length=16)
'name' =>
array
0 => string '286' (length=3)
1 => string '287' (length=3)
2 => string '288' (length=3)
'q_id' => string '200' (length=3)
I tried following code but It didnot preserve the keys so it fails:
foreach ($form_option_data as $key=>$option)
{
if($form_option_data[$key]['q_id'] == $form_option_data[$key+1]['q_id']){
unset($form_option_data[$key+1]);
}
}
The following should do the job:
$data = ...; //initial array
$result = array();
foreach ($data as $entry) {
if (!array_key_exists($entry['q_id'], $result)) {
$result[$entry['q_id']] = array(
'q_id' => $entry['q_id'],
'name' => array($entry['name'])
);
} else {
$result[$entry['q_id']]['name'][] = $entry['name'];
}
}
$result = array_values($result); // to re-index the table so that keys start from 0
I am trying to merge 2 arrays: 1 multidimensional and another one normal:
Multidimensional array - $_SESSION["products"]
array (size=2)
0 =>
array (size=4)
'name' => string 'Lg Monitor' (length=10)
'code' => string '30' (length=2)
'qty' => string '1' (length=1)
'price' => string '1300.50' (length=7)
1 =>
array (size=4)
'name' => string 'Smasung Monitor' (length=15)
'code' => string '29' (length=2)
'qty' => string '1' (length=1)
'price' => string '2300.50' (length=7)
Simple array - $qty
array (size=2)
0 => string '2' (length=1)
1 => string '3' (length=1)
EXPECTED OUTPUT
array (size=2)
0 =>
array (size=4)
'name' => string 'Lg Monitor' (length=10)
'code' => string '30' (length=2)
'qty' => string '2' (length=1) // notice the qty change
'price' => string '1300.50' (length=7)
1 =>
array (size=4)
'name' => string 'Smasung Monitor' (length=15)
'code' => string '29' (length=2)
'qty' => string '3' (length=1) // notice the qty change
'price' => string '2300.50' (length=7)
I tried:
foreach ($_SESSION["products"] as $cart_itm){
foreach($qty as $qt) {
$cart_itm['qty'] = $qt;
}
}
But did not work, cart_itm['qty'] remained the same (1).
Try with this:
foreach ($_SESSION["products"] as $key => &$cart_itm){
$cart_itm['qty'] = $qty[$key];
}
I have this multidimensional array
I am wondered how can i sort this array again so i can use it in for loop.
array (size=3)
0 =>
array (size=1)
0 =>
array (size=7)
'username' => string 'wajdi' (length=5)
'userimage' => string 'file_3898.jpg' (length=13)
'date' => int 1373721708
'postid' => string '118' (length=3)
'type' => string 'comment' (length=7)
'comment' => string 'a' (length=1)
'notify' => string '0' (length=1)
2 =>
array (size=1)
2 =>
array (size=7)
'username' => string 'wajdi' (length=5)
'userimage' => string 'file_3898.jpg' (length=13)
'date' => int 1373721711
'postid' => string '118' (length=3)
'type' => string 'comment' (length=7)
'comment' => string 'c' (length=1)
'notify' => string '0' (length=1)
3 =>
array (size=1)
3 =>
array (size=7)
'username' => string 'wajdi' (length=5)
'userimage' => string 'file_3898.jpg' (length=13)
'date' => int 1373721712
'postid' => string '118' (length=3)
'type' => string 'comment' (length=7)
'comment' => string 'd' (length=1)
'notify' => string '0' (length=1)
How can I reindex this array to become
array (size=3)
0 =>
array (size=1)
0 =>
array (size=7)
'username' => string 'wajdi' (length=5)
'userimage' => string 'file_3898.jpg' (length=13)
'date' => int 1373721708
'postid' => string '118' (length=3)
'type' => string 'comment' (length=7)
'comment' => string 'a' (length=1)
'notify' => string '0' (length=1)
1 =>
array (size=1)
1 =>
array (size=7)
'username' => string 'wajdi' (length=5)
'userimage' => string 'file_3898.jpg' (length=13)
'date' => int 1373721711
'postid' => string '118' (length=3)
'type' => string 'comment' (length=7)
'comment' => string 'c' (length=1)
'notify' => string '0' (length=1)
2 =>
array (size=1)
2 =>
array (size=7)
'username' => string 'wajdi' (length=5)
'userimage' => string 'file_3898.jpg' (length=13)
'date' => int 1373721712
'postid' => string '118' (length=3)
'type' => string 'comment' (length=7)
'comment' => string 'd' (length=1)
'notify' => string '0' (length=1)
I tried array_shift and array_chunk but nothing works !!!
Please help, thank you all :)
Use array_multisort to sort multi-dimensional arrays or to sort an array using multiple keys.
I think this should do it but it's be a lot cleaner if you didn't have the extra level off the array.
$new_array = array();
$index = 0;
foreach($array as $i1 => $a1){
foreach($a1 as $i2 => $a2){
$new_array[$index][$index] = $a2;
}
$index++;
}
You can use 'array_values' for re-indexing which start index from 0. As per your requirement inner array are not starting from 0 but are same as parent array index. You have to use foreach for that. To index the way you want can be done like this:
$info = array(
0 => array (
0 => array (
'username' => 'wajdi',
'userimage' => 'file_3898.jpg',
'date' => 1373721708,
'postid' => '118',
'type' => 'comment',
'comment' => 'a',
'notify' => '0'
)
),
2 => array (
2 => array (
'username' => 'wajdi',
'userimage' => 'file_3898.jpg',
'date' => 1373721708,
'postid' => '118',
'type' => 'comment',
'comment' => 'a',
'notify' => '0'
)
),
3 => array (
3 => array (
'username' => 'wajdi',
'userimage' => 'file_3898.jpg',
'date' => 1373721708,
'postid' => '118',
'type' => 'comment',
'comment' => 'a',
'notify' => '0'
)
)
);
var_dump($info); // original index
$info = array_values($info);
foreach ($info as $key => $value) {
$temp = array();
foreach($value as $k => $v) {
$temp[$key] = $v;
}
$info[$key] = $temp;
}
var_dump($info); // re-index
I have this array
array
0 =>
array
'prodid' => string '2' (length=1)
'qty' => int 2
'price' => string '100.00' (length=6)
'sid' => string '13' (length=2)
2 =>
array
'prodid' => string '3' (length=1)
'qty' => int 1
'price' => string '380.00' (length=6)
'sid' => string '13' (length=2)
1 =>
array
'prodid' => string '8' (length=1)
'qty' => int 1
'price' => string '300.00' (length=6)
'sid' => string '24' (length=2)
How can i split it in to a multi dimentional array where the values 'sid' match like so.
array
0 =>
array
0 =>
array
'prodid' => string '2' (length=1)
'qty' => int 2
'price' => string '100.00' (length=6)
'sid' => string '13' (length=2)
1 =>
array
'prodid' => string '2' (length=1)
'qty' => int 2
'price' => string '100.00' (length=6)
'sid' => string '13' (length=2)
1 =>
array
0 =>
array
'prodid' => string '7' (length=1)
'qty' => int 1
'price' => string '200.00' (length=6)
'sid' => string '15' (length=2)
You pick the value you want to group with as key for a new array and push the member to it
$new = array();
foreach($array as $member)
{
$key = $member['group-value'];
$new[$key][] = $member;
}
You then extract the bare values from the new array by using array_values:
$new = array_values($new);
And that's it. If you want to spare the last part, you can also create the mapping of keys and it's numerical index on your own:
$new = array();
$keys = array();
foreach($array as $member)
{
$key = $member['group-value'];
isset($keys[$key]) || $keys[$key] = count($keys);
$new[$keys[$key]][] = $member;
}
For PHP5.3+
$result = array_reduce (
$array,
function ($item, $result) {
if (!isset($result[$item['sid']])) $result[$item['sid']] = array();
$result[$item['sid']][] = $item;
return $result;
},
array()
)
// $ouput array will be indexed by the 'sid' value
$output = array();
// Iterate over the main array and create a new subarray if
// it doesn't already exist, or add to it if it does.
foreach($input as $subarr) {
if (!isset($output[$subarr['sid']]) {
// New array indexed by sid
$output[$subarr['sid']] = array();
}
// Append the whole array
$output[$subarr['sid']][] = $subarr;
}
Note: this produces slightly different output than you described, in that it uses the sid as array keys rather than just indexing them from 0:
array
// Note key difference...
// sid == 13
13 =>
array
0 =>
array
'prodid' => string '2' (length=1)
'qty' => int 2
'price' => string '100.00' (length=6)
'sid' => string '13' (length=2)
1 =>
array
'prodid' => string '2' (length=1)
'qty' => int 2
'price' => string '100.00' (length=6)
'sid' => string '13' (length=2)
// sid == 15
15 =>
array
0 =>
array
'prodid' => string '7' (length=1)
'qty' => int 1
'price' => string '200.00' (length=6)
'sid' => string '15' (length=2)