ChenXiu
Members-
Posts
177 -
Joined
-
Last visited
Everything posted by ChenXiu
-
A company allows me to curl their API for up to 3 sku numbers at a time (e.g. appending "sku[]=12345&sku[]=36545&sku[]=88775" to their URL). To exceed their "3 sku limit," they provided me a 2nd API key. The following script is supposed to divide my total number of skus in half, and curl both halves in parallel (half using one API key, half using the other). 🤢 I have a feeling that my script isn't really properly dividing my skus amongst my API url/key pairs! 🤢 <?php $sku_numbers = array( '12345', '33333', '98745', '44444', '11111', '05054' ) $key1 = 'first_SECRET_key'; $key2 = 'second_SECRET_key'; $keycount = 0; foreach(array_chunk($sku_numbers,3) as $chunk) { $api_KEY = $key1; $keycount++; if($keycount % 2 != 0) { $api_KEY = $key2; } $sku_LISTINGS = '&sku[]=' . implode('&sku[]=',$chunk); $url = 'https://api.data_companneee.com/script.php?apiKey='.$api_KEY.$sku_LISTINGS; $url2 = 'https://api.data_companneee.com/script.php?apiKey='.$api_KEY.$sku_LISTINGS; $ch1 = curl_init($url); $ch2 = curl_init($url2); curl_setopt($ch1, CURLOPT_HEADER, 0); curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch2, CURLOPT_HEADER, 0); curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true); $mh = curl_multi_init(); curl_multi_add_handle($mh, $ch1); curl_multi_add_handle($mh, $ch2); $active = null; do { $status = curl_multi_exec($mh, $active); curl_multi_select($mh); } while ($status === CURLM_CALL_MULTI_PERFORM || $active); $response_A = curl_multi_getcontent($ch1); $response_B = curl_multi_getcontent($ch2); curl_multi_remove_handle($mh, $ch1); curl_multi_remove_handle($mh, $ch2); curl_multi_close($mh); foreach($chunk as $each_sku) { // do stuff (process $response_A) etc. // do stuff (process $response_B) etc. } } May I please have your thoughts on this? Thank you!!
-
I believe I mis-spoke. Olumide, your answer seems to work! Thank you for that!! You have restored my faith in PHP 😀
-
Thank you all for your time.
-
I expected 4 items to arrive. Here are the SKU numbers: 1111 2222 2222 3333 When the box arrived and I opened it, I found these SKU numbers: 1111 2222 3333 3333 Upon opening the box, I immediately saw: • SKU 2222 was missing • there was an extra SKU 3333 My simple PHP script correctly shows the missing SKUs and the reconciled SKUs..... but why is it not showing the extra SKU? <?php $expected = array( '1111', '2222', '2222', '3333' ); $received = array( '1111', '2222', '3333', '3333' ); $expected = array_filter($expected); $received = array_filter($received); $expected_counts = array_count_values($expected); $received_counts = array_count_values($received); $not_received = array(); $reconciled = array(); $extra = array(); foreach ($expected_counts as $sku => $expected_count) { if (isset($received_counts[$sku])) { if ($received_counts[$sku] >= $expected_count) { $reconciled = array_merge($reconciled, array_fill(0, $expected_count, $sku)); } else { $reconciled = array_merge($reconciled, array_fill(0, $received_counts[$sku], $sku)); $not_received = array_merge($not_received, array_fill(0, $expected_count - $received_counts[$sku], $sku)); } } else { $not_received = array_merge($not_received, array_fill(0, $expected_count, $sku)); } } foreach ($received_counts as $sku => $received_count) { if (!isset($expected_counts[$sku])) { $extra = array_merge($extra, array_fill(0, $received_count, $sku)); } } // Output the results print_r($not_received); // SKUs that are missing print_r($reconciled); // SKUs that reconcile print_r($extra); // EXTRA (unexpected) SKUs ?>
-
@barand, same result whether or not they are strings: $total = 9780393872439 + 9780134130422 + 30.50; echo $total; // WRONG TOTAL AGAIN @ginerjm, I have error reporting on, there are no errors. If you have errors ($total is undefined), then you forgot to initialize "$total = 0;" like I did. @requinix, PHP is the easiest programming language to learn, the others are too complex for me to understand. Just like 13 digit numbers are too complex for PHP to understand. C'est la vie. The correct answer is: When using numbers over 12 digits long, PHP may exhibit 'unexpected behavior' because of the way it handles floating points (euphemistically called "rounding errors"), therefore extensions like BCmath may help.
-
Now I know why real programmers snicker a little bit when I mention PHP. $numbers = array( '9780393872439', '9780134130422', '30.50' ); foreach($numbers as $num) { $total += $num; } echo $total //WRONG! WRONG! WRONG! I'm frankly disappointed.
-
For a mySQL query on 4 tables that will be joined on the "sku_number varchar(20) PRIMARY KEY" column, does the ordering of the tables in the query affect performance (speed) ? • I need a large-data longtext column from Table "a" • I need a small varchar(9) column from Table "b" • I need 5 medium sized (varchar(255)) columns from Table "c" • I need 2 small varchar(55) columns from Table "d" Will the speed be just as fast if I do the query in alphabetical order (e.g. "select a.bigdata, b.color, c.weight, c.height, c.depth...") -or- Would the query execute faster if done from the "smallest data size - to - biggest data size" (e.g. "select b.color, a.bigdata...." ... and, should "how many columns I need from a table" affect the order? Thank you in advance.
-
mySQL "union all" queries on tables with varying number of columns
ChenXiu replied to ChenXiu's topic in PHP Coding Help
Thank you both. My goal was actually to speed up 4 mysql queries to 4 different tables, some sharing the same data, some not. I was looking for a huge speed improvement, but with the complexity of the "union all" style, it is a nightmare for me, especially for keeping track of what data comes from which table. But I'm glad I learned, and actually executed a real "union" style query! However, I just now experimented with using "join" instead of "union" and was surprised that using "join" benchmarked in at 3 (THREE) times faster (unlike the meager 2X faster of "union") than doing normal mysql queries of 4 tables in sequence (thus extending the vacation/relaxation portion of my life by an extra 20 picoseconds, instead of 15 picoseconds (golf course, here I come!)). I'm finding mysql to be more of a religion than a science. Like Excel. A few have absolutely mastered it, and for them, mysql can do just about everything on the planet. Same with Excel. The rest of us will never truly get it. But that's what makes it fun to try and learn. -
mySQL "union all" queries on tables with varying number of columns
ChenXiu replied to ChenXiu's topic in PHP Coding Help
Barand, thank you. Okay, I think I understand. It sounds like if the first query defines the column names, then maybe what I should do is make my first query the longest query with the most actual columns, and set the alias names there to be used for the remaining "union select" subqueries. Regarding using a "dummy column" to differentiate, I am not sure how PHP echoes a dummy column, would it be something like this: $result = $db->query($query); while($row=$result1->fetch_assoc()) { echo $row["A"."description"]; } ( ...probably not, but I want to demonstrate that I am trying 😀 ) The reason some of the columns have the same names is because I have never used "union select" before, so never needed to differentiate, and there is often lots of duplication amongst the data I receive from various sources (e.g. all the sources seem to provide "color" and "description," of which only one of them may provide "weight" or "dimensions," etc. So when I built the mySQL tables, I didn't know in advance I would ever need to differentiate between which table I needed, for example, the color from. I suppose the easiest thing would be to simply rename the table columns so they are all different (takes 5 minutes max to do this), but then I won't learn the new stuff :-) Regarding empty results -- Yes! For whatever reason, out of the 100's of experiments I did earlier today, while looping through the "while ($row = $result->fetch_assoc())" loops, PHP would always echo empty results in seemingly random spots. My "fix" on this was this: $dog = !empty($row["columnName"]) ? $row["columnName"] : ""; $horse = !empty($row["other_columnName"]) ? $row["other_columnName"] : ""; $bird = !empty($row["columnSomething"]) ? $row["columnSomething"] : ""; echo $dog . $bird . $horse, etc., etc. I really want to learn how to make the, "Union Select" technique work! I did some benchmark loops of 1000 queries (union 3 queries, versus 3 queries in sequence, ran 1000 times each) and found that "Union Select" is twice as fast! Instead of taking 30 nanoseconds for a 1000-query loop, it only takes 15 nanoseconds. That being said, just running a "union query" once will only save me 15 picoseconds. However, when you get to be my age, every picosecond counts. -
I have tried several "UNION ALL" queries, and I either get: • "undefined index" errors or • when selecting from tables with same column names, I can't differentiate which table the column belongs to • I get alot of empty results Here are some examples I have tried: $query = "SELECT description as ffff, NULL as gggg FROM myTable WHERE sku = '1234' UNION ALL SELECT description as ffff, weight as gggg FROM my_other_Table WHERE sku = '1234'" $result = $db->query($query); while ($row = $result->fetch_assoc()) { echo $row['fff']; echo $ow['gggg']; } $query = "SELECT description as ffff, NULL as gggg FROM myTable WHERE sku = '1234' UNION ALL SELECT color as uuuuuu, weight as pppppppppp FROM my_2nd_Table WHERE sku = '1234'" UNION ALL SELECT color as wwxxyyzz, weight as mmmnnnoooppp FROM my_3rd_Table WHERE sku = '1234'" $result = $db->query($Query); while ($row = $result->fetch_assoc()) { echo $row["ffff"]; echo $row["uuuuuu"]; echo $row["pppppppppp"] echo $row["wwxxyyzz"]; echo $row["mmmnnnoooppp"]; } I think I am almost on the right 'track' but the train conductor wants his ticket :-)
-
@Barand, thank you! That thing you do -- that putting the "key-back-into-the-array-thing" -- $received[$k] -- got me again! I still don't know how you know when to do this. But it works perfect! Thank you again! Your code appears to work perfectly! Had you not come up with a solution, my next step would have been to use mySQL :-) @jodunno, thank you! Your code worked. The logic you used makes perfect sense now.... but was out of my grasp this past week I've been trying to figure this on out. (I'm sure unintentional, but it is amusing how your variable name "$reckoning" sorta fits right in there with Noah and $ark 😀)
-
Hmm.... I see 2 replies while I was typing my last post. I'll try them both.
-
I'll repost my original post: The ark has 14 animals on it: 6 cows, 7 fish, and a cat (keeps the mice away) $ark = array( '3 cows', '7 fish', '3 cows', '1 cat' ); When the Ark arrived, Billy made a checklist: $received = array( '3 cows', '7 fish' ); As you see, his checklist has only 10 animals: 3 cows, and 7 fish. (story has it that the cat and 3 of the cows had a scheme to escape, right from the beginning. But that's another story....) So the PHP script should yield this: 3 cows ARRIVED! 7 fish ARRIVED! 3 cows 1 cat .... again, this is way *way* harder than it looks, because arrays are so finicky. When I saw your post, I immediately thought how easy this would be using mySQL: Simply read the textfile line by line into a temp mySQL table, and use "limit 1" for the queries. But I want to see if this can be cracked using PHP arrays...
-
Without the break, it puts "arrived!" after everything 3 horses arrived! 7 fish arrived! 3 horses arrived! 1 cat I'm trying to get PHP to move onto the next array value when a match is found. This is way *way* harder than it looks. Maybe arrays aren't the way to do this.
-
Noah is having trouble with his PHP code $ark = array( '3 cows', '7 fish', '3 cows', '1 cat' ); $received = array( '3 cows', '7 fish' ); foreach($ark as $expected) { echo $expected; foreach($received as $line) { if($expected == $line) { echo ' ARRIVED!'; } break; } echo '<hr>'; } The result should be: 3 cows ARRIVED! 7 fish ARRIVED! 3 cows 1 cat What can Noah do to fix his code?
-
Sure, no problem. Here is the script: <?php $data = [ [ 'price' => 8.50, 'postage' => 3.50 ], [ 'price' => 4.18, 'postage' => 0 ], [ 'price' => 5.25, 'postage' => .17 ], [ 'price' => 4.15, 'postage' => .02 ], [ 'price' => 2.50, 'postage' => 1.80 ] ]; usort($data, function($a, $b) { return $a['price'] + $a['postage'] - $b['price'] - $b['postage']; }); echo '<pre>' . print_r($data, 1) . '</pre>'; // $data[1] and $data[2] are in wrong position ?> Here is the result: <pre>Array ( [0] => Array ( [price] => 4.18 [postage] => 0 ) [1] => Array ( [price] => 4.15 [postage] => 0.02 ) [2] => Array ( [price] => 2.5 [postage] => 1.8 ) [3] => Array ( [price] => 5.25 [postage] => 0.17 ) [4] => Array ( [price] => 8.5 [postage] => 3.5 ) ) </pre> (I had to take some extra time to edit my post because the results are correct with certain numbers, but if I change certain digits, then the result will be wrong. It's "hit and miss" and I finally found some numeric values that produce the error. This error happens in both PHP 7.4, as well as PHP 8. The only way I got it to sort correctly is to get some chew toys and catnip and do: usort($data, function($a, $b) { $cat = $a['price'] + $a['postage']; $dog = $b['price'] + $b['postage']; if($cat > $dog) { $result = 1; } elseif ($cat < $dog) { $result = -1; } return $result; }); ***NEWSFLASH***EXTRA EXTRA READ ALL ABOUT IT ****** CHENXIU FINDS BUG IN PHP8 GETS AWARDED SCHOLARSHIP TO PHP ARRAY COLLEGE *******************
-
This specific array causes the problem: <?php $data = [ [ 'price' => 8.50, 'postage' => 3.50 ], [ 'price' => 6.18, 'postage' => 2.33 ], [ 'price' => 6.13, 'postage' => 0 ], [ 'price' => 4.19, 'postage' => 2.63 ], [ 'price' => 2.50, 'postage' => 1.80 ] ]; usort($data, function($a, $b) { return $a['price'] + $a['postage'] - $b['price'] - $b['postage']; }); echo '<pre>' . print_r($data, 1) . '</pre>'; // $data[1] and $data[2] are in wrong position ?> $data[1] and $data[2] are not in the correct position. But if I do something like the following code, then the result is correct: <?php $data = [ [ 'price' => 8.50, 'postage' => 3.50 ], [ 'price' => 6.18, 'postage' => 2.33 ], [ 'price' => 6.13, 'postage' => 0 ], [ 'price' => 4.19, 'postage' => 2.63 ], [ 'price' => 2.50, 'postage' => 1.80 ] ]; usort($data, function($a, $b) { $cat = $a['price'] + $a['postage']; $dog = $b['price'] + $b['postage']; if($cat > $dog) { $result = 1; } elseif ($cat < $dog) { $result = -1; } return $result; }); echo '<pre>' . print_r($data, 1) . '</pre>'; // The result order is now correct. ?>
-
Dogs and cats fix the error: <?php $data = [ [ 'price' => 8.50, 'postage' => 3.50 ], [ 'price' => 6.49, 'postage' => 0 ], [ 'price' => 6.10, 'postage' => 0 ], [ 'price' => 10.00, 'postage' => 1.50 ], [ 'price' => 2.50, 'postage' => 1.80 ] ]; usort($data, function($a, $b) { $dog = $a['price'] + $a['postage']; $cat = $b['price'] + $b['postage']; return $dog - $cat; }); echo '<pre>' . print_r($data, 1) . '</pre>'; ?>
-
When there is zero, the result is incorrect: <?php $data = [ [ 'price' => 8.50, 'postage' => 3.50 ], [ 'price' => 6.18, 'postage' => 2.33 ], [ 'price' => 6.13, 'postage' => 0 ], [ 'price' => 4.19, 'postage' => 2.63 ], [ 'price' => 2.50, 'postage' => 1.80 ] ]; usort($data, function($a, $b) { return $a['price'] + $a['postage'] - $b['price'] - $b['postage']; }); echo '<pre>' . print_r($data, 1) . '</pre>'; ?>
-
Interesting: It appears that usort may not sort as anticipated when using decimal numbers (e.g. a dollar amount of $18.53 in my "item price + postage cost" example above). Multiplying by 100 seems to fix it. return (100 * $a['item_price']['value']) + (100 * $a['postage']['price']['value']) - (100 * $b['item_price']['value']) - (100 * $b['postage']['price']['value']);
-
I'm gonno I'm gonna use that word in a sentence every day for the next week! 😀
-
@Barand, that works! Thank you! Yes, I was advised to use usort() ....BUT I quickly found that usort (like sort, array_multisort, et al) requires the programmer have a basic understanding of arrays (e.g. array keys, key/value pairs, etc.), and this is the hardest concept for me to wrap my brain around. My experience of seeing array code such as yours is akin to shock and awe a non-technologically advanced tribal villager experiences when they see a satellite..... or a cellphone for the first time. Or when the UFO crashed at Roswell and army personnel were presented with anti-gravity technology......... "How the h*** do they know this stuff?" I've started from the basics, "Arrays 101," but the teaching examples are dumbed down, e.g. $array = array('a' , 'b' , 'c'); "This is how you can magically count how many elements are in $array: use 'array_count'" Wow, how useful. Not. And then if I go onto google / Stack Overflow and type keywords like "sort array based on sum" (like I did 1000 times already), there are these convoluted questions from people with ridiculous arrays (usually of sports teams and their stats, etc., or some professor trying to keep track of their students in Excel, etc.), and NO WAY to extrapolate clues from the "voted best answer" to apply to my own array question. Like the dog who really doesn't understand the relationship between the owner, the can opener sound, and the bowl of food that appears moments later, I really have no clue how you knew to start with "$listings = $data['listings']," but I'm happy it's now sitting in front of me. 😀
-
Thank you, but I still can't make it work with my specific array. To get to the item price, and delivery price, I need to loop through the array, and I cannot seem to do this without errors. To demonstrate that I am really trying...... I believe this gets me to the relevant part: foreach($data["listings"] as $each) { ....and once I'm there, then I have $each["item_price"]["value"] and $each["postage"]["price"]["value"] And therefore, the total item cost will be: $total_price = $each["item_price"]["value"] + $each["postage"]["price"]["value"]; After that, I have no clue..... I am forcing myself to learn Arrays, but there comes a point where I must ask for help when I get totally stumped. Thank you.
-
$data is an array of customers. This array shows the Customer's name, the item price, and the postage cost. The total cost will be "item price + postage cost." How do I sort this array based on the total cost? (the sum of postage and item) print_r($data); // prints this in my browser: Array ( [listings] => Array ( [0] => Array ( [item_price] => Array ( [value] => 60.00 ) [postage] => Array ( [price] => Array ( [value] => 20.00 ) ) [customer] => Array ( [name] => Barbara ) ) [1] => Array ( [item_price] => Array ( [value] => 40.00 ) [postage] => Array ( [price] => Array ( [value] => 30.00 ) ) [customer] => Array ( [name] => Fred ) ) [2] => Array ( [item_price] => Array ( [value] => 70.00 ) [postage] => Array ( [price] => Array ( [value] => 20.00 ) ) [customer] => Array ( [name] => Julie ) ) ) ) The result should be from lowest total to highest total. I tried this, but only got errors: $price = array_column($data["listings"]["item_price"] + $data["listings"]["postage"]); array_multisort( $price , SORT_ASC , $data["listings"] ); Am I going about this correctly? Thank you.
-
@mac_gyver THANK YOU! The $matches[1][$key] <--- This is the one thing that I didn't try. That "$key" variable in that spot. Thank you again!