Splitting one Array into many Array in PHP - php

I'm in need of splitting of the single array into multiple array for some report generating purpose.
I have an array like this which I have given below.
Array
(
[blah_1] => 1
[blahblah_1] => 31
[blahblahblah_1] => 25
[blah_3] => 1
[blahblah_3] => 3
[blahblahblah_3] => 5
[blah_10] => 1
[blahblah_10] => 10
[blahblahblah_10] => 2
)
I want to split the above to,
Array
(
[blah_1] => 1
[blahblah_1] => 31
[blahblahblah_1] => 25
)
Array
(
[blah_3] => 1
[blahblah_3] => 3
[blahblahblah_3] => 5
)
Array
(
[blah_10] => 1
[blahblah_10] => 10
[blahblahblah_10] => 2
)
How can I do this in PHP ??

$oldarray=array('blah_1'=>1,'blahblah_1'=>31,'blahblahblah_1'=>25,
'blah_3'=>1,'blahblah_3'=>3,'blahblahblah_3'=>5,
'blah_10'=>1,'blahblah_10'=>10,'blahblahblah_10'=>2
)
$newarray=array();
foreach ($oldarray as $key=>$val) { //Loops through each element in your original array
$parts=array_reverse(explode('_',$key)); //Splits the key around _ and reverses
$newarray[$parts[0]][$key]=$val; //Gets the first part (the number) and adds the
//value to a new array based on this number.
}
The output will be:
Array (
[1]=>Array
(
[blah_1] => 1
[blahblah_1] => 31
[blahblahblah_1] => 25
)
[3]=>Array
(
[blah_3] => 1
[blahblah_3] => 3
[blahblahblah_3] => 5
)
[10]=>Array
(
[blah_10] => 1
[blahblah_10] => 10
[blahblahblah_10] => 2
)
)

Use array_chunk. Example:
<?php
$chunks = array_chunk($yourarray, 3);
print_r($chunks);
?>

Use array_chunk
i.e
$a = array(1,2,3,4,5,6,7);
var_dump(array_chunk($a, 3));

Not sure if you are looking for array_slice
but take a look at this example:
<?php
$input = array("a", "b", "c", "d", "e");
print_r(array_slice($input, 2, -1));
print_r(array_slice($input, 2, -1, true));
?>
will result in this:
Array
(
[0] => c
[1] => d
)
Array
(
[2] => c
[3] => d
)

array_chunk ( array $input , int $size);

Related

Sum parts of an array in php

this is quite beyond me. Appreciate some help.
I have an array in php like so:
[0] => Array
(
[cust_id] => 1006
[no_of_subs] => 2
[dlv_id] => 1000
)
[1] => Array
(
[cust_id] => 1011
[no_of_subs] => 3
[dlv_id] => 1000
)
[2] => Array
(
[cust_id] => 1012
[no_of_subs] => 5
[dlv_id] => 1001
)
[3] => Array
(
[cust_id] => 1013
[no_of_subs] => 6
[dlv_id] => 1001
)
I don't need the cust_id field. I just need to group the dlv_id and the sum of no_of_subs for each matching dlv_id. The result should look like this:
[0] => Array
(
[dlv_id] => 1000
[no_of_subs] => 5
)
[1] => Array
(
[cust_id] => 1011
[no_of_subs] => 11
)
Thank you for any help.
I don't understand the downvotes for this question. Am i doing it all wrong? Downvoting without a reason is not helping.
The simplest, most efficient way to group and sum is to perform a single loop and assign temporary associative keys.
When a row is identified as a new dlv_id row, save the two desired elements, otherwise add the no_of_subs value to the pre-existing value.
Optionally, remove the temporary keys with array_values().
Code (Demo)
$array = [
["cust_id" => 1006, "no_of_subs" => 2, "dlv_id" => 1000],
["cust_id" => 1011, "no_of_subs" => 3, "dlv_id" => 1000],
["cust_id" => 1012, "no_of_subs" => 5, "dlv_id" => 1001],
["cust_id" => 1013, "no_of_subs" => 6, "dlv_id" => 1001]
];
foreach ($array as $row) {
if (!isset($result[$row["dlv_id"]])) {
$result[$row["dlv_id"]] = ["dlv_id" => $row["dlv_id"], "no_of_subs" => $row["no_of_subs"]];
} else {
$result[$row["dlv_id"]]["no_of_subs"] += $row["no_of_subs"];
}
}
var_export(array_values($result));
Output:
array (
0 =>
array (
'dlv_id' => 1000,
'no_of_subs' => 5,
),
1 =>
array (
'dlv_id' => 1001,
'no_of_subs' => 11,
),
)
Using array_column function, we can extract out dlv_id and no_of_subs separately in two different arrays, using cust_id as the key.
Now, simply loop over the array of dlv_id, and if matching key found, add the no_of_subs to it, else set the value (for the first time).
We use isset function to check if the key exists already or not.
Try the following:
// your input array is $input_array
// get all dlv_id maintaining the cust_id as index
$dlv_id = array_column($input_array, 'dlv_id', 'cust_id');
// get all no_of_subs maintaining the cust_id as index
$no_of_subs = array_column($input_array, 'no_of_subs', 'cust_id');
$output = array();
foreach ($dlv_id as $key => $value) {
if (isset($output[$value]['dlv_id'])) {
$output[$value]['dlv_id'] += $no_of_subs[$key];
} else {
$output[$value]['dlv_id'] += $no_of_subs[$key];
}
}

How to convert array multidimensional array to single array?

my output is
Array ( [bid] => 1 [bookiing_date] => 2014-11-19 )
Array ( [bid] => 2 [bookiing_date] => 2014-11-15 )
Array ( [bid] => 3 [bookiing_date] => 2015-01-10 )
Array ( [bid] => 4 [bookiing_date] => 2015-01-27 )
but i want to convert in this form..
$date = ['2015-01-10','2015-01-27','2014-11-19'];
Please any one help me
What you've written above doesn't look too much like PHP, but something like this should do the trick
$single_dimensional_array = array();
foreach($multi_dimensional_array as $array_item) {
if(isset($array_item['bookiing_date'])) {
$single_dimensional_array[] = $array_item['bookiing_date'];
}
}
echo(var_export($single_dimensional_array), true);
Btw, the $single_dimensional_array[] = syntax above simply pushes the value on the right side of the equal sign onto the end of the array on the left side of the equal sign.
There is a very simple approach to get that... I think you did not tried anyway..here is the basic and simple answer ...
$sing_arr = array();
$multi_arr = array(array ( "bid"=> 1, "bookiing_date"=> "2014-11-19" ) ,
array ( "bid"=> 2, "bookiing_date"=> "2014-11-15" ) ,
array ( "bid"=> 3, "bookiing_date"=> "2015-01-10" ) ,
array ( "bid"=> 4 ,"bookiing_date"=> "2015-01-27" )
);
foreach($multi_arr as $key=>$val)
{
array_push($sing_arr,"'".$val["bookiing_date"]."'");
}
echo var_dump($sing_arr);
You Can Loop Through the array using foreach and store the date in another array:
Code:
<?php
$array1=array(
Array ( "bid" => 1 ,"bookiing_date" => "2014-11-19" ),
Array ( "bid" => 2 ,"bookiing_date" => "2014-11-15" ),
Array ( "bid" => 3 ,"bookiing_date" => "2015-01-10" ),
Array ( "bid" => 4 ,"bookiing_date" => "2015-01-27" )
);
// print_r($array1);
$date=array();
foreach ($array1 as $temparray){
$date[]=$temparray["bookiing_date"];
}
print_r($date);
Output:
Array
(
[0] => 2014-11-19
[1] => 2014-11-15
[2] => 2015-01-10
[3] => 2015-01-27
)
Use this
$array = array(array('bid' => 1, 'bookiing_date' => 2014 - 11 - 19),
array('bid' => 2, 'bookiing_date' => 2014 - 11 - 15),
array('bid' => 3, 'bookiing_date' => 2015 - 01 - 10),
array('bid' => 4, 'bookiing_date' => 2015 - 01 - 27));
$data = array();
foreach ($array as $array_item){
if(isset($array_item['bookiing_date'])) {
$data[] = $array_item['bookiing_date'];
}
}
print_r($data);
if you are using PHP 5.5+, use array_column()
$data = array_column($array, 'bookiing_date');
print_r($data);

Unset one duplicate value in array PHP

I have 2 arrays: Array1 and Array2. As you can see in Array1 I have 2 duplicate values. So what I want to do, is unset one of dublicates (doesn't matter which one) and as a result I need to unset value from Array2 with the same key as already unset value in Array1
Array1
(
[0] => 1331-14-2-45
[1] => 1344-1-4-22
**[2] => 1409-1-1-4**
[4] => 1312-14-1-23
**[5] => 1409-1-1-4**
[6] => 1365-10-3-30
)
AND
Array2
(
[0] => opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6
[1] => opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6
[2] => opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6
[4] => deviceNotActive#nemodel.GPON.4.6
[5] => deviceNotActive#nemodel.GPON.4.6
[6] => opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6
)
<?php
$array1 = array
(
0 => '1331-14-2-45',
1 => '1344-1-4-22',
2 => '1409-1-1-4',
4 => '1312-14-1-23',
5 => '1409-1-1-4',
6 => '1365-10-3-30',
);
$array1_tmp = $array1;
$array2 = array
(
0 => 'opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6',
1 => 'opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6',
2 => 'opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6',
4 => 'deviceNotActive#nemodel.GPON.4.6',
5 => 'deviceNotActive#nemodel.GPON.4.6',
6 => 'opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6',
);
$array1 = array_unique($array1);
$remove_keys = array_keys(array_diff_key($array1_tmp, $array1));
foreach($remove_keys as $k => $v) {
unset($array2[$v]);
}
You can use the array_unique() function, and maybe take a look at the Array functions list.
Use array_unique function and array_diff
$uniqueValues = array_unique($inputArray); //your first array
$deletedValues = array_diff($inputArray, $uniqueValues);
foreach($deletedValues as $key => $deletedValue){
unset($secondIput[$key]); //you second array
}

Sort PHP array by numerical values

I would like to sort the following names
Array ( [Jessie] => 2 [Sarah] => 3 [Simon] => 2 [John] => 2 [Kevin] => 1 [Canvasser] => 8 [canvasser] => 11 )
based on the values corresponding to them
I printed the names through the following function
// get canvasser individual names and count houses canvassed
foreach ($canvassers as $key => $value) {
// Add to the current group count if it exists
if ( isset( $canvasser_counts[$value] ) ) {
$canvasser_counts[$value]++;
}
// or initialize to 1 if it doesn't exist
else {
$canvasser_counts[$value] = 1;
}
}
print_r($canvasser_counts);
where $canvassers simply held all the names eg.
$canvassers = array('Jessie', 'Simon', 'Jessie')
Any help would be really appreciated, I have spent so long on this but can't get my head straight to sort the array correctly.
You want to use asort() - http://php.net/manual/en/function.asort.php - to sort the values in ascending order, or arsort() - http://php.net/manual/en/function.arsort.php - to sort in descending order.
Given this PHP:
$vals = array("Jessie" => 2, "Sara" => 3, "Simon" => 2, "John" => 2, "Kevin" => 1, "Canvasser" => 8, "canvasser" => 11 );
print_r($vals); // current order
asort($vals); // sort array
print_r($vals); // new order
You will get the following output:
Array
(
[Jessie] => 2
[Sara] => 3
[Simon] => 2
[John] => 2
[Kevin] => 1
[Canvasser] => 8
[canvasser] => 11
)
Array
(
[Kevin] => 1
[Jessie] => 2
[John] => 2
[Simon] => 2
[Sara] => 3
[Canvasser] => 8
[canvasser] => 11
)

PHP: A clean way of converting an array

What is a clean way to convert an array that looks like this:
[584] => Array ( [link_id] => 1 [site_id] => 5 [COUNT(*)] => 2 )
[585] => Array ( [link_id] => 243 [site_id] => 5 [COUNT(*)] => 2 )
[586] => Array ( [link_id] => 522 [site_id] => 89223 [COUNT(*)] => 3 )
To an array where the key is the site_id from above, so for the above example the resulting array would be:
[5] => Array( 1, 2, 243, 2) //Even ones and 0 are link_id, odd ones are count(*)
[89223] => Array(522, 3)
So, basically, group them by site_id. I still need to keep the relationship between link_id and count(*), in the case above I am doing it by the positions (so 0 and 1 are together, 2 and 3, etc) but I am open to a new structure as well.
Thanks!
You mean something like this? (Demo):
$out = array();
foreach($input as $v)
{
$site_id = $v['site_id'];
unset($v['site_id']);
$out[$site_id][] = $v;
}
Or in case you prefer value pairs after each other (Demo):
...
unset($v['site_id']);
$out[$site_id][] = array_shift($v);
$out[$site_id][] = array_shift($v);
} ...

Categories