$newarry = array
(
[0] => 2
[1] => 4
)
$serial_model_itemid = array
(
[0] => 0
[1] => 2
[2] => 1
[3] => 2
)
need to merge both arrays and get like
array
(
[2] => 2 // in 2nd array [1]+[3] as single count
[4] => 1 // in 2nd array [0] + [2] as single count
)
I have tried:
$indent_det_id1 = array_unique($data['indent_detail_id1tabl']);
$serial_model_itemid = array_count_values($data['item_id']);
$sortted_itm_id = sort($serial_model_itemid, SORT_NUMERIC);
$itmid_zero = array_splice($serial_model_itemid,0);
unset($itmid_zero[0]);
$newarry = array_filter($indent_det_id1);
$serial_model_itemid = array_count_values($data['item_id']);
print_r( $serial_model_itemid);
$a = array_unique( $serial_model_itemid);
print_r( $data['item_id']);
$serial_model_itemid = array_count_values($data['item_id']);
//$sortted_itm_id = sort($serial_model_itemid, SORT_NUMERIC);
$pos = array_search (0,$serial_model_itemid);
echo "0 founded at ".$pos;
unset($serial_model_itemid[$pos]);
print_r($serial_model_itemid);
print_r( $serial_model_itemid);
$a = array_unique( $serial_model_itemid);
// count items in array
$serial_model_itemid = array_count_values($data['item_id']);
// search the array
$pos = array_search (0,$serial_model_itemid);
// echo where it was found
echo "0 found at ".$pos;
// unset it
unset($serial_model_itemid[$pos]);
// print out for debug
print_r($serial_model_itemid);
// Assign the unique key
$a = array_unique( $serial_model_itemid);
Use the function array_count_values
<?php
$n = array(0,2,1,2);
$newarray = array_count_values($n);
echo '<pre>'.print_r($newarray, true).'</pre>';
?>
Result
Array
(
[0] => 1
[2] => 2
[1] => 1
)
Here's what I found worked:
// count items in array
$serial_model_itemid = array_count_values($data['item_id']);
// search the array
$pos = array_search (0,$serial_model_itemid);
// echo where it was found
echo "0 found at ".$pos;
// unset it
unset($serial_model_itemid[$pos]);
// print out for debug
print_r($serial_model_itemid);
// Assign the unique key
$a = array_unique( $serial_model_itemid);
Related
I have a little problem. Here is the code:
$arr = explode(',', $odluka);
$arr2 = array($arr[0], $arr[1], $arr[2], $arr[3], $arr[4], $arr[5], $arr[6], $arr[7], $arr[8], $arr[9]);
while ($arrk = current($arr2)) {
if ($arrk == '1') {
$ark = key($arr2);
//print_r($ark);
//echo $arr2[$ark];
$arop = explode(',', $utroseno);
$aropk = array($arop[0], $arop[1], $arop[2], $arop[3], $arop[4], $arop[5], $arop[6], $arop[7], $arop[8], $arop[9]);
$array = array($aropk[$ark]);
print_r($array);
}
next($arr2);
}
Output of $array is
Array ( [0] => 1 ) Array ( [0] => 5 ) Array ( [0] => 10 ) Array ( [0] => 4 ) Array ( [0] => 4 ) Array ( [0] => 1 ) Array ( [0] => 1 )
How can I merge this values and sum them. I want sum of 1+5+10+4+4+1+1. Thanks!
declare a variable to store sum
iterate over array
-> add value to sum
Here is a simple example how to deal with your output array:
$data = [
[1], [5], [10], [4]
];
$sum = array_sum(array_map(function($elem) { return $elem[0]; }, $data));
var_dump($sum);
You don't need to assign them to another array and loop..you can just sum everything after explode. You just need one line of code for that:
array_sum(explode(',', $odluka));
Then you'll get the sum of all the numbers
Not need using any array and loop.You are using only "array_sum ()" php building function.Like
<?php
$foo[] = "12";
$foo[] = 10;
$foo[] = "bar";
$foo[] = "summer";
echo array_sum ($foo); //same as echo "22";
?>
For more information Read Php Manual link
Use this function
array_sum ($arr);
using php how to get array result into below way,
Array
(
[3] => Array
(
[15] => 15
[16] => 16
[17] => 17
[18] => 18
[19] => 19
)
)
how to convert above array into below format,
Array
(
[0] => 15
[1] => 16
[2] => 17
[3] => 18
[4] => 19
)
array_values() is your friend;
Presuming your array exists in a variable called $array;
$newArray = array_values($array[3]);
you should use RecursiveArrayIterator to remove parent array
$arr = new RecursiveIteratorIterator(new RecursiveArrayIterator($multidimensional_array));
$new_arr = iterator_to_array($arr, false);
try this, if you have more than one sub-array, it will work.
$arr = array(3 =>
array
(
15 => 15,
16 => 16,
17 => 17,
18 => 18,
19 => 19
)
);
$new = array();
foreach ($arr as $v){
$new = array_merge($new , array_values($v)) ;
}
echo "<pre>"; print_r($new);
Working Demo
Have not tested it but should work as per your requirement.
<?php
$parent array = array(); // The array which you want to change
$result_array = array(); // The array that will hold the results
foreach( $parent_array as $child_array )
{
if( is_array( $child_array ) )
{
foreach( $child_array as $element )
{
$result_array[] = $element
}
}
}
echo '<pre>';
print_r($result_array);
echo '</pre>';
?>
it's pretty simple, as you can just assign the array holding variable to the values of one index …
<?php
/* build list */
for($i=15;$i<=19;$i++)
$b[$i] = $i;
$a[3] = $b;
var_export($a);
/* make array smaller again */
$a = $a[3];
var_export($a);
/* reindexing, just values */
$a = array_values( $a );
var_export($a);
?>
the reindexing part is done by a build-in function, also have a look on php.net for the linked functions for arrays, you can do massive stuff simple with them.
I need to get numbers as an array from a given string.
Example string:
$t = '1-P,2-T,3-P,4-R,5-C,6-T,';
Expected output:
if I search -T the output needs to be like this:
array(
[0] => 2,
[1] => 6
)
if it's -P:
array(
[0] => 1,
[1] => 3
)
I tried var_export(explode("-T,",$t)); but it didn't work as expected.
Can any one give me a suggestion to get this?
The below matches the full integer number which preceeds the search term -P.
Let's keep it concise:
$matches = array();
if (preg_match_all('/([0-9]+)\-P/', $t, $matches) >= 1) {
var_dump($matches[1]);
}
Search for '/([0-9]+)\-P/, '/([0-9]+)\-C/, '/([0-9]+)\-T/ an so on.
A more dynamic way to look for different search terms/filters:
$filter = '-T';
$pattern = sprintf('/([0-9]+)%s/', preg_quote($filter));
See preg_match_all and preg_quote functions.
Try this:
$t = '211111111131-P,2-T,3654554-P,4-R,5-C,6-T,';
$find = "-P"; // Search element
$found = []; // Result array
$array = explode(",", $t); // Breaking up into array
foreach($array as $arr) {
if (strpos($arr, $find)) { // Checking if search element is found in $arr
$found[] = explode('-',$arr)[0]; // Extracting the number prefix e.g 1 for 1-P
}
}
Output:
Array
(
[0] => 1
[1] => 3
)
Use it as
$t = '1-P,2-T,3-P,4-R,5-C,6-T,';
$data = explode(",", $t);
print_r($data);
$row=array();
for ($i = 0; $i <= count($data); $i++) {
if (!empty($data[$i])) {
if (strpos($data[$i], '-T') !== false) {// pass find value here
$final = explode("-", $data[$i]);
$row[]=$final[0];
}
}
}
print_r($row);
Output
Array
(
[0] => 2
[1] => 6
)
DEMO
$t = '1-P,2-T,3-P,4-R,5-C,6-T,';
$temp = [];
// if the last comma is not typo the 3rd argument `-1` omit empty item
$array = explode(",", $t, -1);
foreach($array as $arr) {
list($v, $k) = explode('-', $arr);
$temp[$k][] = $v;
}
print_r($temp['T']);
demo
Lots of good answers here already, but none take the approach of first putting the data into a better structure.
The code below converts the data to an associative array mapping letters to arrays of numbers, so that you can then do repeated lookups by whichever letter you want:
$t = '1-P,2-T,3-P,4-R,5-C,6-T,';
$a = array_filter(explode(',', $t));
$map = [];
foreach($a as $item) {
$exploded = explode('-', $item);
$number = $exploded[0];
$letter = $exploded[1];
if (!array_key_exists($letter, $map)) {
$map[$letter] = [];
}
$map[$letter][] = $number;
}
print_r($map);
// Array
// (
// [P] => Array
// (
// [0] => 1
// [1] => 3
// )
//
// [T] => Array
// (
// [0] => 2
// [1] => 6
// )
//
// [R] => Array
// (
// [0] => 4
// )
//
// [C] => Array
// (
// [0] => 5
// )
//
// )
print_r($map['T']);
// Array
// (
// [0] => 2
// [1] => 6
// )
print_r($map['P']);
// Array
// (
// [0] => 1
// [1] => 3
// )
I am trying to "subtract" the values of an array in php. I used array_diff but it doesn't seem to work for more than one value.
<?php
$array1 = array(1,3,7,10,7);
$array2 = array(1,7);
$result=array_diff($array1,$array2);
print_r($result);
?>
//Output//
Array ( [1] => 3 [3] => 10 )
What I would like to do is return 3,7,10 instead of excluding all 7's. Thanks in advance!
Try:
$array1 = array(1,3,7,10,7);
$removals = Array(1,7);
foreach( $removals as $remove ) {
foreach( $array1 as $key => $value ) {
if ($value === $remove ) {
unset($array1[ $key ]);
break;
}
}
}
print_r($array1); // Array ( [1] => 3 [3] => 10 [4] => 7 )
sort($array1)
print_r($array1); // Array ( [0] => 3 [1] => 7 [2] => 10 )
based on thelastshadows post but shorter and may faster because only one foreach
$array1 = array(1,3,7,10,7);
$removals = Array(1,7);
foreach( $removals as $remove ) {
unset($array1[array_search($remove,$array1)]);
}
sort($array1);
print_r($array1);
I have the following code and am trying to compare two array's with array_diff however I keep getting no results. I not sure if it matters, but there are many fields in the array and I really only want to compare 1 field...is this possible? what am I missing?
<?php
$json = file_get_contents("http://ebird.org/ws1.1/data/obs/region/recent?rtype=subnational1&r=US-AZ&back=7&fmt=json");
$json2 = file_get_contents("http://ebird.org/ws1.1/data/obs/region/recent?rtype=subnational1&r=US-NV&back=7&fmt=json");
$array1 = json_decode($json, TRUE);
$array2 = json_decode($json2, TRUE);
if ( $array1 == $array2 ) {
echo 'There are no differences';
}else
var_dump(array_diff($array2, $array1));
echo 'they are different';
?>
You will need to check the arrays against each other:
$Array_1 = array (1,2,3,4,5);
$Array_2 = array(1,2,3,4,5,6);
print_r(array_diff($Array_1,$Array_2));
Will output:
Array
(
)
Whereas:
print_r(array_diff($Array_2,$Array_1));
will output:
Array
(
[5] => 6
)
So this might be a solution:
function ArrayDiff ($Array_1, $Array_2){
$Compare_1_To_2 = array_diff($Array_1,$Array_2);
$Compare_2_To_1 = array_diff($Array_2,$Array_1);
$Difference_Array = array_merge($Compare_1_To_2,$Compare_2_To_1);
return $Difference_Array;
}
print_r(ArrayDiff($Array_1,$Array_2));
Which will output:
Array
(
[0] => 6
)
Putting this into an if statement:
$Differences = ArrayDiff($Array_2,$Array_1);
if (count($Differences) > 0){
echo 'There Are Differences Between The Array:';
foreach ($Differences AS $Different){
echo "<br>".$Different;
}
All the examples and code is based off the arrays at the start ($Array_1 and $Array_2)
$po_line_array=array();
$po_line_clone_array=array();
foreach($cart->line_items as $line_no => $po_line)
$po_line_array[$line_no]=$po_line->labdip_details_id;
print_r($po_line_array,1);
foreach($cart->line_items_clone as $line_no_clone => $po_line_clone)
$po_line_clone_array[$line_no_clone]=$po_line_clone->labdip_details_id;
print_r($po_line_clone_array,1);
$result=array_diff($po_line_clone_array,$po_line_array);
print_r($result,1);
Output:
Array ( [0] => 101 )
Array ( [0] => 101 [1] => 103 )
Array ( [1] => 103 )