How to use array_push in 2D array? - php

I am having an array like this
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 6
[5] => 7
[6] => 8
[7] => 9
)
[4] => Array
(
[0] => 2
[1] => 3
[2] => 4
[3] => 5
[4] => 6
[5] => 7
[6] => 8
[7] => 9
[8] => 10
[9] => 11
)
)
Now i want to put this into another array using array_push keyword...
How can i achieve this?

<?php
foreach($yourArray as $array) {
array_push($firstArray, $array);
}
?>
or
<?php
foreach($yourArray as $array) {
$firstArray[] = $array;
}
?>
or
<?php
array_push($firstArray, $array);
?>

$shiftedarray=array();
$aftershift=array();
foreach($twodarray as $key=>$val)
{
//Remove 0th index in array
$shiftedarray[]=array_shift($val);
//Array After Removed 0th index
$aftershift[]=$val;
}
echo "<pre>";
print_r($shiftedarray);
print_r($aftershift);
$oneDimensionalArray = call_user_func_array('array_merge', $aftershift);
print_r($oneDimensionalArray);

Related

Concatenate value elements of two or more different arrays in PHP

I originally have this array
Array
(
[0] => Amministrativo ^25^9,11,2,10,18,4,7,^17,13,^0.75^0^0.25
[1] => Logico deduttive^7^^21,^0.75^0^-0.25
[2] => Situazionali^8^^20,^0.75^0^0.375
)
Using the function explode and array_diff i can get to this
Array
(
[0] => Amministrativo
[1] => 25
[2] => 9,11,2,10,18,4,7,
[3] => 17,13,
[4] => 0.75
[5] => 0
[6] => 0.25
)
Array
(
[0] => Logico deduttive
[1] => 7
[2] =>
[3] => 21,
[4] => 0.75
[5] => 0
[6] => -0.25
)
Array
(
[0] => Situazionali
[1] => 8
[2] =>
[3] => 20,
[4] => 0.75
[5] => 0
[6] => 0.375
)
but I would like to concatenate the elements of each array to get a unique array. I think I need to use the array_map function but I don't know how. This below is the result I would like to achieve
Array (
[0] => Amministrativo Logico deduttive Situazionali
[1] => 25 7 8
[2] => 9,11,2,10,18,4,7,
[3] => 17,13,21,20,
[4] => 0.75 0.75 0.75
[5] => 0 0 0
[6] => 0.25 -0.25 0.375
)
tnks
EDIT:
I tried the code that is here and it is fine.
But now I realized that there is also the problem that the arrays can be in variable number 1, 2, 3 or more and I can't know it before, I should adapt this code
$result = array_map(function ($item1, $item2,$item3) {
return "$item1 $item2 $item3";
}, $test[0], $test[1],$test[2]);
The lines of the original array are split with explode. The result is a two-dimensional array. This is transposed (swapping rows and columns). Then the lines are reassembled with implode.
function array_transpose(array $a) {
$r = array();
foreach($a as $keyRow => $subArr) {
foreach($subArr as $keyCol => $value) $r[$keyCol][$keyRow] = $value;
}
return $r;
}
$arr = [
'Amministrativo ^25^9,11,2,10,18,4,7,^17,13,^0.75^0^0.25',
'Logico deduttive^7^^21,^0.75^0^-0.25',
'Situazionali^8^^20,^0.75^0^0.375',
];
foreach($arr as $i => $row){
$arr[$i] = explode('^',$row);
}
$arr = array_transpose($arr);
foreach($arr as $i => $row){
$arr[$i] = implode(' ',$row);
}
var_export($arr);
Demo: https://3v4l.org/IdsDh

Remove internal array in php

I have to remove an internal array from an array. Actually, the array is obtained by decoding JSON, and can go upto n levels. I need to remove an internal array from an array of its parent based on the key which is dynamic. Below is the code that I have tried referring to answers on php arrays.
$quotationHistory = json_decode($quotationCollection->getHistory(), true);
$quotationId = 5;
foreach ($quotationHistory as $sales_id => $history) {
foreach($history as $quotationIdValue => $values) {
if ($quotationId == $quotationIdValue) {
unset ($history[$quotationIdValue]);
}
}
}
sample:
Array
(
[1] => Array
(
[5] => Array
(
[0] => Array
(
[0] => 3
[1] => 8490.0000
[2] => 21-10-2016 11:43:18am
[3] => 24-11-2016 11:43:18am
[4] => 199
[5] => rtg
)
[1] => Array
(
[0] => 12
[1] => 8490.0000
[2] => 21-10-2016 11:43:40am
[3] => 24-11-2016 11:43:18am
[4] => 199
[5] => rtg
)
[2] => Array
(
[0] => 45
[1] => 8490.0000
[2] => 21-10-2016 11:43:54am
[3] => 24-11-2016 11:43:18am
[4] => 199
[5] => rtg
)
[3] => Array
(
[0] => 11
[1] => 8490.0000
[2] => 21-10-2016 11:44:04am
[3] => 24-11-2016 11:43:18am
[4] => 199
[5] => rtg
)
[4] => Array
(
[0] => 54
[1] => 8490.0000
[2] => 21-10-2016 11:44:16am
[3] => 24-11-2016 11:43:18am
[4] => 199
[5] => rtg
)
)
)
)
Now, I want to remove the second level data with key = 5
You'd better do the unset on the original array:
$quotationHistory = json_decode($quotationCollection->getHistory(), true);
foreach($quotationHistory as $sales_id => $history) {
foreach($history as $quotationIdValue => $values) {
if($quotationId == $quotationIdValue){
unset($quotationHistory[$sales_id][$quotationIdValue]);
}
}
}
The reason is that the internal array is passed as a copy. But you can also specify an assignation by reference:
In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference (official Php doc).
$quotationHistory = json_decode($quotationCollection->getHistory(), true);
foreach($quotationHistory as $sales_id => &$history) {
foreach($history as $quotationIdValue => &$values) {
if($quotationId == $quotationIdValue){
unset($$history[$quotationIdValue]);
}
}
}
You will probably want a recursive iterator since the return can be any level down in the array (presumably that's what you mean by n levels). One note, it would remove any key with the same value so it would remove any key with 5. You would be better to recurse to remove the key based on value rtg:
$arr = array(
'one'=>array(
'one'=>'and a half',
'two'=>array(
'three'=>'three and a half'
)
)
);
function recurse($array,$remove=false)
{
foreach($array as $key => $value) {
if($key != $remove){
if(is_array($value)) {
$new[$key] = recurse($value,$remove);
}
else
$new[$key] = $value;
}
}
return $new;
}
# Run the iterator to remove the key named "three"
print_r(recurse($arr,'three'));
If you need to search by value, this should work:
function recurseValue($array,$remove=false)
{
foreach($array as $key => $value) {
if(is_array($value)) {
$new[$key] = recurseValue($value,$remove);
}
else {
if($value != $remove){
$new[$key] = $value;
}
}
}
return $new;
}
# Run the iterator to remove the key with the value named "Three and a half"
print_r(recurseValue($arr,'three and a half'));
Gives you:
Array
(
[one] => Array
(
[one] => and a half
[two] =>
)
)
This last option will normalize the array to one level so you can loop over it normally:
function recurseArray($array,&$new,$bkey = false)
{
foreach($array as $key => $value) {
if(is_array($value)) {
recurseArray($value,$new,$key);
}
else {
$new[$bkey][] = $value;
}
}
}
$new = array();
recurseArray($arr,$new);
print_r($new);
Gives you:
Array
(
[0] => Array
(
[0] => 3
[1] => 8490.0000
[2] => 21-10-2016 11:43:18am
[3] => 24-11-2016 11:43:18am
[4] => 199
[5] => rtg
)
[1] => Array
(
[0] => 3
[1] => 8490.0000
[2] => 21-10-2016 11:43:18am
[3] => 24-11-2016 11:43:18am
[4] => 199
[5] => rtg
)
[2] => Array
(
[0] => 3
[1] => 8490.0000
[2] => 21-10-2016 11:43:18am
[3] => 24-11-2016 11:43:18am
[4] => 199
[5] => rtg
)
[3] => Array
(
[0] => 3
[1] => 8490.0000
[2] => 21-10-2016 11:43:18am
[3] => 24-11-2016 11:43:18am
[4] => 199
[5] => rtg
)
)

How would i Print An Array like array([2]=>4,[6]=>8,[5]=>7)

I have been given array like
array(2,4,5,6,7,8).
Now I was asked to do some array operation which will give me output as
array([2]=>4,[6]=>8,[5]=>7)
Try this..
<?php
$array=array("2"=>4,"6"=>8,"5"=>7);
$data=array_keys($array);
$data1=array_values($array);
$result = array_merge($data, $data1);
sort($result);
print_r($result);
?>
Output:Array ( [0] => 2 [1] => 4 [2] => 5 [3] => 6 [4] => 7 [5] => 8 )

PHP highlight duplicate values in array [duplicate]

This question already has answers here:
How to detect duplicate values in PHP array?
(13 answers)
Closed 8 years ago.
Ok I am pretty sure there is a simple solution, and I am missing something on this but
lets say I have this a simple array:
Array
(
[0] => 79990
[1] => 79040
[2] => 79100
[3] => 79990
[4] => 79490
[5] => 79290
[6] => 79990
)
0, 3 and 6 are the same value
how do I mark/highlight these values on a foreach loop? result should be something like:
Array
(
[0] => *79990*
[1] => 79040
[2] => 79100
[3] => *79990*
[4] => 79490
[5] => 79290
[6] => *79990*
)
edit: typos
This should do the trick:
<?php
$array = array( '79900',
'79040',
'79100',
'79990',
'79490',
'79290',
'79990');
$count = array_count_values($array);
echo "<pre>".print_r($array, true)."</pre>";
foreach($array as $val)
{
if($count[$val]>1) {
$output[] = "*".$val."*";
} else {
$output[] = $val;
}
}
echo "<pre>".print_r($output, true)."</pre>";
?>
Outputs:
Array
(
[0] => 79900
[1] => 79040
[2] => 79100
[3] => 79990
[4] => 79490
[5] => 79290
[6] => 79990
)
Array
(
[0] => 79900
[1] => 79040
[2] => 79100
[3] => *79990*
[4] => 79490
[5] => 79290
[6] => *79990*
)
Note: Your [0] isn't actually the same as [3] and [6], but I'm assuming this is just a typo
Let me know how you get on!
$array = array("79900","79040","79100","79990","79490","79290","79990");
$count = array_count_values( $array );
$list = array();
foreach( $count as $index => $value ){
if( $value > 1 ){
$list[] = "*" . $index . "*";
}else{
$list[] = $index;
}
}
Note that the repeated index is removed

sort results of array count values into a new array

I want to sort the results to a new array but without the $key
so the most UNunique number will be first (or the most duplicated number will be first).
<?php
$a = array (1,1,2,2,2,3,3,3,3,4,4,4,4,4,5);
foreach (array_count_values($a) as $key => $value) {
echo $key.' - '.$value.'<br>';
}
//I am expecting to get the most duplicated number FIRST (without the $key)
//so in that case :
// $newarray = array(4,3,2,1,5);
?>
$a = array (1,1,2,2,2,3,3,3,3,4,4,4,4,4,5);
$totals = array_count_values($a);
arsort( $totals );
echo "<pre>";
print_r($totals);
Output
Array
(
[4] => 5
[3] => 4
[2] => 3
[1] => 2
[5] => 1
)
Do like this
<?php
$a = array (1,1,2,2,2,3,3,3,3,4,4,4,4,4,5,6,7,8,9);
$n=array_count_values($a);
arsort($n);
print_r(array_keys($n));
Demo
OUTPUT:
Array
(
[0] => 4
[1] => 3
[2] => 2
[3] => 1
[4] => 9
[5] => 8
[6] => 5
[7] => 6
[8] => 7
)
$newarray = array_keys(array_count_values($a));

Categories