split php array by value - php

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)

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

Remove array 0 in array PHP

I have 2 product id and my code is:
$Final=array();
foreach ($ids as $me)
{
$op=DB::table('product')->where(['id'=>$me])->get();
//$Final[]= $op;
array_push($Final,$op);
}
This code returns:
array (size=1)
0 =>
array (size=1)
0 =>
array (size=15)
'id' => string '34' (length=2)
'title' => string 'گوسفند' (length=12)
'title_url' => string 'sheep' (length=5)
'code' => string 'eerer' (length=5)
'code_url' => string 'eerer' (length=5)
'content' => string '<p>sheep</p>
' (length=14)
'cat' => string '68' (length=2)
'price' => string '50000' (length=5)
'product_state' => string '1' (length=1)
'date' => string '' (length=0)
'order_number' => string '0' (length=1)
'Special' => string '0' (length=1)
'View' => string '0' (length=1)
'number_product' => string '1' (length=1)
'discounts' => string '' (length=0)
I need to remove
array (size=2) 0 => array (size=1) 0 =>
$ids => filter id
for get product number for example (22,34)
I Think you should try this.
$Final=array();
foreach ($ids as $me){
$op=DB::table('product')->where(['id'=>$me])->get();
if($op) {
array_push($Final,$op[0]);
}
}
Then you will get these values.
array (size=2)
0 =>
array (size=15)
'id' => string '34' (length=2)
1 =>
array (size=15)
'id' => string '22' (length=2)
If you are using Any framework then framwork provide us some methods to run query with where in to get all the records in single query.
$op=DB::table('product')->whereIn('id'=>$ids)->get();
you will get array of collection for all the products.

Change data type in specific element array in PHP

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

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

Merge 1 multidimensional array with a simple array

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];
}

Categories