generating an array out of another array by using a unique key - php

I have an array that contains 3 products with attributes of "color" and "size" and the products are identified by a number ( ['code'] ).
My problem is that when i pull the data from the database I get this array that is in 6 pieces because "color" and "size" get stored in separate arrays.
My question is, how do I generate the data into an array of these 3 products with all their data in the same array.
Array(
[0] => Array
(
[code] => 123
[name] => box
[stock] => 2.00
[price] => 10.00
[color] => brown
)
[1] => Array
(
[code] => 123
[name] => box
[stock] => 2.00
[price] => 10.00
[size] => L
)
[2] => Array
(
[code] => 1234
[name] => box
[stock] => 3.00
[price] => 11.00
[color] => brown
)
[3] => Array
(
[code] => 1234
[name] => box
[stock] => 3.00
[price] => 11.00
[size] => XL
)
[4] => Array
(
[code] => 12345
[name] => box
[stock] => 4.00
[price] => 12.00
[size] => XL
)
[5] => Array
(
[code] => 12345
[name] => box
[stock] => 4.00
[price] => 12.00
[color] => gray
)
)
Expected output:
[0] => Array
(
[code] => 123
[name] => box
[stock] => 4.00
[price] => 12.00
[color] => gray
[size] => XL
)
What i'm looking to do is just combine the doubled array into one. Dont want to mess with SQL anymore - it gets attribute name in one table, attribute values from another table, code,stock,price from another table, name from another table. I know something can be done with just this array even if it will be just a temporary solution.

You can do this:
assuming your array is $products
$merged = array();
foreach($products as $product) {
if (!isset($sorted[$product['code']])) {
$merged[$product['code']] = $product;
} else {
$merged[$product['code']] = array_merge($sorted[$product['code']], $product);
}
}
Use the product code as array key and then merge the array.

If the two rows are always direct successors, with the first one holding the color field, while the latter holes the size field, you can make it easily by iterating over them in a for loop:
$maxCount = count($array);
for ($i = 1; $i < $maxCount; $i += 2) {
$array[$i - 1]['size'] = $array[$i]['size'];
unset($array[i]);
}
This will iterate over each second element of the array and add the size field to the preceding field.
If you need to have succeeding array keys afterwards you can call $arry = array_values($array);.
In case the the associated rows might not be successors you need to map them based on their code field (in case thats the primary key). You can use array_reduce() for that:
$desiredOutput = array_reduce($array, function($output, $element) {
if (!array_key_exists($element['code'], $output)) {
$output[$element['code']] = $element;
} elseif (array_key_exists('size', $element)) {
$output[$element['code']]['size'] = $element['size'];
} elseif (array_key_exists('color', $element)) {
$output[$element['code']]['color'] = $element['color'];
}
return $output;
}, []);

Related

How to make an array from the deference between two arrays?

I am making an online shopping cart. so I want to update my total stock after confirming my products... I am able to catch the quantity from the user and i am able to catch my data from MYSQL. Now i want to make an array from the difference between my MYSQL quantity and user quantity and update my total stock after purchase...
My MYSQL ARRAY OUTPUT IS:
print_r($qty);
Array ( [0] => Array ( [stock] => 100 ) [1] => Array ( [stock] => 100 ) [2] => Array ( [stock] => 50 ) [3] => Array ( [stock] => 100 ) )
and
my user ARRAY OUTPUT IS:
print_r ($_SESSION['productqty']);
Array ( [0] => 10 [1] => 12 [2] => 14 [3] => 16 )
I want to make an array which will be like
Array ( [0] => 90 [1] => 88 [2] => 36 [3] => 84 )
This array is the difference between the two arrays and it will be updated in the MYSQLI Query...
I have Tried Everything. Please Help me...
If the indexes are equal the following code could work:
$remaining = [];
// iterate $qty rows and fill $remaining array
foreach($qty as $index => $entry) {
$remaining[] = $entry['stock'] - $_SESSION['productqty'][$index];
}
var_dump($remaining);
EDIT: Store updated stock in database
Firstly it's necessary to select products' ids in your first query so that you're able to update them later. This step should result the following dump (apart from the correct ids)
Array (
[0] => Array ( [id] => 12, [stock] => 100 )
[1] => Array ( [id] => 37, [stock] => 100 )
[2] => Array ( [id] => 39, [stock] => 50 )
[3] => Array ( [id] => 50, [stock] => 100 )
)
Now you can optimize your calculation by attaching the products' ids to your array:
remaining = [];
foreach($qty as $index => $entry) {
$remaining[] = [
'id_product' => $entry['id'],
'stock' => $entry['stock'] - $_SESSION['productqty'][$index]
];
}
Now you're able to store your updated stocks:
foreach($remaining as $entry) {
// Perform your SQL-Operation, something like:
// UPDATE products SET stock = $entry['stock'] WHERE id = '$entry['id']
}
Attention: Please secure your application against SQL-Injections.
Take a look at: https://en.wikipedia.org/wiki/SQL_injection

PHP Nested Array is misaligned

I have a PHP loop that is pushing data into an array that will eventually be used to create a list of select options in my dropdown.
I feel like it is kinda close but I am doing something wrong somewhere.
The array of types needs to be part of the category it is associated with.
// Get the list of behavior types
public function _behavior_types()
{
$cat = $this->global_model->get_behavior_categories();
$typ = $this->global_model->get_behavior_types();
$output = array();
// Loop over our categories
foreach($cat as $c)
{
// Push the category name to an array
$output[] = $c['categoryName'];
// Loop over our types
foreach($typ as $t)
{
// If this type belongs to the category we are currently in, add it to the array
if($t['categoryID'] == $c['categoryID'])
{
array_push($output, $t);
}
}
}
return $output;
}
I have something out of place however and its causing the array to not be set up in the correct way.
Here is the current output:
Array
(
[0] => Negative
[1] => Array
(
[categoryID] => 2
[points] => -3
[typeID] => 4
[typeName] => Bad School Day
)
[2] => Positive
[3] => Array
(
[categoryID] => 1
[points] => 2
[typeID] => 1
[typeName] => Ate Dinner
)
[4] => Array
(
[categoryID] => 1
[points] => 2
[typeID] => 3
[typeName] => Brushed Teeth
)
[5] => Array
(
[categoryID] => 1
[points] => 3
[typeID] => 2
[typeName] => Completed Homework
)
)
Here is my desired output:
Array
(
[0] => Negative
[0] => Array
(
[categoryID] => 2
[points] => -3
[typeID] => 4
[typeName] => Bad School Day
)
[1] => Positive
[0] => Array
(
[categoryID] => 1
[points] => 2
[typeID] => 1
[typeName] => Ate Dinner
)
[1] => Array
(
[categoryID] => 1
[points] => 2
[typeID] => 3
[typeName] => Brushed Teeth
)
[2] => Array
(
[categoryID] => 1
[points] => 3
[typeID] => 2
[typeName] => Completed Homework
)
)
The dropdown in turn will look like:
Negative
Bad day at school
Positive
Ate Dinner
Brushed Teeth
Completed Homework
Your desired output is not really a valid array structure, at least how you have it typed. $output[0] cannot be both a string Negative and an array of options. I suggest making the category the key something like this:
// Get the list of behavior types
public function _behavior_types()
{
$cat = $this->global_model->get_behavior_categories();
$typ = $this->global_model->get_behavior_types();
$output = array();
// Loop over our categories
foreach($cat as $c)
{
// Push the category name to an array
$output[$c['categoryName']] = array();
// Loop over our types
foreach($typ as $t)
{
// If this type belongs to the category we are currently in, add it to the array
if($t['categoryID'] == $c['categoryID'])
{
array_push($output[$c['categoryName']], $t);
}
}
}
return $output;
}

PHP Session Array Update

I wish to update a single product quantity from a session with a new value using php.
How can I do this.
Array data is as displayed
Array
(
[products] => Array
(
[0] => Array
(
[name] => Ford AA Flatbed
[code] => IWV001
[qty] => 1
[price] => 15.00
[weight] => 0.12
)
[1] => Array
(
[name] => Ford AA Stakebed
[code] => IWV003
[qty] => 1
[price] => 15.00
[weight] => 0.21
)
)
)
any help would be most appreciated.
I want to be able to search through the session and find the product by code and update the quantity within that product.
Something like this:
foreach($products as &product) {
if ($product[code] == $codeToUpdate) {
$product[qty] = $newQty;
}
}
Of course, this is a sequential search so it will be slow for large data sets.

Extracting arrays into lists

I'm trying to extract each sub array using the foreach as list command.
Updated:
I am now trying this:
foreach ($data as $element) {
list($id,$ownerid,$owner,$coords,$name,$score,$citytype,$location) = $element;
findcontinent($coords);
echo $coords;
echo '<br>';
echo $cont;
echo '<br>';}
the function I am calling is as follows. I know this function works when I statically call a specific coordinate from the array $data[0]['coords'] for example.
function findcontinent($coords){
$tc2 = explode(":",$coords);
$tcx = (int)(trim($tc2[0],"'\'"));
$tcy = (int)(trim($tc2[1],"'"));
$tcx2 = round((($tcx)*(600/383)),0);
$tcy2 = round((($tcy)*(600/360)),0);
if($tcx2 < 100) // checking for continents ending with 0
{
$tcx3 = '0';
$xtrans = substr((string)$tcx3,0,1);
}
else
{
$xtrans = substr((string)$tcx2,0,1);
}
if($tcy2 < 100) // checking for continents starting with 0
{
$tcy3 = '0';
$ytrans = substr((string)$tcy3,0,1);
}
else
{
$ytrans = substr((string)$tcy2,0,1);
}
$cont = C.$ytrans.$xtrans;
return($cont);
}
I then want to run a set of SQL commands to update a DB. Do I have the correct syntax?
My array is as follows - sample. The actual datasets could be dynamic, many individual sub arrays?
[data] => Array
(
[0] => Array
(
[id] => 16515340
[owner_id] => 3475
[owner] => Player1
[coords] => '268:252
[name] => AC2013
[score] => 11863
[city_type] => castle
[location] => land
)
[1] => Array
(
[id] => 16515335
[owner_id] => 3475
[owner] => Player1
[coords] => '263:252
[name] => AC2013
[score] => 7
[city_type] => castle
[location] => water
)
[2] => Array
(
[id] => 17891610
[owner_id] => 3523
[owner] => Player2
[coords] => '282:273
[name] => City of Repoman9900
[score] => 1978
[city_type] => castle
[location] => water
)
[3] => Array
(
[id] => 10616856
[owner_id] => 73
[owner] => Player2
[coords] => '024:162
[name] => 1killer
[score] => 1308
[city_type] => castle
[location] => water
)
[4] => Array
(
[id] => 10813465
[owner_id] => 2862
[owner] => Player3
[coords] => '025:165
[name] => City of vuvuzea991
[score] => 1091
[city_type] => castle
[location] => land
)
[5] => Array
(
[id] => 17367317
[owner_id] => 84
[owner] => Player4
[coords] => '277:265
[name] => Dreadland
[score] => 776
[city_type] => castle
[location] => water
)
[6] => Array
(
[id] => 2162850
[owner_id] => 2989
[owner] => Player5
[coords] => '162:033
[name] => City of Dinoeyez
[score] => 157
[city_type] => castle
[location] => water
)
[7] => Array
(
[id] => 2818192
[owner_id] => 556
[owner] => Player6
[coords] => '144:043
[name] => City of wildfire123
[score] => 7
[city_type] => castle
[location] => water
)
)
Since it looks like associative arrays cannot be exploded into lists, which would have been nice, trying to run this loop to extract even 1 record, and failing
$count = 0;
// loop to cycle through array
foreach($data as $data=>$inner_array){
$c2 = (string)$count;
$id = $data[$count]['id'];
echo $id;
echo '<br>';
echo $count;
echo '<br>';
//then increment counter and move to next
$count = $count+1;
}
The counters return just fine, but I cannot seem to parse any of the variables in the sub-array. Echo output is as follows:
<br>0<br><br>1<br><br>2<br><br>3<br><br>4<br><br>5<br>
The data set I am using right now is 6 sub-arrays, so I know it is going through the loop correctly.
foreach ($data as list($id,$ownerid,$owner,$coords,$name,$score,$citytype,$location)) {
// commands
}
Since this use of list is only allowed in the beta version of PHP, the more portable syntax requires two steps:
foreach ($data as $element) {
list($id,$ownerid,$owner,$coords,$name,$score,$citytype,$location) = $element;
// commands
}
foreach ($data=>inner_array as xxx) is not valid. The var=>var syntax is only valid after as, not before it. I'm not sure why you have nested for loops -- there's only one array you need to iterate over.
I would be very wary of using this syntax when the sub-arrays are associative arrays. List assignment is based on the order of elements in the sub-array, it doesn't try to match variable names with keys. So if you have a sub-array where the owner_id element is before the id element, the list assignment will set $id to the owner ID, and $owner_id to the ID. The order of elements in an associative array is the order they were added to the array (unless you've used a function to rearrange them, such as array_sort() or array_splice()), so unless you're sure you always add keys in the same order this can be dangerous.
UPDATE:
List assignment like this doesn't work with associative arrays. According to PHP list documentation:
list() only works on numerical arrays and assumes the numerical indices start at 0.
So you'll need to assign the variables individually:
foreach ($data as $element) {
$id = $element['id'];
$owner_id = $element['owner_id'];
// and so on
}

Remove duplicates from multi-dimensional array based on higher points

I'm racking my brains trying to think of a solution. I can find plenty of solutions to remove dupes from a 2d array but I need to remove dupes where a value is lower than the other. Here is the array:
Array
(
[basketball] => Array
(
[0] => stdClass Object
(
[id] => 2
[username] => Beans
[points] => 30
)
[1] => stdClass Object
(
[id] => 314
[username] => slights
[points] => 20
)
[2] => stdClass Object
(
[id] => 123
[username] => gibb54
[points] => 5
)
)
[soccer] => Array
(
[0] => stdClass Object
(
[id] => 2
[username] => Beans
[points] => 95
)
[1] => stdClass Object
(
[id] => 49
[username] => sans
[points] => 65
)
[2] => stdClass Object
(
[id] => 122
[username] => peano
[points] => 50
)
[3] => stdClass Object
(
[id] => 174
[username] => fordb
[points] => 30
)
[4] => stdClass Object
(
[id] => 112
[username] => danc
[points] => 30
)
)
)
As you may see, user ID 2, Beans is the first selection for both basketball and soccer. As they have more points for soccer, I need to remove their entry for basketball to make ID 314, slights the 0 value.
I would need to do this continually until no user be the 0 value for any of the primary array values twice.
I've tried various combinations of foreach solutions but I'm not getting anywhere. I thought a while loop would be more suitable but I don't know what condition to test for.
Any ideas please?!
I would loop through your data and create a dictionary where the keys are the user ids, and the values are the appropriate user objects with the sport appended. Then you can reconstruct your example data array structure by looping through this de-duped array using the sport data to determine where to put each user.
To create the de-duped array, use something like:
$deDupedData = array();
foreach ($data as $sport => $users) {
foreach ($users as $user) {
if (isset($deDupedData[$user->id])) {
if ($user->points > $deDupedData[$user->id]->points) {
$deDupedData[$user->id]->sport = $sport;
$deDupedData[$user->id]->points = $user->points;
}
} else {
$modifiedUser = $user;
$modifiedUser->sport = $sport;
$deDupedData[$user->id] = $modifiedUser;
}
}
}
// Now reconstruct your array...

Categories