So, I have been searching but I could not find something similair.
What I need is to merge 2 rows what I got from DB (stored in an Array) and fill out empty fields.
We have 2 keys FIXED and WEIGHT and they have different values in columns. If we use FIXED type we do not use values from WEIGHT and those columns are empty, now I have to merge those 2 2rows into one.
There is N rows (but always in pair).
Here is example
id type postal fix_price weight_price our_price your_price product_id group_id
1 fixed 8888 50 - 50 - 1 2
2 weight 8888 - 100 - 100 1 2
3 fixed 7777 20 - 20 - 1 2
4 weight 7777 - 30 - 30 1 2
And I need result like below:
id postal fix_price weight_price our_price your_price product_id group_id
1 8888 50 100 50 100 1 2
2 7777 20 30 20 30 1 2
Thank you for helping!
Try:
$collect=array();
$useType='fixed';
foreach($array as $set){
$check=$set['type'];
unset($set['type']);
if(!isset($collect[$set['postal']])) {
$collect[$set['postal']] = $set;
} else {
foreach($set as $k=>$v){
if($k=='id' && $check!=$useType) {
continue;
}
if(!empty($v)){
$collect[$set['postal']][$k] = $v;
}
}
}
}
$collect=array_values($collect);
var_export($collect);
This produces your expacted result, but i dont thing that is what you really want.
I don't really understand how your question is related with the (very different) data structure you shared in your comment under the #JustOnUnderMillions answer, so here is an answer based on the structure suggested by your initial question.
Taking advantage of your other comment where you say "I have tried with $arra1 + $array2" I guess you're able to first format the source data like this:
$fixed = [
8888 => [50, NULL, 50, NULL, 1, 2],
7777 => [20, NULL, 20, NULL, 1, 2],
];
$weight = [
8888 => [NULL, 100, NULL, 100, 1, 2],
7777 => [NULL, 30, NULL, 30, 1, 2],
];
Then it's pretty easy to use this simple code:
foreach ($fixed AS $postal => $fixed_data) {
$weight_data = $weight[$postal];
foreach ($fixed_data AS $key => $fixed_value) {
$all[$postal][$key] = $fixed_value ? $fixed_value : $weight_data[$key];
}
}
which gives the expected result:
echo '<pre>' . print_r($all, TRUE) . '</pre>';
/*
Array
(
[8888] => Array
(
[0] => 50
[1] => 100
[2] => 50
[3] => 100
[4] => 1
[5] => 2
)
[7777] => Array
(
[0] => 20
[1] => 30
[2] => 20
[3] => 30
[4] => 1
[5] => 2
)
)
*/
Related
this questin is asked many times but every one using same array but in my case i have 2 arrays
consider i have 2 arrays
array1:3 [
10 => 900.0
20 => 450.0
30 => 600.0
]
array2:3 [
30 => 200.0
10 => 500.0
20 => 600.0
]
output should be
[900.0 - 500 = 400 // according to same id 10 = 10
450.0 - 600 = -150 // 20 = 20
600.0 - 200 = 400 // 30 = 30
]
in this array consider 10,20,30 are ids and next is value i want output where compare ever id and get difference example if (id1 = id2 ){ id1 => value - id2 => value }
i need help in that code which i already tried
$getsellerreport = SellerSellsReport::where('seller_id' , $seller_id);
$getunitdiff = $getsellerreport->pluck('unit')->toArray();// [0 => 75 1 => 500 => 100]
$getamountdiff = $getsellerreport->pluck('amount')->toArray(); // [0 => 11000 => 40 2 => 900]
$getproductdiff = $getsellerreport->pluck('product_id')->toArray(); // [0 => 39 1 => 242 => 23]
foreach($product_report as $preport){
$unit[] = $preport['unit'];// [0 => 75 1 => 25 2 => 100]
$amount[] = $preport['amount'];// [0 => 900 1 => 450 2 => 600]
$product_id[] = $preport['product_id'];// [0 => 23 1 => 242 => 39]
} // here we get array two values
above code get values with starting 0 key value and on below for() loop we can use product_id to compare both product id and get unit and amount but i dont know how i can do that can someone help me?
for ($i = 0 ; $i < sizeof($amount) ; $i++){
$unitdiff[] = $getunitdiff[$i] - $unit[$i];
$amountdiff[] = $getamountdiff[$i] - $amount[$i];
}
You could collect the arrays and use map, here is a sample to get you started:
$a = [
10 => 900.0,
20 => 450.0,
30 => 600.0,
];
$b = [
30 => 200.0,
10 => 500.0,
20 => 600.0,
];
$x = collect($a)->map(function($aItem, $index) use ($b) {
return $aItem - $b[$index];
});
dd($x); // yields [ 10 => 400.0, 20 => -150.0, 30 => 400.0 ]
I have an array of id's and I want to filter those id's to last 5 and unique ids.
$recently_viewed_ids
array:16 [▼
0 => 1
1 => 2
2 => 1
3 => 2
4 => 8
5 => 7
6 => 6
7 => 6
8 => 6
9 => 5
10 => 8
11 => 4
12 => 1
13 => 1
14 => 1
15 => 1
]
Here is my code and it's messing up because I'm getting 85672
$items = array_slice(array_unique(array_reverse($recently_viewed_ids)), -5);
Output I am expecting
14856
You need to use next combination of array functions:
array_slice( // get first 5 values
array_unique( // get only unique values
array_reverse($arr) //reverse array for get last values
)
,0,5);
Code example here: PHPize.online
In Laravel, I believe you can rewrite the correct answer to:
return collect($arr)
->reverse()
->unique()
->slice(0, 5)
->all();
try this :
$items = array_slice(array_unique(array_reverse($recently_viewed_ids)), 5);
I am trying to build a website that will display the text in multiple languages.
I have a table 'text' with all the languages. If the text does not exist in the chosen language it has to display the default language.
query SELECT * FROM text WHERE TextId = 10
results in
Id TextId LanguageId Text
10 10 1 first name
13 10 2 名前
If I r_print this result I get something like this
Array ( [0] => Array ( [0] => 10 [Id] => 10 [1] => 10 [TextId] => 10 [2] => 1 [LanguageId] => 1 [3] => first name [Text] => first name )
[1] => Array ( [0] => 13 [Id] => 13 [1] => 10 [TextId] => 10 [2] => 2 [LanguageId] => 2 [3] => 名前 [Text] => 名前 ) )
How can I check that LanguageId 2 exist in this array ?
the problem is that it is possible that TextId 2 and Id 2 can also exist in this array.
Is this possible to do with in_array()?
Here is a function that can check if LanguageId equals a special value .
function isLanguageIdExists($yourArray , $LanguageId){
$exists=false;
foreach($yourArray as $array){
if(isset($array['LanguageId'])&& $array['LanguageId'] == $LanguageId){
$exists=true;break;
}
}
return $exists;
}
$exist = isLanguageIdExists($yourArray , 2);//return true or false
You can check by isset the key and match the value in php.
$dataArray = array(
0 => array(0 => 10 ,'Id' => 10, 1 => 10, 'TextId' => 10, 2 => 1, 'LanguageId' => 1),1 => array(0 => 10 ,'Id' => 10, 1 => 10, 'TextId' => 10, 2 => 1, 'LanguageId' => 1)
);
foreach($dataArray as $value) {
if(isset($value['LanguageId']) && $value['LanguageId'] == 2) {
echo 'language ID 2 is available';
}
}
Working Demo
After giving this some more thought I came up with a maybe not so elegant solution.
Instead of getting an array back I modified the SQL Query to give one row back with the default language (English) and a user selected language (Japanese).
It uses two left joins. This shows that I received (some) training in SQL but am really not at ease with multidimensional arrays.
Query
def_text = text in default language
usr_text = text in user chosen language
$table01 = "text";
$query="SELECT $table01.TextId,
text_def.Text as def_text,
text_usr.Text as usr_text
FROM $table01
LEFT JOIN $table01 as text_def ON $table01.TextId = text_def.TextId AND text_def.LanguageId = $_SESSION[default_language]
LEFT JOIN $table01 as text_usr ON $table01.TextId = text_usr.TextId AND text_usr.LanguageId = $_SESSION[language]
WHERE $table01.TextId=$mess;";
after getting back the results it is easy to check with isset() or empty() to see if the text is available in the user selected language
I have this small problem although it's small i can't seem to work it out, I've set of data i need to display, lets say 1 to 17. i need to display 3 in a row like 1,2,3 in one row and 4,5,6 in the next because bootstrap row support 12 columns and there are 3 elements of 4 columns each.
Because the amount of data can vary and the total number of data won't divide by 3 like the example it's 17 how can I write something in PHP that will display the data 3 in a row and like in this example there will be 5 rows of 3 and a last row having 2 sets.
Thanks
Edit:
I didn't write any code of this but was thinking a loop and a nested loop but think that's too clunky any better way of doing this?
You can use following code :
for($i = 1;$i<=17;$i++){
if($i%3 !=1 && $i%3 != 0){
print_r($i." , ");
}else if( $i%3 == 0){
print_r($i);
}
else{
print_r("<br/>".$i." , ");
}
}
It'll give you output like this:
1 , 2 , 3
4 , 5 , 6
7 , 8 , 9
10 , 11 , 12
13 , 14 , 15
16 , 17 ,
Rukshan you can use the modulus operator.
You can use the code below as an example. I have mixed html and php, but it is just to show you an example:--
<?php
echo "<table>";
for($i=1;$i<18;$i++)
{
echo "<tr>";
echo "<td>".$i."</td>";
if($i%3 == 0) echo "</tr>";
}
echo "</table>";
?>
Try using array_slice() to slice your array as per your need. You will get your division in arrays. Loop through them to create your table.
Reference Example
$stores = array(1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12);
$division = ceil( count($stores) / 3 ); //to divide array into 3 halves
$firstHalf = array_slice($stores, 0, $division);
$secondHalf = array_slice($stores, $division, $division);
$thirdHalf = array_slice($stores, $division * 2);
Output for $stores = array(1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12)
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
Array
(
[0] => 5
[1] => 6
[2] => 7
[3] => 8
)
Array
(
[0] => 10
[1] => 11
[2] => 12
)
Output for $stores = array(1, 2, 3, 4, 5, 6, 7, 8, 10)
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
Array
(
[0] => 5
[1] => 6
[2] => 7
[3] => 8
)
Array
(
[0] => 9
[1] => 10
)
Output for $stores = array(1, 2, 3, 4, 5, 6, 7, 8);
Array
(
[0] => 1
[1] => 2
[2] => 3
)
Array
(
[0] => 4
[1] => 5
[2] => 6
)
Array
(
[0] => 7
[1] => 8
)
To divide the array in two halves you can use
$division = ceil( count($stores) / 2 );
$firstHalf = array_slice($stores, 0, $division);
$secondHalf = array_slice($stores, $division);
It's 2 days that i'm trying to understand why i get so illogical results. Let me explain with an example. I create invoices based on this simple array.
Array
(
[0] => Array
(
[userid] => 208
[number_of_services] => 3
[username1] => sara
[income1] => 10
[username2] => mark
[income2] => 18
[username3] => joe
[income3] => 12
)
[1] => Array
(
[userid] => 4
[number_of_services] => 1
[username1] => bruce
[income1] => 5
)
[2] => Array
(
[userid] => 303
[number_of_services] => 1
[username1] => michael
[income1] => 7
)
)
With this array i want to create 3 invoices for clients with the following ids: 208, 4, 303. I can make it with with a simple foreach.
foreach($myarray AS $key => $value)
{
// create my invoice
}
Now as you can see client with ID 208 has multiple usernames. I store them as [username1], [username2], [username3] and so on.
[0] => Array
(
[userid] => 208
[number_of_services] => 3
[username1] => sara
[income1] => 10
[username2] => mark
[income2] => 18
[username3] => joe
[income3] => 12
)
I want to display each username as new line (item) in the invoice so i do the following:
foreach($myarray AS $key => $value)
{
for($n=1; $n<=$value["number_of_services"]; $n++)
{
$invoice["itemdescription$n"] = "Username: ".$value["username$n"]."";
$invoice["itemamount$n"] = $value["income$n"];
$invoice["itemtaxed$n"] = 1;
}
}
Now with this cycle i should have:
1 invoice for client 4 with 1 item (bruce -> 5 euro)
1 invoice for client 303 with 1 item (michael -> 7 euro)
1 invoice for client 208 with 3 item (sara -> 10, mark -> 18, joe -> 12)
But i always get:
1 invoice for client 4 with 3 item (bruce -> 5 euro, mark -> 18, joe -> 12)
1 invoice for client 303 with 3 item (michael -> 7 euro, mark -> 18, joe -> 12)
1 invoice for client 208 with 3 item (sara -> 10, mark -> 18, joe -> 12)
I don't get the reason why the script keeps "merging" [username2], [income2] and [username3], [income3] of client 208 into client 4 and 303! I debugged the script in every single step and the problems seems to be in the for() cycle.
Any idea?
foreach($myarray AS $key => $value)
{
for($n=1; $n<=$value["number_of_services"]; $n++)
{
$invoice[] = array(
"itemdescription" => "Username: " . $value . "username" . $n,
"itemamount" . $n = $value . "income" . $n,
"itemtaxed" . $n = 1
);
}
}
Try this.