-
Posts
24,606 -
Joined
-
Last visited
-
Days Won
831
Everything posted by Barand
-
How to manipulate multi-dimension array in PHP?
Barand replied to soyaslim's topic in PHP Coding Help
OK - I've added the sort usort($test, fn($a, $b) => $b['itemCount']<=>$a['itemCount']); // sort descending itemCount $seen = []; foreach ($test as $k => &$rec) { $rec['rolanID'] = array_diff($rec['rolanID'], $seen); // find new ids if ($rec['rolanID']) { // if there are some new ones ... $rec['itemCount'] = count($rec['rolanID']); // count them $seen = array_merge($seen, $rec['rolanID']); // add the new ones to those already seen } else unset($test[$k]); // if no ids, remove the array item } and I now get this (no duplicate 123)... Array ( [0] => Array ( [supplier] => TEST2 DEPO [rolanID] => Array ( [0] => 456 [1] => 188 [2] => 200 [3] => 123 ) [itemCount] => 4 ) [1] => Array ( [supplier] => TEST DEPO [rolanID] => Array ( [1] => 234 ) [itemCount] => 1 ) [2] => Array ( [supplier] => DIFFERENT DEPO [rolanID] => Array ( [0] => 897 [1] => 487 [2] => 100 ) [itemCount] => 3 ) ) -
How to manipulate multi-dimension array in PHP?
Barand replied to soyaslim's topic in PHP Coding Help
Using your new $test array with my code, I get this in the $test array... Array ( [0] => Array ( [supplier] => ROLAN [rolanID] => Array ( [0] => 456 ) [itemCount] => 1 ) [1] => Array ( [supplier] => ROLAN [rolanID] => Array ( [0] => 123 [1] => 234 ) [itemCount] => 2 ) [3] => Array ( [supplier] => DIFFERENT DEPO [rolanID] => Array ( [0] => 897 [1] => 487 [2] => 100 ) [itemCount] => 3 ) [4] => Array ( [supplier] => TEST2 DEPO [rolanID] => Array ( [1] => 188 [2] => 200 ) [itemCount] => 2 ) ) -
Yes, you could.
-
Does output of your $_GET array show an "id" key? echo '<pre>' . print_r($_GET, true) . '</pre>';
-
How to manipulate multi-dimension array in PHP?
Barand replied to soyaslim's topic in PHP Coding Help
Here's my solution $seen = []; foreach ($test as $k => &$rec) { $rec['rolanID'] = array_diff($rec['rolanID'], $seen); // find new ids if ($rec['rolanID']) { // if there are some new ones ... $rec['itemCount'] = count($rec['rolanID']); // count them $seen = array_merge($seen, $rec['rolanID']); // add the new ones to those already seen } else unset($test[$k]); // if no ids, remove the array item } +-----------------------------------------------+ | | | $test ARRAY - AFTER | | | +-----------------------------------------------+ | | | Array | | ( | | [0] => Array | | ( | | [supplier] => TEST DEPO | | [rolanID] => Array | | ( | | [0] => 123 | | [1] => 234 | | [2] => 456 | | ) | | | | [itemCount] => 3 | | ) | | | | [1] => Array | | ( | | [supplier] => ANOTHER DEPO | | [rolanID] => Array | | ( | | [1] => 786 | | [2] => 345 | | ) | | | | [itemCount] => 2 | | ) | | ) | | | +-----------------------------------------------+ -
I feel your pain. Fetch syntax is far too cryptic for me.
-
Google turned up this https://rapidapi.com/guides/fetch-api-async-await
-
how to connect php to mysql (any errors in code?)
Barand replied to duchaine1's topic in PHP Coding Help
Yes - line 7. Plus it's normal to specify a default database. Depends what you want to do with it and what operating system you are using. -
Is there an item in your $_GET array with the key "c.id"? I suspect it will be just $_GET["id"] as you were originally using.
-
Would this work? $results = array_filter($results, fn($v) => in_array($v->iso_639_1, ['en','fr']) && $v->type == 'Trailer' && $v->site == 'YouTube' ); rsort($results); // 'fr' first then 'en' results foreach ($results as $r) { echo "{$r->iso_639_1} – {$r->name}<br>"; }
-
I can't imagine anyone attempting to edit that amount of pastebin data to make it suitable to test any solution. Post a json_encoded version of the array here so we have something useable. PS and post the code you have so far - it may give us a clue to what you are talking about.
-
How to manipulate multi-dimension array in PHP?
Barand replied to soyaslim's topic in PHP Coding Help
Breakfast and two coffees in the morning are my minimum requirement. -
You find that out with a process commonly known as "testing" (You might want to Google that)
-
The one with braces will work, the other won't even run.
-
If that's how you want the layout to look. Only you know that.
-
Then append those to the content too.
-
How to manipulate multi-dimension array in PHP?
Barand replied to soyaslim's topic in PHP Coding Help
Similar data but solution is more complicated with this one -
How to submit a form in php multiple times with a single submit button
Barand replied to alir22456's topic in PHP Coding Help
You have a form which has been designed to produce invoices one month at a time - there is only the option to enter a singlw month First thing to do is redesign the form to allow the specification of multiple months. For example a month menu allowing multiple selections, or 12 checkboxes, or have fields to specify number of months and starting month (4 months starting at August) Then change the processing of the submitted form to produce the invoice records for those months. -
Don't use 2 queries when 1 will do SELECT c.id , c.name , c.last_name , c.mobile_number , c.status , a.username as mentorname FROM contacts c LEFT JOIN accounts a ON c.mentor = a.id WHERE c.id = 1; +----+-------+-----------+---------------+---------+------------+ | id | name | last_name | mobile_number | status | mentorname | +----+-------+-----------+---------------+---------+------------+ | 1 | Scott | Chegg | 01012345678 | current | jbloggs | +----+-------+-----------+---------------+---------+------------+
-
I see you joined in 2003 - experience is always welcome.
-
How to process the multi dimensional array into specific array?
Barand replied to soyaslim's topic in PHP Coding Help
It's the append that makes the difference. Your solution was always overwriting the product id that that was there. -
How to process the multi dimensional array into specific array?
Barand replied to soyaslim's topic in PHP Coding Help
All you need to do is loop through your original array using foreach() and append the products to a new array, providing the required key values. foreach ($orig as $rec) $new[ $rec['supplier'] ]['productID'][] = $rec['part_id']; Result for $new Array ( [COOLDRIVE DISTRIBUTION] => Array ( [productID] => Array ( [0] => 2338117 ) ) [ROLAN] => Array ( [productID] => Array ( [0] => 2338117 [1] => 51154 ) ) ) -
How to process the multi dimensional array into specific array?
Barand replied to soyaslim's topic in PHP Coding Help
What have you tried so far? -
Need this code to demote user role if points fall below minimum
Barand replied to Cybercli's topic in PHP Coding Help
PS If you really want to do it in PHP then you can apply the same method to an array $thresholds = array( '-' => [0, 100], 'contributor' => [101, 1000], 'author' => [1001, 10000], 'editor' => [10001, 100000], 'administrator' => [100001, 999999999] ); echo getUserRole(50000, $thresholds); //--> editor function getUserRole($current_balance, $thresholds) { foreach ( $thresholds as $role => $range ) { if ( $range[0] <= $current_balance && $current_balance <= $range[1] ) { return $role; } } return false; } -
Need this code to demote user role if points fall below minimum
Barand replied to Cybercli's topic in PHP Coding Help
With a couple of db tables like this Table: user Table: role +---------+----------+--------+ +---------+---------------+-----------+------------+ | user_id | username | points | | role_id | role_name | point_min | points_max | +---------+----------+--------+ +---------+---------------+-----------+------------+ | 1 | John | 66 | | 5 | - | 0 | 100 | | 2 | Paul | 101 | | 6 | Contributor | 101 | 1000 | | 3 | George | 3000 | | 7 | Author | 1001 | 10000 | | 4 | Ringo | 200000 | | 8 | Editor | 10001 | 100000 | +---------+----------+--------+ | 9 | Administrator | 100001 | 999999999 | +---------+---------------+-----------+------------+ Then a simple query SELECT username , rolename FROM user u JOIN role r ON u.points BETWEEN r.points_min AND r.points_max; does the job for you +----------+---------------+ | username | rolename | +----------+---------------+ | John | - | | Paul | Contributor | | George | Author | | Ringo | Administrator | +----------+---------------+