How to make an element array inside an array explode again.
I have an array like this
echo "<pre>";
print_r($pks);
The Result.
Array
(
[0] => 2017-04-15||KMTC_HOCHIMINH
[1] => 2017-04-15||OOCL_NAGOYA
)
I expected like this,
Array
(
[record] =>
[1] 2017-04-15
[2] KMTC_HOCHIMINH
[record] =>
[1] 2017-04-15
[2] OOCL_NAGOYA
)
What keys on php to process array like this.
Please advise.
UPDATE
How about this .
Array
(
[0] => Array
(
[date] => 2017-04-15
[vessel] => KMTC_HOCHIMINH
)
[1] => Array
(
[date] => 2017-04-15
[vessel] => OOCL_NAGOYA
)
)
Try this, check the live demo
foreach($pks as $k => $v) {
$values = explode('||', $v);
$result[] = array_combine(range(1, count($values)), $values);
//added
$res[] = array_combine(['date', 'vessel'], $values) ;
}
result
Array
(
[0] => Array
(
[1] => 2017-04-15
[2] => KMTC_HOCHIMINH
)
[1] => Array
(
[1] => 2017-04-15
[2] => OOCL_NAGOYA
)
)
You can use array_walk() (or just foreach if you want):
array_walk($pks, function(&$a) {
$a = array_combine(['date', 'vessel'], explode('||', $a));
});
Foreach method:
foreach($pks as $k => $v) {
$pks[$k] = array_combine(['date', 'vessel'], explode('||', $v));
}
However, the key of each array won't be record, since it's impossible to have the same key multiple times.
Output:
Array
(
[0] => Array
(
[date] => 2017-04-15
[vessel] => KMTC_HOCHIMINH
)
[1] => Array
(
[date] => 2017-04-15
[vessel] => OOCL_NAGOYA
)
)
Bonus method because I like messing with arrays:
$pks = array_map('explode', array_fill(0, count($pks), '||'), $pks);
Related
I need some help on below array formation. I want to create a custom array using 2 arrays.
This is my first array :-
Array
(
[0] => Array
(
[0] => 26
[1] => 0.0000000000000000
)
[1] => Array
(
[0] => 25
[1] => 0.0000000000000000
)
[2] => Array
(
[0] => 24
[1] => 0.0000000000000000
)
)
This is my second array :-
Array
(
[0] => Array
(
[0] => 24
)
[1] => Array
(
[0] => 26
)
)
I want final array as below. Can someone please suggest how to form this array.
Array
(
[0] => Array
(
[0] => 26
[1] => 0.0000000000000000
)
[2] => Array
(
[0] => 24
[1] => 0.0000000000000000
)
)
I have used below but I want it without foreach.
$finalArray = array();
foreach ($secondArray as $key => $value) {
$key = array_search($value[0], array_column($firstArray, 0));
$finalArray[] = $firstArray[$key];
}
You can use array_filter():
$finalArray = array_filter($firstArray,
fn($item) => in_array($item[0], array_column($secondArray, 0))
);
It could be more efficiant to compute allowed values first:
$allowedValues = array_column($secondArray, 0);
$finalArray = array_filter($firstArray, fn($item) => in_array($item[0], $allowedValues));
Before PHP 7.4, you have to use use() to pass variable to the anonymous function:
$finalArray = array_filter($firstArray,
function($item) use($secondArray) {
return in_array($item[0], array_column($secondArray, 0));
}
);
echo '<pre>'.print_r($listings,1).'</pre>';
ksort($listings, SORT_NUMERIC);
echo '<pre>'.print_r($listings,1).'</pre>';
Output:
Array
(
[quick-brown-fox] => Array
(
[0] => Quick-brown-fox
[1] => quick-brown-fox
[4] => general_thumbs/quick-brown-fox.jpg
[2] => 320
[3] => 240
)
)
Array
(
[quick-brown-fox] => Array
(
[0] => Quick-brown-fox
[1] => quick-brown-fox
[4] => general_thumbs/quick-brown-fox.jpg
[2] => 320
[3] => 240
)
)
I tried foreach, but it won't affect the original array, and for won't work because the its a key, not an index. What should I do in that case?
You have nested array in this $listings array. To sort it, write it like this:
foreach($listings as $k => $a){
ksort($a, SORT_NUMERIC);
$listings[$k] = $a;
}
array_walk(
$listings,
function(&$value) {
ksort($value, SORT_NUMERIC);
}
);
This question already has answers here:
Merge row data from multiple arrays
(6 answers)
Closed 5 months ago.
here: Transforming array values in elements of a subarray using PHP I asked quite the same, but the arrays were flatter. I tried to adapt the code, but unfortunately without success.
How could I merge following arrays so the second array won't be added after the end of the first array, but each subarray of the first array will receive the correspondent values of subarrays of the second. In other words, I want to merge the subarrays. Here my example:
Array 01:
Array01 (
[0] => Array
(
[0] => 40292633
[1] => 412
)
[1] => Array
(
[0] => 41785603
[1] => 382
)
[2] => Array
(
[0] => 48792980
[1] => 373
)
[3] => Array
(
[0] => 44741143
[1] => 329
))
Array 02:
Array02(
[0] => Array
(
[0] => 3460581
[1] => 1407424B1
[2] => 951753
)
[1] => Array
(
[0] => 3484251
[1] => 1028325B1
[2] => 159357
)
[2] => Array
(
[0] => 3519102
[1] => 0586365A1
[2] => 456654
)
[3] => Array
(
[0] => 3529714
[1] => 1059876A1
[2] => 852258
))
Final array:
finalArray(
[0] => Array
(
[0] => 40292633
[1] => 412
[2] => 3460581
[3] => 1407424B1
[4] => 951753
)
[1] => Array
(
[0] => 41785603
[1] => 382
[2] => 3484251
[3] => 1028325B1
[4] => 159357
)
[2] => Array
(
[0] => 48792980
[1] => 373
[2] => 3519102
[3] => 0586365A1
[4] => 456654
)
[3] => Array
(
[0] => 44741143
[1] => 329
[2] => 3529714
[3] => 1059876A1
[4] => 852258
))
Many thanks in advance!
try this code
function merge_arrays($a1, $a2) {
return array($a1, $a2);
}
$result = array_map("merge_arrays", $a1, $a2);
foreach($result as $nr)
{
$nres[] = array_merge ($nr[0], $nr[1]);
}
Try this:
$result = array();
$keys = array_unique( array_merge(array_keys($arr1), array_keys($arr2)) );
foreach($keys as $key) {
if( !array_key_exists($key, $arr1) ) {
$result[$key] = $arr2;
} else if( !array_key_exists($key, $arr2) ) {
$result[$key] = $arr1;
} else {
$result[$key] = array_merge($arr1[$key], $arr2[$key]);
}
}
It does not assume that both arrays have the same keys.
If you'd like to use array_map, this is an equivalent solution:
$keys = array_unique( array_merge(array_keys($arr1), array_keys($arr2)) );
$result = array_map(function($key) use ($arr1,$arr2) {
if( !array_key_exists($key, $arr1) ) {
return $arr2;
} else if( !array_key_exists($key, $arr2) ) {
return $arr1;
}
return array_merge($arr1[$key], $arr2[$key]);
},
$keys);
If you're sure both arrays have the same keys, you can try this:
$result = array();
foreach($arr1 as $key => $arr1val) {
$result[$key] = array_merge($arr1val, $arr2[$key]);
}
I've an array in php something like below
Array
(
[0] => Array
(
[0] => 40173
[1] => 514081
[2] => 363885
[3] => 891382
),
[1] => Array
(
[0] => 40173
[1] => 5181
[2] => 385
[3] => 891382
)
)
Now I want to remove the parents indexes 0,1... and finally want to get all the values (only unique values).
Thanks.
One possible approach is using call_user_func_array('array_merge', $arr) idiom to flatten an array, then extracting unique values with array_unique():
$new_arr = array_unique(
call_user_func_array('array_merge', $old_arr));
Demo. Obviously, it'll work with array of any length.
$startArray = Array
(
[0] => Array
(
[0] => 40173
[1] => 514081
[2] => 363885
[3] => 891382
),
[1] => Array
(
[0] => 40173
[1] => 5181
[2] => 385
[3] => 891382
)
);
//Edited to handle more the 2 subarrays
$finalArray = array();
foreach($startArray as $tmpArray){
$finalArray = array_merge($finalArray, $tmpArray);
}
$finalArray = array_unique($finalArray);
Using RecursiveArrayIterator Class
$objarr = new RecursiveIteratorIterator(new RecursiveArrayIterator($yourarray));
foreach($objarr as $v) {
$new_arr[]=$v;
}
print_r(array_unique($new_arr));
Demo
OUTPUT:
Array
(
[0] => 40173
[1] => 514081
[2] => 363885
[3] => 891382
[5] => 5181
[6] => 385
)
$new_array = array_merge($array1, $array2);
$show_unique = array_unique($new_array);
print_r($show_unique);
array_merge is merging the array's, array_unique is removinge any duplicate values.
Try something like this:
$new_array = array();
foreach($big_array as $sub_array) {
array_merge($new_array, $sub_array);
}
$new_array = array_unique($new_array);
(code not tested, this just a concept)
Try this:
$Arr = array(array(40173, 514081, 363885, 891382),
array(40173,5181, 385,891382));
$newArr = array();
foreach($Arr as $val1)
{
foreach($val1 as $val2)
{
array_push($newArr, $val2);
}
}
echo '<pre>';
print_r(array_unique($newArr));
Output:
Array
(
[0] => 40173
[1] => 514081
[2] => 363885
[3] => 891382
[5] => 5181
[6] => 385
)
Refer: https://eval.in/124240
--
Thanks
I have another array unique question in the endless list of questions about them.
I can imagine this problem is quite simple to solve but I simply do not come on it.
Just because there are so many questions on this subject i wasn't able to find anything useful in this case.
the array:
Array
(
[0] => Array
(
[0] => blabla values
[1] => 91.181818181818
)
[1] => Array
(
[0] => blabla same values
[1] => 95.333333333333
)
[2] => Array
(
[0] => blabla other values
[1] => 86
)
[3] => Array
(
[0] => blabla other values
[1] => 92.5
)
[4] => Array
(
[0] => blabla same values
[1] => 88.5
)
)
I want to unique the array by the first array dimension and only keep the entry with the highest value from the second.
Maybe in MYSQL this would be no big deal but at the moment i am not able to implement something like that in php.
desired output array would be:
Array
(
[0] => Array
(
[0] => blabla values
[1] => 91.181818181818
)
[1] => Array
(
[0] => blabla same values
[1] => 95.333333333333
)
[2] => Array
(
[0] => blabla other values
[1] => 92.5
)
)
Has anyone a clever idea?
<?php
$list = array(
array('blabla values',91.181818181818),
array('blabla same values', 95.333333333333),
array('blabla other values', 86),
array('blabla other values', 92),
array('blabla same values', 88.5),
);
$result = array();
foreach ($list as $item)
{
$key = $item[0];
$value = $item[1];
if (!isset($result[$key]) || $result[$key][1] < $value)
{
$result[$key] = $item;
}
}
$result = array_values($result);
print_r($result);
the output:
Array
(
[0] => Array
(
[0] => blabla values
[1] => 91.1818181818
)
[1] => Array
(
[0] => blabla same values
[1] => 95.3333333333
)
[2] => Array
(
[0] => blabla other values
[1] => 92
)
)
usort($arr, function ($a, $b){
return $a[1] - $b[1];
});
$out = array();
foreach ($arr as $key => $value){
$out[$value[0]] = $value[1];
}
$arr = array_map(NULL, array_keys($out), $out);
Output:
Array
(
[0] => Array
(
[0] => blabla same values
[1] => 95.333333333333
)
[1] => Array
(
[0] => blabla other values
[1] => 86
)
[2] => Array
(
[0] => blabla values
[1] => 91.181818181818
)
)