Array in php codeigniter framework - php

I am having problem with the array order problem, array coming from the database is correct but when i do print_r($data) each and every time i refresh the pages the order of the array change.
Array
(
[0] => stdClass Object
(
[prodtmappid] => 53624
[totalrating] => 5.00
[SKUNO] => C46P53624R7621
[ProId] => 31369
[CatId] => 46
[proQnty] => 15
[productMRP] => 1299
[ProDisPrice] => 0
[SellingPrice] => 390
[AdditionDiscount] => 0
[FinalPrice] => 390
[ProDiscount] => 70
[ProDisType] => percentage
[ProName] => Trendy Bandey Men TB20 Yellow Tshirt
[TotalValues] => 0.00
[image] => MCTSTB-TB20-YL_1_55x_885.JPG
[BrandId] => 4233
[FDisId] => 7
[ColorID] => 191
[size_Val] => M
)
[1] => stdClass Object
(
[prodtmappid] => 9526
[totalrating] => 0.00
[SKUNO] => C46P9526R1870
[ProId] => 3351
[CatId] => 46
[proQnty] => 8
[productMRP] => 1899
[ProDisPrice] => 0
[SellingPrice] => 1329
[AdditionDiscount] => 0
[FinalPrice] => 1329
[ProDiscount] => 30
[ProDisType] => percentage
[ProName] => FCUK 56DPJ Pink Men T-Shirt
[TotalValues] => 0.00
[image] => FCUK2_1_28x_885.JPG
[BrandId] => 1285
[FDisId] => 4
[ColorID] => 194
[size_Val] => S
)
so what problem i am facing is array[0]['prodtmappid'] will have diffrent values on every page refress.

What exactly is your query? From the results, it looks like it's ORDER BY prodQnty DESC and GROUP BY catId. If you want to keep the results consistent, add ORDER BY prodtmappid to your query.

Related

search inside arrays with condition in php

I have an api with a list of arrays inside one array and each array inside this array it has one key [attributes] and inside this key one array with key [CITY_NAME]
this is my array coming from api
this array number is 0 but every day this number is changed and my figures come wrong, what is the good way to always have my city figures using the key [CITY_NAME]
I am fetching the data using this code
$json = file_get_contents($url);
$json_data = json_decode($json, true);
$data = $json_data['features'];
$mycity = $data[0]['attributes'];
Array
(
[0] => Array
(
[attributes] => Array
(
[CITY_NAME] => city1
[Name] => city1
[ADMIN_NAME] => city1
[POP_CLASS] => 5,000,000 to10,000,000
[Population] => 7676654
[Population_Data_Source] => Wikipedia
[Population_Data_Date] => 2018
[CityID] => 14
[Longitude] => 46.7614868685786
[Latitude] => 24.7388786516234
[Confirmed] => 0
[Recovered] => 0
[Deaths] => 0
[Active] => 0
[Tested] => 0
[Name_Eng] => city1
[Join_Count] => 61
[Confirmed_SUM] => 5152
[Deaths_SUM] => 9
[Recovered_SUM] => 1407
[Active_SUM] => 3736
[Tested_SUM] => 376607
[ObjectId] => 14
)
)
[1] => Array
(
[attributes] => Array
(
[CITY_NAME] => city2
[Name] => city2
[ADMIN_NAME] => city2
[POP_CLASS] => 1,000,000 to 5,000,000
[Population] => 1675368
[Population_Data_Source] => Wikipedia
[Population_Data_Date] => 2010
[CityID] => 9
[Longitude] => 39.8148987363852
[Latitude] => 21.4273876500039
[Confirmed] => 0
[Recovered] => 0
[Deaths] => 0
[Active] => 0
[Tested] => 0
[Name_Eng] => city2
[Join_Count] => 59
[Confirmed_SUM] => 6848
[Deaths_SUM] => 85
[Recovered_SUM] => 1145
[Active_SUM] => 5618
[Tested_SUM] => 0
[ObjectId] => 9
)
)
Since I may have misunderstood and you may have many, just build a new array with CITY_NAME:
foreach($data as $values) {
$result[$values['attributes']['CITY_NAME']] = $values['attributes'];
}
Now you can access by CITY_NAME:
echo $result['city1']['Population'];
This might be easier. Extract all attributes from all arrays into an array, then create an array from that indexed by CITY_NAME:
$data = array_column(array_column($json_data['features'], 'attributes'),
null, 'CITY_NAME');

group same name from array and show total of an indivisual group

I have an array which look like this:
Array
(
[0] => stdClass Object
(
[products_name] => Parla to gold(Flower)
[measurement] => 18
[unit] => mm
[products_size_height] => 0
[products_size_width] => 0
[products_size_unit] => inch
[products_type] => 4
[products_thickness_measurement] => 18
[product_ordered_pcs] => 4
[product_ordered_quantity] => 100
[others_feature] =>
[rate] => 500
[amount] => 50000
)
[1] => stdClass Object
(
[products_name] => Parla to gold(Flower)
[measurement] => 18
[unit] => mm
[products_size_height] => 0
[products_size_width] => 0
[products_size_unit] => inch
[products_type] => 4
[products_thickness_measurement] => 18
[product_ordered_pcs] => 0
[product_ordered_quantity] => 45
[others_feature] =>
[rate] => 45
[amount] => 2025
)
[2] => stdClass Object
(
[products_name] => Parla to gold(Flower)
[measurement] => 18
[unit] => mm
[products_size_height] => 0
[products_size_width] => 0
[products_size_unit] => inch
[products_type] => 2
[products_thickness_measurement] => 18
[product_ordered_pcs] => 4
[product_ordered_quantity] => 100
[others_feature] =>
[rate] => 850
[amount] => 85000
)
.......
Now I want to group the same product (product will be identified by combination of products_name, measurement and products_type) to show one product at a time and make total of "product_ordered_quantity" of the same product. See the image you may understand what I am trying to say. I will generate PDF file so no javascript please.
result:
I want:
If this is a query from the DB you can use some methods provided by the Collection class (https://laravel.com/docs/5.4/eloquent-collections)
$query; //The query object BEFORE getting the data, so before get() or paginate()
$data = $query->selectRaw('sum(product_ordered_quantity) as quantity')
->groupBy('products_name','measurement', 'products_type')
->get();
You can give a try to this beautiful class, which will let you query to your array same as you query to the database - https://phplinq.codeplex.com/

PHP Sort Multi-Tired ARRAY by multiple sorts?

i have an php array of sports scores and i want to sort by the following
Wins Desc, Ties Desc, Points Scored Desc, Points Allowed Asc
anyone know how i can do this?
This array was generated by multiple mysql quires
[14] => Array
(
[title] => Crushers
[team_color] => #0000ff
[wins] => 0
[losses] => 1
[ties] => 1
[pointsscored] => 18
[pointsallowed] => 19
)
[15] => Array
(
[title] => Purple Stars
[team_color] => #ae25d9
[wins] => 1
[losses] => 1
[ties] => 0
[pointsscored] => 20
[pointsallowed] => 21
)
[16] => Array
(
[title] => Lighting Boltz
[team_color] => #d6c128
[wins] => 0
[losses] => 0
[ties] => 2
[pointsscored] => 12
[pointsallowed] => 12
)
[17] => Array
(
[title] => The Black Cheetahs
[team_color] => #000000
[wins] => 1
[losses] => 0
[ties] => 1
[pointsscored] => 15
[pointsallowed] => 13
)
I made a function to sort every multidimensional array by one of his columns ,look at array_multisort() and customize this function to your need:
function sortArrayBy($array , $column_name,$sort=SORT_DESC){
foreach ($array as $key => $row) {
$column[$key] = $row[$column_name];
}
array_multisort($column, $sort, $array);
return $array;
}
Call it like this :
<?php sortArrayBy($yourArray,'date') ; ?>

Removing row from array based on duplicate value of a key

I have a array called $data['job_list'] which is as follows
Array
(
[0] => Array
(
[job_id] => 2
[job_title] => JQuery developer
[job_desc] => Developer
[job_slug] => 2-JQuery-developer
[job_type] => 191
[job_skill] => 2
[job_salary] => 2
[job_experience] => 1
[company_name] => IGUTS
[company_desc] => IGUTS is a fresh company
[company_industry] => 24
[company_address] => 35 Lawrence Street
[company_state] => 35
[company_city] => 650
[user_id] => 1
[concerned_fname] => Saswat
[concerned_lname] => Routroy
[contact] => 8961287928
[date_of_post] => 26-04-2014
[job_timestamp] => 1398517810
[industry_id] => 191
[industry_title] => Web Designer/Developer
[p_cid] => 24
[industry_slug] => 191-Web-Designer-Developer
[industry_desc] =>
[industry_image] =>
[industry_priority] => 0
[industry_timestamp] => 1396535046
)
[1] => Array
(
[job_id] => 1
[job_title] => PHP developer
[job_desc] => Developer
[job_slug] => 1-PHP-developer
[job_type] => 191
[job_skill] => 1,2
[job_salary] => 1
[job_experience] => 1
[company_name] => IGUTS
[company_desc] => IGUTS Company
[company_industry] => 24
[company_address] => 35 Lawrence Street
[company_state] => 35
[company_city] => 650
[user_id] => 1
[concerned_fname] => Saswat
[concerned_lname] => Routroy
[contact] => 8961287928
[date_of_post] => 18-04-2014
[job_timestamp] => 1397842605
[skill_id] => 2
[skill_title] => JQuery
[skill_slug] => 2-JQuery
[industry] => 24
[skill_timestamp] => 1397395987
)
[2] => Array
(
[job_id] => 2
[job_title] => JQuery developer
[job_desc] => Developer
[job_slug] => 2-JQuery-developer
[job_type] => 191
[job_skill] => 2
[job_salary] => 2
[job_experience] => 1
[company_name] => IGUTS
[company_desc] => IGUTS is a fresh company
[company_industry] => 24
[company_address] => 35 Lawrence Street
[company_state] => 35
[company_city] => 650
[user_id] => 1
[concerned_fname] => Saswat
[concerned_lname] => Routroy
[contact] => 8961287928
[date_of_post] => 26-04-2014
[job_timestamp] => 1398517810
[skill_id] => 2
[skill_title] => JQuery
[skill_slug] => 2-JQuery
[industry] => 24
[skill_timestamp] => 1397395987
)
)
[job_id] => 2 is present twice
What I want is that, the row with duplicate job_id => 2 should be removed
How can I achieve that??
$jobIds = array();
foreach ($jobs as $key => $job) {
if (in_array($jobIds, $job['job_id'])) {
unset($jobs[$key]);
continue;
}
$jobIds[] = $job['job_id'];
}
print_r($jobs);
I would cycle though each "job" and create an array of Job IDs. If I see the same ID twice then I would remove the duplicate and store it somewhere else if needed.
As Matei Mihai stated, i made some minor changes in his script and bingo it solved..
Matei Mihai made a mistake regarding the data-type of arguments of the in_array() function
I fixed that.
here's my code
$jobIds = array();
for($i = 0;$i < count($data['job_list']); $i++ )
{
if (in_array($data['job_list'][$i]['job_id'], $jobIds))
{
unset($data['job_list'][$i]);
continue;
}
$jobIds[] = $data['job_list'][$i]['job_id'];
}

PHP simple array manipulation for filtering common keys

I know it's simple task, but my mind not clicking at present for appropriate solution.
I have 2 arrays
$array1=Array
(
[302] => Array
(
[id] => 302
[medical_id] => 55
[medication_name] => disprin
[medication_frequency] => 1
[medication_status] => Stoped
[users_id] => 106
[update_date] => 2012-03-02 11:03:57
)
)
$array2=Array
(
[302] => Array
(
[medication_id] => 302
[id] => 14
[medical_id] => 55
[medication_name] => disprin
[medication_frequency] => 2
[medication_status] => Stoped
[users_id] => 106
[update_date] => 2012-03-02 11:03:57
[change_date] =>
)
[305] => Array
(
[medication_id] => 305
[id] => 15
[medical_id] => 57
[medication_name] => disprin
[medication_frequency] => 2
[medication_status] => Continued
[users_id] => 106
[update_date] => 2012-03-02 11:02:46
[change_date] =>
)
)
I want Output:
$outputarray=Array
(
[302] => Array
(
[id] => 302
[medical_id] => 55
[medication_name] => disprin
[medication_frequency] => 1
[medication_status] => Stoped
[users_id] => 106
[update_date] => 2012-03-02 11:03:57
)
[305] => Array
(
[medication_id] => 305
[id] => 15
[medical_id] => 57
[medication_name] => disprin
[medication_frequency] => 2
[medication_status] => Continued
[users_id] => 106
[update_date] => 2012-03-02 11:02:46
[change_date] =>
)
)
means if same key is exist in array1 then take it from array1 else from array2.
I tried with some PHP functions like array_merge,array_merge_recursive but not got desired output.
am not able to recollect memory now, Please suggest me any PHP array function to sort out this.
It's so simple that it's almost unethical:
$outputArray = $array1 + $array2;
Array union (above) does what you describe. Using array_merge could also work in general, but not directly with such input because your keys are integers.
Maybe this http://php.net/manual/pt_BR/function.array-intersect.php
Check with in_array() & store using array_push() will be a solution for you.

Categories