How to merge array key in php - php

array (size=1)
11 =>
array (size=1)
'subject' =>
array (size=1)
'Maths' => string '21' (length=2)
array (size=1)
10 =>
array (size=1)
'subject' =>
array (size=1)
'Maths' => string '10' (length=2)
array (size=1)
11 =>
array (size=1)
'subject' =>
array (size=1)
'Gujarati' => string '21' (length=2)
array (size=1)
10 =>
array (size=1)
'subject' =>
array (size=1)
'Gujarati' => string '20' (length=2)
array key duplicate like 11 and 10
MY Question:
How to Create Below output array .
array (size=1)
11 =>
array (size=1)
'subject' =>
array (size=1)
'Maths' => string '21' (length=2)
'Gujarati' => string '21' (length=2)
array (size=1)
10 =>
array (size=1)
'subject' =>
array (size=1)
'Maths' => string '10' (length=2)
'Gujarati' => string '20' (length=2)

Use this function to get your result.
Btw: Just asking for help again and again without investigating by yourself is bad behavior, if someone points you to a function (like array_merge_recursive) you should at least take some time to RTM.
function array_merge_recursive_new() {
$arrays = func_get_args();
$base = array_shift($arrays);
foreach ($arrays as $array) {
reset($base); //important
while (list($key, $value) = #each($array)) {
if (is_array($value) && #is_array($base[$key])) {
$base[$key] = array_merge_recursive_new($base[$key], $value);
} else {
$base[$key] = $value;
}
}
}
return $base;
}
Source/Copyright:
http://php.net/manual/en/function.array-merge-recursive.php#106985
I have tested it with these arrays, which should be the same as you have them.
$arr1 = array(
10 => array( "subject" => array("math" => 1)),
11 => array( "subject" => array("math" => 2)),
);
$arr2 = array(
10 => array( "subject" => array("guawhatever" => "foo")),
11 => array( "subject" => array("guawhatever" => "bar")),
);
$blubb = array_merge_recursive_new($arr1,$arr2);

Related

How to show an array in alphabet order?

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

PHP - Merge arrays and subtraction some value

Assume I have two arrays $array1 :
array (size=3)
0 =>
array (size=5)
'id' => int 16
'project_id' => string '37' (length=2)
'description' => string 'Guitar' (length=6)
'qty' => int 87
'uom' => string 'Units' (length=5)
1 =>
array (size=5)
'id' => int 17
'project_id' => string '37' (length=2)
'description' => string 'Drums' (length=5)
'qty' => int 889
'uom' => string 'Drum' (length=4)
2 =>
array (size=5)
'id' => int 13
'project_id' => string '37' (length=2)
'description' => string 'Bump' (length=4)
'qty' => int 76
'uom' => string 'Units' (length=5)
and $array2 :
array (size=2)
0 =>
array (size=3)
'id' => int 17
'qty' => int 800
1 =>
array (size=3)
'id' => int 16
'qty' => int 87
I need some help:
merge above arrays by id and do subtraction of qty values
check if qty values less than or equal to 0 do not include in $result.
so the final $result would be:
array (size=2)
0 =>
array (size=5)
'id' => int 17
'project_id' => string '37' (length=2)
'description' => string 'Drums' (length=5)
'qty' => int 89
'uom' => string 'Drum' (length=4)
1 =>
array (size=5)
'id' => int 13
'project_id' => string '37' (length=2)
'description' => string 'Bump' (length=4)
'qty' => int 76
'uom' => string 'Units' (length=5)
Any help would be appreciated, thanks.
Try this:
foreach($array1 as $a) {
$match = false;
foreach($array2 as $b) {
if ($a['id'] == $b['id']) {
$match = true;
if (($a['qty'] - $b['qty']) > 0) {
$a['qty'] -= $b['qty'];
$result[] = $a;
break;
}
}
}
if(!$match) $result[] = $a;
}

PHP renaming array and covert from string to integer

I have var_dump() variable $fruit_array as below:
array (size=3)
0 =>
array (size=1)
'fruit_id' => string '2' (length=1)
1 =>
array (size=1)
'fruit_id' => string '1' (length=1)
2 =>
array (size=1)
'fruit_id' => string '3' (length=1)
I need to rename fruit_id to id and convert array string value to integer, example result:
array (size=3)
0 =>
array (size=1)
'id' => int 2
1 =>
array (size=1)
'id' => int 1
2 =>
array (size=1)
'id' => int 3
How can I do that ? thanks
You can do something like this:
<?php
foreach ( $array as $k=>$v )
{
$array[$k] ['id'] = intval( $array[$k] ['fruit_id'] );
unset($array[$k]['fruit_id']);
}

How to add to an array in a loop

I’m trying to add to an array in a loop but only the first element in the loop is added.
The array
array (size=7)
0 =>
array (size=2)
'id' => int 1
'name' => string 'john' (length=11)
1 =>
array (size=2)
'id' => int 2
'name' => string 'adam' (length=13)
2 =>
array (size=2)
'id' => int 3
'name' => string 'mary' (length=11)
My loop
foreach ($loops as $key => $loop) {
$idArray['id'] = $loop['id'];
}
var_dump($idArray); die();
Did I do anything wrong?
You overwrite your old value by assigning the new value to the array. An array can't have identical keys.
Try this:
foreach ($loops as $key => $loop)
{
$idArray['id'][] = $loop['id'];
}
var_dump($idArray); die();
So you add items to an array inside your array.
If you want the ordinal values of the $idarray to be the primary key values of what you are iterating over you can do this.
$loops = array(array('id' => 1, 'name' => 'john'), /* ... */);
foreach ($loops as $key => $loop)
{
$idArray[$loop['id']] = $loop;
}
var_dump($idArray); die();
var_dump will reveal this structure
array (size=7)
1 =>
array (size=2)
'id' => int 1
'name' => string 'john' (length=4)
2 =>
array (size=2)
'id' => int 2
'name' => string 'adam' (length=4)
3 =>
array (size=2)
'id' => int 3
'name' => string 'mary' (length=4)

split php array by value

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)

Categories