How to Sort all data from array - php

how can i crawl this array with a for loop or foreach ,
this is the content of a single array:
Array ( [0] => 1 ) Array ( [0] => 1 [1] => 2 ) Array ( [0] => 1 [1] => 2 [2] => 3 )

If you mean what I think you mean...
try this:
$tempArray = array();
foreach($myarray as $k => $v){
foreach($v as $ke => $val ){
$tempArray[] = $val;
}
}
sort($tempArray);
Or if you want to avoid duplicates try:
$temp = array_unique($myArray);
sort($temp);

Related

Change value inside an array

I'd like to change the values of an array.
Currently my array looks like this:
Array
(
[0] => Array
(
[0] => 12-Multi_select-customfield-retina-ready+Yes
[1] => 12-Multi_select-customfield-retina-ready+N/A
[2] => 12-Multi_select-customfield-retina-ready+No
)
)
I want to remove everything before the + symbol, so in the end the new array will looke like this
Array
(
[0] => Array
(
[0] => Yes
[1] => N/A
[2] => No
)
)
This is my code:
$new_array = array();
foreach( $array as $key => $value ) {
$split = explode("+", $value[0]);
$new_array[] = $split[1];
}
Hoping that it would worked, but when I check the new array, it only shows one value.
Array
(
[0] => Yes
)
Any help in putting me in the right direction is much appreciated.
Please, check it:
<?php
$array[0][0] = '12-Multi_select-customfield-retina-ready+Yes';
$array[0][1] = '12-Multi_select-customfield-retina-ready+N/A';
$array[0][2] = '112-Multi_select-customfield-retina-ready+No';
echo '<pre>';
print_r($array);
$new_array = array();
foreach( $array[0] as $key => $value ) {
$split = explode("+", $value);
$new_array[] = $split[1];
}
print_r($new_array);
echo '</pre>';
Try This this work even if you have multiple key in the original array $original_array[0], $original_array[1] ... :
$original_array[0] = [
0 => '12-Multi_select-customfield-retina-ready+Yes',
1 => '12-Multi_select-customfield-retina-ready+N/A',
2 => '12-Multi_select-customfield-retina-ready+No'
];
print_r($original_array);
$new_array = [];
foreach ($original_array as $key => $value) {
foreach ($value as $index => $val) {
$split = explode("+", $val);
$new_array[$key][] = $split[1];
}
}
print_r($new_array);
Example :
Original array
Array
(
[0] => Array
(
[0] => 12-Multi_select-customfield-retina-ready+Yes
[1] => 12-Multi_select-customfield-retina-ready+N/A
[2] => 12-Multi_select-customfield-retina-ready+No
),
[1] => Array
(
[0] => 12-Multi_select-customfield-retina-ready+Yes
[1] => 12-Multi_select-customfield-retina-ready+N/A
[2] => 12-Multi_select-customfield-retina-ready+No
)
)
New Array
Array
(
[0] => Array
(
[0] => Yes
[1] => N/A
[2] => No
),
[1] => Array
(
[0] => Yes
[1] => N/A
[2] => No
)
)

add values in php based off first value php

I have an array as shown below which some rows will have the same name. I am getting nowhere fast trying to do what i want to achieve.
With a foreach loop how i would go through each one add the values of the same name up and put it into a new array?
Thanks :)
Array
(
[0] => Array
(
[0] => Name1
[1] => Value
)
[1] => Array
(
[0] => Name2
[1] => Value
)
[2] => Array
(
[0] => Name1
[1] => Value
)
[3] => Array
(
[0] => Name3
[1] => Value
)
[4] => Array
(
[0] => Name2
[1] => Value
)
This only a snippet and will change to length when the csv it comes from changes weekly Thanks
It's not the most efficient way yet but it's working the way you want it to happen.
<?php
$testArr = array(
array('Name1', 1),
array('Name2',2),
array('Name1',3)
);
$newArr = array();
$tmp = array();
foreach($testArr as $i=>$row)
{
if( in_array($row[0], $tmp) )
{
$key = array_search($row[0], $tmp);
$newArr[ $key ][1] += $row[1];
}
else
{
array_push( $newArr, array($row[0], $row[1]) );
array_push( $tmp, $row[0] );
}
}
print_r($newArr);
?>
OUTPUT
Array
(
[0] => Array
(
[0] => Name1
[1] => 4
)
[1] => Array
(
[0] => Name2
[1] => 2
)
)
Tested working, Check it here, phpFiddle
Do you want something like this?
$newData = [];
foreach ($array as $item) {
$newData[$item[0]][] = $item[1];
}
var_dump($newData);
You can get to the result with this approach.
$data_array = Array{};
$result_array = NULL;
foreach ($data_array as $value) {
$result_array[$value[0]] += $value[1];
}

how to intersect 2 multidimensional array into third multimensional array

i have 2 arryas and i want them to intersect and store the finding matches into third array with values from first array and second array.
the first array looks like this:
Array
(
[0] => Array
(
[0] => 45
[1] => 10640
[2] => 1041-0567041700116
)
[1] => Array
(
[0] => 46
[1] => 10640
[2] => 1041-0567041700318
)
[2] => Array
(
[0] => 207
[1] => 10645
[2] => 03320103000052
)
and the second array:
Array
(
[0] => Array
(
[0] => 03320103000052
[1] => 0
)
[1] => Array
(
[0] => 10013800805001
[1] => 12
)
[2] => Array
(
[0] => 1090-0360141758201
[1] => 3
)
the out put should be:
Array
(
[0] => Array
(
[0] => 207 =>value from first array
[1] => 10645 =>value from first array
[2] => 03320103000052 =>value from first and second array (this is what i need to compare)
[3] => 0 =>value from second array
)
this is similar to this post
but i have problems to store data into multidimensional array
thanks in forward for any suggestions and help
You can do this with only two foreach loops and one if statement:
$combined = array();
foreach ($array1 as $a) {
foreach ($array2 as $b) {
if ($a[2] == $b[0]) {
$combined[] = array($a[0], $a[1], $a[2], $b[1]);
}
}
}
The following is the test I set up to try this:
<?php
$array1 = array();
$array1[] = array('45', '10640', '1041-0567041700116');
$array1[] = array('46', '10640', '1041-0567041700318');
$array1[] = array('207', '10645', '03320103000052');
$array2 = array();
$array2[] = array('03320103000052', '0');
$array2[] = array('10013800805001', '12');
$array2[] = array('1090-0360141758201', '3');
$combined = array();
foreach ($array1 as $a) {
foreach ($array2 as $b) {
if ($a[2] == $b[0]) {
$combined[] = array($a[0], $a[1], $a[2], $b[1]);
}
}
}
print_r($combined);
?>

How to sum array values based on keys?

The first array
Array
(
[0] => Array
(
[1] => 2
)
[1] => Array
(
[1] => 2
)
[2] => Array
(
[2] => 1
)
[3] => Array
(
[3] => 1
)
)
I want output like
Array
(
[0] => Array
(
[1] => 4
)
[1] => Array
(
[2] => 1
)
[2] => Array
(
[3] => 1
)
)
How can i do this?
Seems like a good case for array_reduce():
$res = array_chunk(array_reduce($arr, function(&$current, $item) {
foreach ($item as $key => $value) {
if (!isset($current[$key])) {
$current[$key] = 0;
}
$current[$key] += $value;
}
return $current;
}, []), 1, true);
For the final result I'm using array_chunk(); it takes an array and creates single element sub arrays of each element.
$result = array();
foreach ($input as $subarray) {
foreach ($subarray as $key => $value) {
if (isset($result[$key])) {
$result[$key][$key] += $value;
} else {
$result[$key] = array($key => $value);
}
}
}
$result = array_values($result); // Convert from associative array to indexed array

how to get array from get to normal array in php

I have an array like this and it can contain multiple values:
Array
(
[rpiid] => Array
(
[1] => 86
)
[sensor_id] => Array
(
[1] => 1
)
[when] => Array
(
[1] => 2014-02-24
)
[val] => Array
(
[1] => 000
)
[train] => Array
(
[1] => True
)
[valid] => Array
(
[1] => False
)
[button] => update
)
Of course, here there is only the number 1 each time but sometimes I have 0, 1, 2 and a value associated. This is because I get this from a GET from multiple forms.
How can I transform this array into
Array
(
[0] => Array
(
[rpiid] => 86
[sensor_id] => 1
...
Thanks,
John.
if your array is $get
$newArray = Array();
foreach($get as $secondKey => $innerArray){
foreach($value as $topKey => $value) {
$newArray[$topKey][$secondKey] = $value;
}
}
This should work
$new_array = array();
foreach($first_array as $value => $key){
$new_array[$key] = $value[1];
}
Sure you can, take a look at this small example:
$a = [ 'rpid' => [1], 'cpid' => [2,2] ];
$nodes = [];
foreach($a as $node => $array) {
foreach($array as $index => $value) {
if(empty($nodes[$index]))
$nodes[$index] = [];
$nodes[$index][$node] = $value;
}
}
print_r($nodes):
Array
(
[0] => Array
(
[rpid] => 1
[cpid] => 2
)
[1] => Array
(
[cpid] => 2
)
)

Categories