How to display data without redundancy - php

i want to display in my system just one record with the same id.
for example: 01,01,02,02,03,03.
I just want to display 01,02,03.

The array_unique() function removes duplicate values from an array. If two or more array values are the same, the first appearance will be kept and the other will be removed.
$number = "01,01,02,02,03,03";
$result = implode(',',array_unique(explode(',', $number )));
echo $result; // output 01,02,03
Happy Programming

Related

Insert/update data to database from list randomly

So I have this db with a table called users.
In here is column called work, which is currently empty for all users. I've made a list containing many job-titles, and now I want this to be added into the users table under work column.
I figured I'd do something with a array like this:
$input = array("worktitle1", "worktitle2", "worktitle3");
$rand_keys = array_rand($input, 1);
and then update rand_keys in a simple query:
$mysqli->query('UPDATE users SET work = '.$rand_keys.'');
However, when I did that, only a number value was inserted and it was the same for all of the xx users I haven in the db.
How can I fix so that it insert a random worktitle for every single row? And not the same worktitle for every users. I thought about doing rand() in the query, but am unsure of this works
BONUS QUESTION: If I want my array_rand to prioritise some worktitles (so in the db a specific worktitle will show 10% more times than others), how can I do that?
I am assuming that you have for loop or while or some array which you want to loop through and insert the dummy data in database table.
I think its because you have used array_rand before the for loop and then using inserting into the database of the random key which you had earlier
Eg:
<?php
$input = array("worktitle1", "worktitle2", "worktitle3");
foreach($somearry as $key => $values){
//Now you need to call the array_rand function
$randomKey = array_rand($input, 1); //This will return only the random key of the array
$randomWork = $input[$randomKey]; //This line will give you the random key value of the input array
//Now write your code *here* to insert this random value ie update query
}
Using while loop as per your request
while(some_condition){
//Now you need to call the array_rand function
$randomKey = array_rand($input, 1); //This will return only the random key of the array
$randomWork = $input[$randomKey]; //This line will give you the random key value of the input array
//Now write your code *here* to insert this random value ie update query
}
Even you can do the following hard coded way.
Note : This is just for the sake of demonstration for the user so that
he can understand it properly.
for($i = 0; $i < 10; $i++){
//Now you need to call the array_rand function
$randomKey = array_rand($input, 1); //This will return only the random key of the array
$randomWork = $input[$randomKey]; //This line will give you the random key value of the input array
//Now write your code *here* to insert this random value ie update query
$mysqli->query("UPDATE users SET work = '$randomWork'");
}
As written, your code selects one title at random and then updates all rows with that title, which is I think what you are saying. You need to loop through all users, select a title, and then set the title "WHERE userid='xxx'" or some other way of selecting specific users.
Weighted randomness. Take you array of titles and add extra entries for the ones you want to weight. If "CEO" is to be 10% more likely to be used, make sure the array has 10 entries of "CEO".

Put values in array and Get values using foreach

e.g. I have this Product Id values 31,32 from database. I want to put them on array. So, I can use the values for foreach.
What I want to achieve:
I want to get the stock of each product from database according to the given values (31,32).
What I tried:
$product_values = '31,32'; //this values can be different, sample values only
$arr_product_values = array();
$arr_product_values[] = $product_values;
foreach ($arr_product_values as $prod_id) {
echo $prod_id;
}
Expected output:
31 & 32
$arr_product_values[] = $product_values;
That doesnt mean you have a new array with those 2 values now. You just took a comma separated string and assigned it to an array element, that doesnt make it an array itself.
$arr_product_values = array(31,32);
Does.
Now you can loop over it

Editing the object returned by $wpdb->get_results(SQL)

We have a nasty database call using the Wordpress function $wpdb->get_results(SQL).
After receiving the result in PHP, we need to make a few changes to the result.
So can anyone tell me how I can:
1) Remove specific rows from the get_results() returned object.
2) Change the values of the specific columns in specific rows in the returned object.
I.e. if the object returned is $nastyData, we need to:
1) Remove specific rows from $nastyData
2) Change the value of specific columns in specific rows in $nastyData, for example $nastyData->name for a specific row.
Any ideas?
I have thought about makeing get_results() return the data as an array, but that will create problems in other places in our code (where the code expects to receive an object).
Thanks,
Mads
To start with, your "Nasty database call" should be optimized to be less nasty. More specifically, only query the results you want so that you don't have to remove stuff afterwords. This is the best solution.
If you insist on trying to modify the objects, this is a workaround. According to the documentation, when returning objects, they are returned in one of two ways:
OBJECT - result will be output as a numerically indexed array of row objects.
OBJECT_K - result will be output as an associative array of row objects, using first column's values as keys (duplicates will be discarded).
So, knowing that the result is an array of objects, we can get to each individual instance using a foreach construct:
$results = $wpdb->get_results( $nastySQL );
foreach($results as $index => $result)
{
// Change name column to FirstName using copy and delete
$tmp = $result->name;
unset($result->name);
$result->FirstName = $tmp;
// Remove specific row
if( $result->name == "Tom")
{
unset($results[$index]);
}
}
Below code will replace the value coming from specific field of database table, if name field has value like Reo and you want to replace it with other name like John then you can to do this via below code
$results = $wpdb->get_results( $nastySQL );
foreach($results as $key => $value){
$results[$key]->name= 'John';
}
return $results;

Compare 2 arrays remove the duplicates that appear in both in php

I have two arrays, I want to remove the duplicate(the values that appear in both) And remain with values that are not in Array A. For example I have array Master_arr and Log_arr. I want to remove the duplicates that appear in both and add in master_arr values that dont exist from log_arr. Master_arr contains CAT,DOG,RABBIT and Log_arr contains CAT,DOG,RABBIT,CAR,PHONE.
I want to add CAR and PHONE to Master_arr.
$master_arr = array(CAT,DOG,RABBIT);
$log_arr = array(CAT,CAR,RABBIT,DOG,PHONE);
$unique=array_unique( array_merge($master_arr, $log_arr) );
print_r($unique);
try this
$master_arr = array(CAT,DOG,RABBIT);
$log_arr = array(CAT,CAR,RABBIT,DOG,PHONE);
$unique=array_unique( array_merge($master_arr, $log_arr) );
$master_arr=array_diff($unique, $master_arr);
print_r($master_arr);
I believe you want
$unique = array_diff(array_merge($master_arr, $log_arr), $master_arr);

Count amount of similar items in array, return value if under 3

I have an array which contains sets of three similar named items; however, sometimes there's only two items in a set and I want to call these out.
<?php
$items = array(
'reviewpitfighter-1.138x88.jpg',
'reviewpitfighter-2.138x88.jpg',
'reviewpopfulmailsegacd-1.138x92.jpg',
'reviewpopfulmailsegacd-2.138x76.jpg',
'reviewpopfulmailsegacd-3.138x97.jpg'
);
?>
You'll note that there are two reviewpitfigher* items, and three reviewpopfulmailsegacd* items. I've started down a rabbit hole of loops and feel that there is something simple I'm just glossing over.
May be you can do this as a 2 stage process.
Stage 1:
Loop through the original array and form another set of array with its key as the value of this original array. Then save the repetition count in each of those new arrays.
Stage 2:
Loop through the new set of arrays and then pick out the arrays which has values less than 3 and retrieve its key.
Hope this helps!!
Here's the solution I came up with, sorry it took so long to post.
foreach($images as $value){
$lastItem = explode('-', $images[$count - 1]);
$parts = explode('-', $value);
if(preg_match('/^1/', $parts[1]) && $count != 0){
if(preg_match('/^2/',$lastItem[1])){
$imgurl = preg_replace('/^p?review/','',$lastItem[0]);
$sql = 'SELECT field FROM table WHERE field = "' . $imgurl . '"';
$result = $dbConn->FetchArray($dbConn->Query($sql), MYSQL_ASSOC);
$array[$imgurl] = $result;
}
}
$count++;
}
I get an array of all the images, then I check to see if I'm looking at the first image, if I am then I see if the last image I looked at was the second image. At this point I then call into the database to get some information to display a neatly messaged out put of what reviews are missing a third image. In the end $array contains this list which I can loop over.

Categories