Make two index array to be one index array PHP [duplicate] - php

This question already has answers here:
Implode two-dimensional array in PHP
(3 answers)
How to "flatten" a multi-dimensional array to simple one in PHP? [duplicate]
(23 answers)
Closed 4 years ago.
i just want to make two index array to be one array.
this is my code:
$intdate=array();
$arr=0;
foreach ($cek_cutay as $key => $value) {
$intdate[] =intervalDate($value->tgl_cuti_awal,$value->tgl_cuti_akhir);
$intdate[$arr++];
}
the result like :
array(2) {
[0]=> array(4) {
[0]=> string(10) "2018-11-12"
[1]=> string(10) "2018-11-13"
[2]=> string(10) "2018-11-14"
[3]=> string(10) "2018-11-15"
}
[1]=> array(2) {
[0]=> string(10) "2018-10-31"
[1]=> string(10) "2018-11-01"
}
}
i hope to be like :
array(5){
[0]=> string(10) "2018-11-12"
[1]=> string(10) "2018-11-13"
[2]=> string(10) "2018-11-14"
[3]=> string(10) "2018-11-15"
[4]=> string(10) "2018-10-31"
[5]=> string(10) "2018-11-01"
}
Thank you..!!

If the return from the intervalDate function is an array, you can do this
$intdate=array();
$arr=0;
foreach ($cek_cutay as $key => $value) {
foreach (intervalDate($value->tgl_cuti_awal,$value->tgl_cuti_akhir) as $date) {
$intdate[] = $date;
}
}

I think you expected to merge two array into a single array. If so then array_reduce might help you simply.
$arr = [
[
"2018-11-12",
"2018-11-13",
"2018-11-14",
"2018-11-15" ,
],
[
"2018-10-31",
"2018-11-01",
]
];
$intdate = array_reduce($arr, function($old, $new) {
return array_merge($old, $new);
}, []);
echo '<pre>', print_r($intdate), '</pre>';

You could use this function
function flatten_array( array $array, array $flattened = array() ) {
foreach ( $array as $item ) {
if ( is_array($item) ) {
$flattened = flatten_array( $item, $flattened );
continue;
}
$flattened[] = $item;
}
return $flattened;
}
$arr = array(
array(
"2018-11-12",
"2018-11-13",
"2018-11-14",
"2018-11-15" ,
),
array(
"2018-10-31",
"2018-11-01",
),
);
flatten_array($arr);

$arr1 = array(1,2,3,4,5,11);
$arr2 = array(6,7,8,9,10,11);
echo'<pre>';print_r(array_merge($arr1,$arr2));
echo'<pre>';print_r(array_unique(array_merge($arr1,$arr2)));die;
respective output :
array_merge :
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 11
[6] => 6
[7] => 7
[8] => 8
[9] => 9
[10] => 10
[11] => 11
)
array_unique + array_merge :
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 11
[6] => 6
[7] => 7
[8] => 8
[9] => 9
[10] => 10
)

Related

How to create a new key name and combine values in array with PHP?

I have 2 PHP arrays that I need to combine values together.
First Array
array(2) {
[0]=>
array(1) {
["id"]=>
string(1) "1"
}
[1]=>
array(1) {
["id"]=>
string(2) "40"
}
}
Second Array
array(2) {
[0]=>
string(4) "1008"
[1]=>
string(1) "4"
}
Output desired
array(2) {
[0]=>
array(1) {
["id"]=>
string(1) "1",
["count"]=>
string(1) "1008"
}
[1]=>
array(1) {
["id"]=>
string(2) "40",
["count"]=>
string(1) "4"
}
}
As you can see I need to add a new key name (count) to my second array and combine values to my first array.
What can I do to output this array combined?
Try something like the following. The idea is to iterate on the first array and for each array index add a new key "count" that holds the value contained on the same index of the second array.
$array1 = [];
$array2 = [];
for ($i = 0; $i < count($array1); $i++) {
$array1[$i]['count'] = $array2[$i];
}
you can do it like this
$arr1=[["id"=>1],["id"=>40]];
$arr2=[1008,4];
for($i=0;$i<count($arr2);$i++){
$arr1[$i]["count"] = $arr2[$i];
}
Live demo : https://eval.in/904266
output is
Array
(
[0] => Array
(
[id] => 1
[count] => 1008
)
[1] => Array
(
[id] => 40
[count] => 4
)
)
Another functional approach (this won't mutate/change the initial arrays):
$arr1 = [['id'=> "1"], ['id'=> "40"]];
$arr2 = ["1008", "4"];
$result = array_map(function($a){
return array_combine(['id', 'count'], $a);
}, array_map(null, array_column($arr1, 'id'), $arr2));
print_r($result);
The output:
Array
(
[0] => Array
(
[id] => 1
[count] => 1008
)
[1] => Array
(
[id] => 40
[count] => 4
)
)
Or another approach with recursion:
$arr1=[["id"=>1],["id"=>40]];
$arr2=[1008,4];
foreach ($arr1 as $key=>$value) {
$result[] = array_merge_recursive($arr1[$key], array ("count" => $arr2[$key]));
}
print_r($result);
And output:
Array
(
[0] => Array
(
[id] => 1
[count] => 1008
)
[1] => Array
(
[id] => 40
[count] => 4
)
)

How to separate array members I have, being part (Based on number of characters per member / Less than equal to 6)?

My code contains an array that has 7 elements and each element has a total number of different characters. I want, when the number of characters meets the criteria (<= 6) then create a new array.
The output is expected in the form of a two dimension array,
// My Variables, $value & $count
$value=array('as','fix','fine','is','port','none','hi','of');
for ($i=0; $i <count($value) ; $i++) {
$count[]=strlen($value[$i]);
}
Then have output like a,
// $value,
Array
(
[0] => as
[1] => fix
[2] => fine
[3] => is
[4] => port
[5] => none
[6] => hi
[7] => of
)
// Count the length $value and store in Variable $count,
Array
(
[0] => 2
[1] => 3
[2] => 4
[3] => 2
[4] => 4
[5] => 4
[6] => 2
[7] => 2
)
and then I hope my code can produce output like this:
(Explode element where length <= 6)
// If length value in the variable $count,
Array
(
[0] => 2
[1] => 3
Total Length(5)
[2] => 4
[3] => 2
Total Length(6)
[4] => 4
Total Length(4)
[5] => 4
Total Length(4)
[6] => 2
[7] => 2
Total Length(4)
)
This is my question point:
// $Values RESULT
Array
(
[0] => Array
(
[0] => as
[1] => fix
)
[1] => Array
(
[0] => fine
[1] => is
)
[1] => Array
(
[0] => port
)
[1] => Array
(
[0] => none
)
[1] => Array
(
[0] => hi
[1] => of
)
)
Your examples were a bit hard to follow but here's what I've got:
<?php
$value=array('as','fix','fine','is','port','none','hi','of');
$final = [];
$accumulator = 0;
$final[0] = [];
$x = 0;
for ($i=0; $i <count($value) ; $i++) {
var_dump($accumulator);
if($accumulator + strlen($value[$i]) > 6) {
echo "adding ".$value[$i] ." to new\n\n";
$x++;
$final[$x] = [];
array_push($final[$x], $value[$i]);
$accumulator = strlen($value[$i]);
}else{
echo "adding ".$value[$i] . " to existing\n\n";
array_push($final[$x], $value[$i]);
$accumulator += strlen($value[$i]);
}
}
var_dump($final);
Yields
int(0)
adding as to existing
int(2)
adding fix to existing
int(5)
adding fine to new
int(4)
adding is to existing
int(6)
adding port to new
int(4)
adding none to new
int(4)
adding hi to existing
int(6)
adding of to new
array(5) {
[0]=>
array(2) {
[0]=>
string(2) "as"
[1]=>
string(3) "fix"
}
[1]=>
array(2) {
[0]=>
string(4) "fine"
[1]=>
string(2) "is"
}
[2]=>
array(1) {
[0]=>
string(4) "port"
}
[3]=>
array(2) {
[0]=>
string(4) "none"
[1]=>
string(2) "hi"
}
[4]=>
array(1) {
[0]=>
string(2) "of"
}
}
http://sandbox.onlinephpfunctions.com/code/20a63b83ad5524c5cd77e111bc15e197bf8bfba2

PHP Sort Multidimensional Array by a sub sub-key

I have looked at numerous threads relating to this and none of them have been of any help to me. I have an array which follows the basic structure of $array[location][store][person] = funds. What is the most efficient way of sorting the array so that the [person] key is in ASC order?
This is what it looks like now:
Array
(
[Florida] => Array
(
[AppleSauce] => Array
(
[Rabbit, Hunting] => 5
[Brown, Bubba] => 20
[Chicken, Cantina] => 10
[Gum, Bubble] => 10
[Pool, Swimming] => 4
[Bath, Taka] => 2
)
)
[Texas] => Array
(
[BeatleJuice] => Array
(
[Chicken, Cantina] => 10
[Pool, Swimming] => 4
[House, Road] => 5
)
[CaramelApple] => Array
(
[Chicken, Cantina] => 10
[Pool, Swimming] => 4
[House, Road] => 5
)
)
This is what I am looking for:
Array
(
[Florida] => Array
(
[AppleSauce] => Array
(
[Bath, Taka] => 2
[Brown, Bubba] => 20
[Chicken, Cantina] => 10
[Gum, Bubble] => 10
[Pool, Swimming] => 4
[Rabbit, Hunting] => 5
)
)
[Texas] => Array
(
[BeatleJuice] => Array
(
[Chicken, Cantina] => 10
[House, Road] => 5
[Pool, Swimming] => 4
)
[CaramelApple] => Array
(
[Chicken, Cantina] => 10
[House, Road] => 5
[Pool, Swimming] => 4
)
)
You can use ksort to sort the array keys of people in alphabetical order
foreach($array as $state => $locations) {
foreach($locations as $location => $people) {
ksort($array[$state][$location]);
}
}
The php function array_multisort can do this:
Sorting multi-dimensional array
<?php
$ar = array(
array("10", 11, 100, 100, "a"),
array( 1, 2, "2", 3, 1)
);
array_multisort($ar[0], SORT_ASC, SORT_STRING,
$ar[1], SORT_NUMERIC, SORT_DESC);
var_dump($ar);
?>
In this example, after sorting, the first array will transform to "10", 100, 100, 11, "a" (it was sorted as strings in ascending order). The second will contain 1, 3, "2", 2, 1 (sorted as numbers, in descending order).
array(2) {
[0]=> array(5) {
[0]=> string(2) "10"
[1]=> int(100)
[2]=> int(100)
[3]=> int(11)
[4]=> string(1) "a"
}
[1]=> array(5) {
[0]=> int(1)
[1]=> int(3)
[2]=> string(1) "2"
[3]=> int(2)
[4]=> int(1)
}
}
This is based from the official documentation:
http://php.net/manual/en/function.array-multisort.php

How to get rid of duplicate values from multidimensional array

I want to remove duplicate values from multi-dim array, I tried all the possible solutions which is already described but for me, is not working, can anyone please correct it?
Here's my Array:
Array (
[0] => Array (
[0] => element_10
[1] => block_1
[2] => element_4
[3] => element_1
[4] => element_3
)
[1] => Array (
[0] => block_1
[1] => block_2
[2] => element_8
[3] => element_10
[4] => element_12
[5] => element_14
[6] => element_4
[7] => element_2
[8] => element_3
[9] => element_9
[10] => element_13
[11] => element_7
)
)
Where I want the array in this format:
Array (
[0] => Array (
[0] => element_10
[1] => block_1
[2] => element_4
[3] => element_1
[4] => element_3
)
[1] => Array (
[1] => block_2
[2] => element_8
[4] => element_12
[5] => element_14
[7] => element_2
[9] => element_9
[10] => element_13
[11] => element_7
)
)
Ican setup the key indexes later.
I tried:
function multi_unique($array) {
foreach ($array as $k=>$na)
$new[$k] = serialize($na);
$uniq = array_unique($new);
foreach($uniq as $k=>$ser)
$new1[$k] = unserialize($ser);
return ($new1);
}
No Luck, then I tried:
function array_unique_multidimensional($input)
{
$serialized = array_map('serialize', $input);
$unique = array_unique($serialized);
return array_intersect_key($input, $unique);
}
Still same array returning.
I tried this method too:
function super_unique($array)
{
$result = array_map("unserialize", array_unique(array_map("serialize", $array)));
foreach ($result as $key => $value)
{
if ( is_array($value) )
{
$result[$key] = self::super_unique($value);
}
}
return $result;
}
Please help me, I know it's pretty simple I don't know where I'm losing?
Thanks,
You need to iterate over your list of input arrays. For each value in that array, you need to see if you've previously encountered it, so you'll have to keep a super-set of all values across all arrays, which you gradually append to. If a value already exists in the super-set array, you can remove it, otherwise you can append it.
function multi_unique($arrays) {
$all_values = array();
foreach ($arrays as &$array) {
foreach ($array as $index => $value) {
if (in_array($value, $all_values)) {
// We've seen this value previously
unset($array[$index]);
} else {
// First time we've seen this value, let it pass but record it
$all_values[] = $value;
}
}
}
return $arrays;
}
$values = array (
array ( 'element_10', 'block_1', 'element_4', 'element_1', 'element_3',) ,
array ( 'block_1', 'block_2', 'element_8', 'element_10', 'element_12', 'element_14', 'element_4', 'element_2', 'element_3', 'element_9', 'element_13', 'element_7',)
);
var_dump(multi_unique($values));
Output:
array(2) {
[0]=>
array(5) {
[0]=>
string(10) "element_10"
[1]=>
string(7) "block_1"
[2]=>
string(9) "element_4"
[3]=>
string(9) "element_1"
[4]=>
string(9) "element_3"
}
[1]=>
array(8) {
[1]=>
string(7) "block_2"
[2]=>
string(9) "element_8"
[4]=>
string(10) "element_12"
[5]=>
string(10) "element_14"
[7]=>
string(9) "element_2"
[9]=>
string(9) "element_9"
[10]=>
string(10) "element_13"
[11]=>
string(9) "element_7"
}
}
If you just want to remove duplicates from the second entry of your array, use array_diff():
$array[1] = array_diff($array[1], $array[0]);
Iterate if you want to apply it to an arbitrary length.
Why are you using the serialize function.
Use array_diff instead.
A simple would be.
$orginal = array(array(values), array(values), ...);
$copy = $original;
foreach($original as $k => $subArray) {
$tmpCopy = $copy;
unset($tmpCopy[$k]);
unshift($tmpCopy, $subArray);
$tmpCopy = array_values($tmpCopy);
$original[$k] = call_user_func_array('array_diff', $tmpCopy);
}
This works for a two dimensions array.
Hope it helps.

Recreate Multidimensional Array in PHP

This is pretty basic, but my question is:
Given an array:
$a = array(
0 => array('Rate'=> array('type_id'=>1, 'name' => 'Rate_1', 'type'=>'day','value'=>10)),
1 => array('Rate'=> array('type_id'=>1, 'name' => 'Rate_2', 'type'=>'night','value'=>8)),
2 => array('Rate'=> array('type_id'=>2, 'name' => 'Rate_3', 'type'=>'day','value'=>7)),
3 => array('Rate'=> array('type_id'=>2, 'name' => 'Rate_4', 'type'=>'nigh','value'=>16)),
4 => array('Rate'=> array('type_id'=>3, 'name' => 'Rate_5', 'type'=>'day','value'=>10))
);
What is the most efficient way to change it so we have something like:
$new_array = array(
[type_id] => array(
[type] => array(
[value]
)
)
)
);
In other words, I would like to strip some data (the name, which I don't need) and reorganise the dimensions of the array. In the end I would have an array which I would be able to access the values by $new_array['type_id']['type']['value'].
Not entirely sure if this is exactly what you want, but with this you can access the values by saying
echo $new[TYPE_ID][DAY_OR_NIGHT];
$new = array();
foreach($a AS $b){
$c = $b['Rate'];
$new[$c['type_id']][$c['type']] = $c['value'];
}
Using print_r on $new would give you:
Array
(
[1] => Array
(
[day] => 10
[night] => 8
)
[2] => Array
(
[day] => 7
[night] => 16
)
[3] => Array
(
[day] => 10
)
)
Since php 5.3.0, array_reduce() allows using an array as the initial value, given your initial array $a, you can use the following code
function my_reducer ($result, $item) {
$result[$item['Rate']['type_id']][$item['Rate']['type']] = $item['Rate']['value'];
return $result;
}
$assoc_arr = array_reduce($a, 'my_reducer', array());
var_dump($assoc_arr);
This returns
array(3) { [1]=> array(2) {
["day"]=>
int(10)
["night"]=>
int(8) } [2]=> array(2) {
["day"]=>
int(7)
["nigh"]=>
int(16) } [3]=> array(1) {
["day"]=>
int(10) } }

Categories