PHP push multidimensional array - php

I got some data from mySQL (Name & Score) and want so push it into an array so I can sort it.
$stack = array();
while ($row = mysqli_fetch_array( $db_erg, MYSQLI_BOTH))
{
$stack_data = array($row['Name'] => $row['Score']);
array_push($stack, $stack_data);
}
//asort($stack);
print_r($stack);
I didn't work to sort it. And when I print my $stack Array it looks like this:
Array ( [0] => Array ( [Nina] => 94 ) [1] => Array ( [Tina] => 50 ) [2] => Array ( [Tim] => 50 ) [3] => Array ( [Anton] => 50 ) [4] => Array ( [Jim] => 50 ) [5] => Array ( [Tom] => 50 ) [6] => Array ( [Ed] => 50 ) [7] => Array ( [Bob] => 50 ) )

Do the sorting with your query:
SELECT `Name`,`Score` FROM `[tablename]` WHERE `Score`>=50 ORDER BY `Score` DESC,`Name`

Related

How to edit inner array inside another array in php?

I have an array coming as below which is dynamic. Now I want to edit a specific array for "BHD0000000002". I am getting the following array through ajax request. However I cannot edit the inner array element. For example, I am trying to edit the inner array value for the key "BHD0000000002" but unable to fix the issues. Can anyone help sir/madam?
This is the original array:
$budget_detail_array=Array
(
[0] => Array
(
[BHD0000000001] => 10000
)
[1] => Array
(
[BHD0000000002] => 12212121
)
[2] => Array
(
[BHD0000000003] => 212121
)
[3] => Array
(
[BHD0000000004] => 212121
)
[4] => Array
(
[BHD0000000005] => 212121
)
[5] => Array
(
[BHD0000000006] => 2121
)
[6] => Array
(
[BHD0000000007] => 2121
)
[7] => Array
(
[BHD0000000008] => 21221
)
[8] => Array
(
[BHD0000000009] => 2112212
)
)
I want to edit the above array as :
Array
(
[0] => Array
(
[BHD0000000001] => 10000
)
[1] => Array
(
[BHD0000000002] => 5000
)
[2] => Array
(
[BHD0000000003] => 212121
)
[3] => Array
(
[BHD0000000004] => 212121
)
[4] => Array
(
[BHD0000000005] => 212121
)
[5] => Array
(
[BHD0000000006] => 2121
)
[6] => Array
(
[BHD0000000007] => 2121
)
[7] => Array
(
[BHD0000000008] => 21221
)
[8] => Array
(
[BHD0000000009] => 2112212
)
)
So far I have tried with the following code but it is not working. Anyone can help?
foreach($budget_detail_array as $key=>$value){
foreach($value as $keyval=>$val){
if($keyval=='BHD0000000002'){
$val=5000;
}
}
/*if($value[$budget_id]){
$value[$budget_id]=100;
}else{
$value[$budget_id]=700;
}*/
}
print_r($budget_detail_array);exit;
In order to modify an associative array value you can do this:
$budget_detail_array[1]['BHD0000000002'] = 5000;
And then check the result again
print_r($budget_detail_array);

foreach in foreach multiple dimensional

I'd like to know how to avoid dupplicate of element while using a foreach in foreach with multidimensional array ?
The first level of my array can have several item (here's just 2, but maybe I can have 7 level). I've a lot trouble with this. Then this ID is going to be used as a parameter in a sql request, but this is another story.
This is my array :
Array
(
[0] => Array
(
[0] => Array
(
[id] => 10
)
[1] => Array
(
[id] => 11
)
[2] => Array
(
[id] => 12
)
)
[1] => Array
(
[0] => Array
(
[id] => 11
)
[1] => Array
(
[id] => 12
)
)
)
This is my foreach loop :
foreach($dataListe as $listeDiff){
foreach($listeDiff as $$item){
// echo $item[0].'<br />';
echo "<pre>".print_r($item, true)."</pre>";
}
}
Result :
Array
(
[id] => 10
)
Array
(
[id] => 11
)
Array
(
[id] => 12
)
Array
(
[id] => 11
)
Array
(
[id] => 12
)
Wanted :
Array
(
[id] => 10
)
Array
(
[id] => 11
)
Array
(
[id] => 12
)
use array_unique()
$result = [];
foreach($dataListe as $listeDiff){
$result[] = $listeDiff;
}
$result = array_unique($result);
Following should work
$dataListe = array(
array(array('id'=>10),array('id'=>20),array('id'=>20),array('id'=>10),array('id'=>20)),
array(array('id'=>10),array('id'=>30),array('id'=>20),array('id'=>10),array('id'=>20))
);
$result = array();
foreach($dataListe as $listeDiff){
foreach($listeDiff as $item){
if(!(in_array($item, $result))){
$result[] = $item;
echo "<pre>".print_r($item, true)."</pre>";
}
}
}
sample out put
Array
(
[0] => Array
(
[id] => 10
)
[1] => Array
(
[id] => 20
)
[2] => Array
(
[id] => 30
)
)

count same categories from array and Store in new array with its count and category name

I have one array which I am getting from database query response.
Now I want to count same categories and print in option_name array.
I have tried it with different array functions but want get desire output.
I have one idea to take new array and push it with foreach loop but not much idea of how can i achieve using code. Please help me to solve it.
Array
(
[0] => Array
(
[CNC] => Array
(
[id] => 5
[category_id] => 68
)
[GVO] => Array
(
[option_name] => Actors
)
)
[1] => Array
(
[CNC] => Array
(
[id] => 11
[category_id] => 72
)
[GVO] => Array
(
[option_name] => Cricketers
)
)
[2] => Array
(
[CNC] => Array
(
[id] => 3
[category_id] => 72
)
[GVO] => Array
(
[option_name] => Cricketers
)
)
[3] => Array
(
[CNC] => Array
(
[id] => 4
[category_id] => 74
)
[GVO] => Array
(
[option_name] => Musician
)
)
[4] => Array
(
[CNC] => Array
(
[id] => 7
[category_id] => 76
)
[GVO] => Array
(
[option_name] => Directors
)
)
[5] => Array
(
[CNC] => Array
(
[id] => 6
[category_id] => 76
)
[GVO] => Array
(
[option_name] => Directors
)
)
)
Desire Output:
Array
(
[Actors] => 1
[Cricketers] => 2
[Musician] => 1
[Directors] => 2
)
Thanks in advance!
You simply need to loop through the array using foreach like as
$result = [];
foreach($arr as $k => $v){
if(isset($result[$v['GVO']['option_name']])){
$result[$v['GVO']['option_name']] += 1;
}else{
$result[$v['GVO']['option_name']] = 1;
}
}
print_R($result);
You can count the option_name values by incrementing a counter in an associative array where the key is the option_name:
$counts = [];
foreach($array as $v) {
if(!isset($counts[$v['GVO']['option_name']])) {
$counts[$v['GVO']['option_name']] = 0;
}
$counts[$v['GVO']['option_name']]++;
}
print_r($counts);
Try this:
$new_arr = array();
foreach(array_column($your_arr, 'option_name') as $value){
if(in_array($value, $new_array)){
$new_array[$value] = $new_array[$value]+1;
}else{
$new_array[$value] = 1;
}
}
Output
Array
(
[Actors] => 1
[Cricketers] => 2
[Musician] => 1
[Directors] => 2
)

How to change the array in following scenario?

I've an SQL query as follows:
SELECT test_user_user_id FROM OCN.tests_users WHERE test_user_test_id = 99
$this->mDb->Query( $sql );
$students_data = $this->mDb->FetchArray();
I get the array as follows:
Array
(
[0] => Array
(
[test_user_user_id] => b9e6493f9f8599bc5a0a5935275228c2
)
[1] => Array
(
[test_user_user_id] => 395599c5891c1418357e2efa89bc3e27
)
[2] => Array
(
[test_user_user_id] => 3255605bb9fd3ecc0295a1bfb3cba147
)
[3] => Array
(
[test_user_user_id] => ebe6711fc156b2cc1a33f64f3d86150f
)
[4] => Array
(
[test_user_user_id] => 627b9c3f21d93f1e13af076cff20b143
)
[5] => Array
(
[test_user_user_id] => 030e96561c01afde1c46384f57cf8749
)
[6] => Array
(
[test_user_user_id] => 9def02e6337b888d6dbe5617a172c18d
)
)
Actually I want the array in following format:
Array
(
[0] => b9e6493f9f8599bc5a0a5935275228c2
[1] => 395599c5891c1418357e2efa89bc3e27
[2] => 3255605bb9fd3ecc0295a1bfb3cba147
[3] => ebe6711fc156b2cc1a33f64f3d86150f
[4] => 627b9c3f21d93f1e13af076cff20b143
[5] => 030e96561c01afde1c46384f57cf8749
[6] => 9def02e6337b888d6dbe5617a172c18d
)
How to get the array in above format?
$resArray = Array();
foreach($myArr as $_arr)
{
$resArray[] = $_arr['test_user_user_id'];
}
You could use array_map:
function user_id($e)
{
return $e['test_user_user_id'];
}
$students_data = array_map('user_id', $students_data);
$new_array = array();
$counter = 0; // optional... because by default starting index is 0... however if you want to change the index then use a counter
foreach($students_data as $row) {
$new_array[$counter] = $row['test_user_user_id'];
$counter++;
}
print_r($new_array);

show distinct result from two array

I have the following two arrays...
1) how could i get only the different key->value one?
2) how can i insert to mysql the second array?
// first array
$aa = Array
(
[t_a] => Array
(
[0] => Array
(
[f_c] => LAL
[p_r] => RN
[id] =>
[gender] => m
)
)
[t_b] => Array
(
)
[t_l] => Array
(
[0] => Array
(
[p_lev] => 2
[p_date] =>
[p_r] =>
)
)
[t_r] => Array
(
[0] => Array
(
[I_r] => 19
)
)
// second array
$bb = Array
(
[t_a] => Array
(
[0] => Array
(
[f_c] => NAN
[p_r] => RN
[id] => 1214125
[gender] => m
)
)
[t_b] => Array
(
)
[t_l] => Array
(
[0] => Array
(
[p_lev] => 2
[p_date] => 21
[p_r] => 25
)
)
[t_r] => Array
(
[0] => Array
(
[I_r] => 19
)
)
I have used the array_diff function but i get NULL.
please some one help?
$aa=(array)$aa;
$bb=(array)$bb;
$result=array_diff($aa,$bb);
It's unclear what you want. Please give an example or your desired output. Here's one possibility:
$ser_aa = array_map(function($e){return serialize($e);}, $aa);
$ser_bb = array_map(function($e){return serialize($e);}, $bb);
$diff = array_diff($ser_aa, $ser_bb);
$out = array_map(function($e){return unserialize($e);}, $diff);
print_r($out);
Output:
Array
(
[t_a] => Array
(
[0] => Array
(
[f_c] => LAL
[p_r] => RN
[id] =>
[gender] => m
)
)
[t_l] => Array
(
[0] => Array
(
[p_lev] => 2
[p_date] =>
[p_r] =>
)
)
)

Categories