Rearrange an array according to key - php

I dont know what to ask so i straight away went to show an example. Hope it helps though!
Say i have an array
array (size=3)
0 =>
array (size=3)
0 => int 1
1 => int 2
2 => int 3
2 =>
array (size=3)
0 => int 2
1 => int 3
2 => int 4
5 =>
array (size=3)
0 => int 5
1 => int 6
2 => int 7
Now i want to arrange it according to KEY so that it looks like
array (size=3)
0 =>
array (size=3)
0 => int 1
1 => int 2
2 => int 3
1 =>
array (size=3)
0 => int 2
1 => int 3
2 => int 4
2 =>
array (size=3)
0 => int 5
1 => int 6
2 => int 7
Does anyone has a solution ?

One possible approach:
$new_arr = array_values($old_arr);

You can use foreach and assign the values to new array
$newArr = array();
foreach($array as $k=>$v){
$newArr[] = $v;
}

The most direct way to achieve that is probably to run array_values on your array :
$array = array_values($array);
It will basically reset the keys

Related

How to store a binary tree as one-dimensional array?

How to construct data into a binary tree sort to output a one-dimensional array?
Now that I have constructed the data into a binary tree, how can I recursively solve the binary tree as a one-dimensional array with the following code and data:
Data
$nodes = array(8,3,10,1,6,14,4,7,13);
Construct a binary tree code
function insertNode($node,$newNode){
//var_dump($node);
//var_dump($newNode);
//exit;
if ($node['key'] < $newNode['key']){
if (empty($node['right'])){
$node['right'] = $newNode;
}else{
$node['right'] = insertNode($node['right'],$newNode);
}
}elseif ($node['key'] > $newNode['key']){
if (empty($node['left'])){
$node['left'] = $newNode;
}else{
$node['left'] = insertNode($node['left'],$newNode);
}
}
return $node;
}
function tree($nodes)
{
$node = [
'key' => '',
'left' => '',
'right' => ''
];
$newNode = [
'key' => '',
'left' => '',
'right'=> ''
];
foreach ($nodes as $key => $value){
//insert($value,$key);
if($key == 0)
{
$node['key'] = $value;
continue;
}
$newNode['key'] = $value;
//Constructing a binary tree
$node = insertNode($node,$newNode);
}
//Recursive solution
$node = midSortNode($node);
return $node;
}
var_dump(tree($nodes));
The following is my constructed binary tree
array (size=3)
'key' => int 8
'left' =>
array (size=3)
'key' => int 3
'left' =>
array (size=3)
'key' => int 1
'left' => string '' (length=0)
'right' => string '' (length=0)
'right' =>
array (size=3)
'key' => int 6
'left' =>
array (size=3)
...
'right' =>
array (size=3)
...
'right' =>
array (size=3)
'key' => int 10
'left' => string '' (length=0)
'right' =>
array (size=3)
'key' => int 14
'left' =>
array (size=3)
...
'right' => string '' (length=0)
I need to recursively classify the binary tree into a well-ordered one-dimensional array.
My code is as follows
function midSortNode($node){
$sortArr = [];
if (!empty($node)){
$sortArr[] = midSortNode($node['left']);
//$sortArr['left'] = midSortNode($node['left']);
array_push($sortArr,$node['key']);
$sortArr[] = midSortNode($node['right']);
//$sortArr['right'] = midSortNode($node['right']);
}
return $sortArr;
}
var_dump(midSortNode($node));
Here is the result, but not what I want
0 =>
array (size=3)
0 =>
array (size=3)
0 =>
array (size=0)
...
1 => int 1
2 =>
array (size=0)
...
1 => int 3
2 =>
array (size=3)
0 =>
array (size=3)
...
1 => int 6
2 =>
array (size=3)
...
1 => int 8
2 =>
array (size=3)
0 =>
array (size=0)
empty
1 => int 10
2 =>
array (size=3)
0 =>
array (size=3)
...
1 => int 14
2 =>
array (size=0)
...
How to solve the binary tree as follows
array (size=9)
0 => int 1
1 => int 3
2 => int 4
3 => int 6
4 => int 7
5 => int 8
6 => int 10
7 => int 13
8 => int 14
I'm assuming that your happy with the steps so far, so the main code as it is isn't changed. All I think you need to do is to extract the data from the final tree into a 1 dimensional array. As the items are all leaf nodes and in order, you can just use array_walk_recursive() to go over all of the nodes and add them to a new array...
$tree = tree($nodes);
array_walk_recursive( $tree,
function ($data) use (&$output) { $output[] = $data;} );
print_r($output);
gives...
Array
(
[0] => 1
[1] => 3
[2] => 4
[3] => 6
[4] => 7
[5] => 8
[6] => 10
[7] => 13
[8] => 14
)
Edit:
To update the existing code to do this, you can change the midSortNode() to pass around the list of outputs and only add in the current node...
function midSortNode($node, $sortArr = []){
if (!empty($node)){
$sortArr = midSortNode($node['left'], $sortArr);
$sortArr[] = $node['key'];
$sortArr = midSortNode($node['right'], $sortArr);
}
return $sortArr;
}

Php Array from database not getting desired array stucture

I have problem with building multi-dimensional array by pulling data from database.
I have a list of Sales Representative (sr) in my database. I run query and selected all sr.
Now I have to create array like this:
array (size=3)
'Manoj' =>
array (size=3)
'mc_count' => 0
'auto_count' => 0
'in_count' => 0
'Bharat' =>
array (size=3)
'mc_count' => 0
'auto_count' => 0
'in_count' => 0
'Pradeep' =>
array (size=3)
'mc_count' => 0
'auto_count' => 0
'in_count' => 0
To create this I have written following code:
<?php
$sr_array=array();
$sr_sql= "select DISTINCT sr from sales_invoice";
$sr_query = mysqli_query($connection, $sr_sql);
while($sr_result = mysqli_fetch_assoc($sr_query)){
$sr_array []= array($sr_result["sr"]=>array(“mc_count”,”auto_count”,”in_coun”);
}
var_dump($sr_array);
?>
I get this OUTPUT
array (size=9)
0 =>
array (size=1)
'Manoj' =>
array (size=3)
'count_in_battery' => int 10
'count_auto_battety' => int 0
'count_indu_battery' => int 0
1 =>
array (size=1)
'Bharat' =>
array (size=3)
'count_in_battery' => int 10
'count_auto_battety' => int 0
'count_indu_battery' => int 0
2 =>
array (size=1)
'Pradeep =>
array (size=3)
'count_in_battery' => int 10
'count_auto_battety' => int 0
'count_indu_battery' => int 0
If you see the output my array has additional index as
0 => //Unwanted Index
array (size=1)
'Manoj' =>
Which is creating problem in programming. If you can please help me.
Try this:
$sr_array = array();
$sr_sql = "select DISTINCT sr from sales_invoice";
$sr_query = mysqli_query($connection, $sr_sql);
while($sr_result = mysqli_fetch_assoc($sr_query)){
$sr_array[$sr_result["sr"]] = array(
"mc_count" => 0,
"auto_count" => 0,
"in_coun" => 0
);
}

How to map the two arrays with a duplicate value?

I have two arrays.
$array1 = ['id_e' =>[91707, 91708]];
$array2 = ['id_s' => [18, 57]];
If I want to insert or delete into the database, I want to make one-to-many mappings on these two arrays. And the result I expect it to be a new array as shown below.
Final Array:
array (size=4)
0 =>
array (size=2)
'id_e' => int 91707
'id_s' => int 18
1 =>
array (size=2)
'id_e' => int 91707
'id_s' => int 57
array (size=2)
2 =>
array (size=2)
'id_e' => int 91708
'id_s' => int 18
3 =>
array (size=2)
'id_e' => int 91708
'id_s' => int 57
I'm stuck after returning array1 and array2. I'm a beginner in php.
How do I do this?
Easiest way is:
$res = array();
foreach ($array1['id_e'] as $ide)
foreach ($array2['id_s'] as $ids)
$res[] = array('id_e' => $ide, 'id_s' => $ids);

Why multisort works only in the first depth of array?

I thought that the function array_multisort can sort multidimensional array, but this code does not work.
Code:
$values = array(0, 2, 1, array(0, 2, 1));
array_multisort($values, SORT_ASC);
var_dump($values);
Return:
array(4) [
0 => 0
1 => 1
2 => 2
3 => array(3) [
0 => 0
1 => 2 //should be 1
2 => 1 //should be 2
]
]
Why the array in array is not sorted? Thanks
You can try
sort_recursive($values);
var_dump($values);
Output
array (size=4)
0 => int 0
1 => int 1
2 => int 2
3 =>
array (size=3)
0 => int 0
1 => int 1
2 => int 2
Function Used
function sort_recursive(&$array) {
sort($array);
foreach ( $array as &$v ) {
is_array($v) and sort_recursive($v);
}
}
is because the array() is in a multidimentional form
try this link to see how to sort multidimensional array click here

settype() in PHP for an ARRAY is producing Different output

CODE 1: Reading data from Excel sheet. Containing 12 Different values.
The values are : 48,600,5.3,5,1500,6000,85,30,70,30,70,14 .
$BATTCONFIG=$objPHPExcel->getActiveSheet()->rangetoArray('C9:C20',null,true,true,true);
CODE 2: Trying to convert all VALUES in the array $BATTCONFIG to INTEGER using FOR loop.
$y1 = (array_values($BATTCONFIG));
var_dump(y1);
for( $i=0 ; $i<=11 ; $i++ )
{
settype($y1[$i], "integer");
}
var_dump($y1);
But I am not getting the desiered output , I am getting all the values as 1 .
MY OUTPUT:
Before SETTYPE():
array (size=12)
0 =>
array (size=1)
'C' => float 48
1 =>
array (size=1)
'C' => int 600
2 =>
array (size=1)
'C' => float 0.3
3 =>
array (size=1)
'C' => int 5
4 =>
array (size=1)
'C' => float 1500
5 =>
array (size=1)
'C' => int 6000
6 =>
array (size=1)
'C' => int 85
7 =>
array (size=1)
'C' => int 30
8 =>
array (size=1)
'C' => int 70
9 =>
array (size=1)
'C' => int 30
10 =>
array (size=1)
'C' => int 70
11 =>
array (size=1)
'C' => float 14
AFTER SETTYPE():
array (size=12)
0 => int 1
1 => int 1
2 => int 1
3 => int 1
4 => int 1
5 => int 1
6 => int 1
7 => int 1
8 => int 1
9 => int 1
10 => int 1
11 => int 1
Please help me out . I need these Integer values as output to plot my Graph.
Thank you in advance.
There must be another error when populating $y1 with its values. Imagine this simplified example which works as expected:
$arr = array('1','2','3','4','5');
for ($i = 0; $i < 5; $i++) {
settype($arr[$i], "integer");
}
var_dump($arr);
What gives you :
array(5) {
[0] =>
int(1)
[1] =>
int(2)
[2] =>
int(3)
[3] =>
int(4)
[4] =>
int(5)
}
After edit of the question (now having $y1 before conversion) it points out, that you aren't aware of that $y1 is multidimensional array. You'll have to change to code to something like this:
$ints = array();
foreach($y1 as $index => $cell) {
$values = array_values($cell);
$ints []= intval(round($values[0]));
}
var_dump($ints);
Also note, that you are trying to convert floats to int using the settype() function. I would use round() for that

Categories