I have an array in PHP as below:
`
Array (
[0] => Array
(
[0] => Some text
[1] => 6230.C3
)
[1] => Array
(
[0] => Some text
[1] => 6230.C3
)
[2] => Array
(
[0] =>
[1] =>
)
[3] => Array
(
[0] => Some text
[1] =>
)
[4] => Array
(
[0] => Some text
[1] =>
)
[5] => Array
(
[0] => Some text
[1] =>
)
[6] => Array
(
[0] =>
[1] =>
)
[7] => Array
(
[0] => Some text
[1] =>
)
[8] => Array
(
[0] => Some text
[1] =>
)
)
`
Array[2] and Array[6] are empty arrays. Please help me split my array to three arrays array1, array2, array3 as below (split at position of Array[2] and Array[6]).
array1 = [ Array[0], Array[1] ]
array2 = [ Array[3], Array[4], Array[5] ]
Aarray3 = [ Array[7], Array[8] ]
Thank you for your help.
This is a little more flexible than your original question because it is not limited to exactly 3 arrays as separate variables. The result of the following would be an multidimensional array, split in the way that you want but for an unlimited number of splits. I can rework it if necessary to fit exactly 3 variables if that is really what your input will always be.
<?php
$final = array();
$counter = 0;
$outerEmpty = false;
foreach($originalArray as $outer) {
$innerEmpty = true;
foreach($outer as $inner) {
if (!empty($inner)) {
$innerEmpty = false;
break;
}
}
if (!$innerEmpty)
$final[$counter][] = $outer;
else
$counter++;
}
Related
I have two array two array. First is multidimensional and other is single dimensional. I want to find difference between them. How do I found.
$arrayresult
Array 1
Array (
[0] => Array ( [0] => ishani.lad [1] => 9033187384 )
[1] => Array ( [0] => rajkumar.prajapati [1] => 8460078459 )
[2] => Array ( [0] => lokesh.bhandari [1] => 9687060900 )
[3] => Array ( [0] => shishanshu.rai [1] => 8401915337 )
[4] => Array ( [0] => vishal.dake [1] => 9879815299 )
[5] => Array ( [0] => mohsin [1] => 8347163123 )
)
$useduser
Array 2
Array (
[0] => ishani.lad
[1] => rajkumar.prajapati
[2] => lokesh.bhandari
)
I need difference as result as below
Result
Array (
[0] => Array ( [0] => shishanshu.rai [1] => 8401915337 )
[1] => Array ( [0] => vishal.dake [1] => 9879815299 )
[2] => Array ( [0] => mohsin [1] => 8347163123 )
)
I have used solution as
$resultremainig = [];
foreach($arrayresult as $val2){
if(!in_array($val2[0], $useduser)){
echo $val2[0]."<br>";
$resultremainig[] = $val2;
}
}
But it show last record also. Result of above code is as below. It always show me last record in second array's also
Array (
[0] => Array ( [0] => lokesh.bhandari [1] => 9687060900 )
[1] => Array ( [0] => shishanshu.rai [1] => 8401915337 )
[2] => Array ( [0] => vishal.dake [1] => 9879815299 )
[3] => Array ( [0] => mohsin [1] => 8347163123 )
)
If you wanted you could try using nested loops like so:
<?php
$arrOne = $arrFinal = [
["ishani.lad", 9033187384],
["rajkumar.prajapati", 8460078459],
["lokesh.bhandari" , 9687060900],
["shishanshu.rai" , 8401915337],
["vishal.dake" , 9879815299],
["mohsin" , 8347163123],
];
$arrTwo = [
"ishani.lad",
"rajkumar.prajapati",
"lokesh.bhandari",
];
foreach($arrOne as $key=>$item){
foreach($arrTwo as $k=>$v){
if(in_array($v, $item)){
unset($arrFinal[$key]);
}
}
}
var_dump($arrFinal);
// PRODUCES:::
array (size=3)
3 =>
array (size=2)
0 => string 'shishanshu.rai' (length=14)
1 => int 8401915337
4 =>
array (size=2)
0 => string 'vishal.dake' (length=11)
1 => int 9879815299
5 =>
array (size=2)
0 => string 'mohsin' (length=6)
1 => int 8347163123
You could use array_filter():
$output = array_filter($arrayresult, function($a) use ($useduser) {
return !in_array($a[0], $useduser);
});
Hi You can also try this
$one = array(array('ishani.lad',9033187384),array('rajkumar.prajapati',8460078459),array('lokesh.bhandari',9687060900),array('shishanshu.rai',8401915337),array('vishal.dake',9879815299),array('mohsin',8347163123));
$two = array('ishani.lad','rajkumar.prajapati','lokesh.bhandari');
foreach($one as $array){
if(!in_array($array[0],$two)){
$final[] = $array;
}
}
echo "<pre>";print_r($final);
Output
Array
(
[0] => Array
(
[0] => shishanshu.rai
[1] => 8401915337
)
[1] => Array
(
[0] => vishal.dake
[1] => 9879815299
)
[2] => Array
(
[0] => mohsin
[1] => 8347163123
)
)
Trim the value before checking in $useduser array
$resultremainig = [];
foreach($arrayresult as $val2){
// this removes any extra spaces from the search string
if(!in_array(trim($val2[0]), $useduser)){
echo $val2[0]."<br>";
$resultremainig[] = $val2;
}
You need to use the array_diff function.
Store your 2 arrays in variables and compare them.
suppose I have an array like this :
Array
(
[0] => Array
(
[0] => A
[1] => 20
)
[1] => Array
(
[0] => B
[1] => 10
)
[2] => Array
(
[0] => G
[1] => 5
)
[3] => Array
(
[0] => A
[1] => 15
)
)
I would like to remove duplicate values and sum just a row of array :
What I want :
Array
(
[0] => Array
(
[0] => A
[1] => 35 // <= sum : 20 + 15
)
[1] => Array
(
[0] => B
[1] => 10
)
[2] => Array
(
[0] => G
[1] => 5
)
)
I've read this question before.
updated
while($row = $stmt->fetch()){
$arr = array(
'GoodMainCode'=>persian_sql_to_php($row['GoodMainCode']), // <= like A in the example
'title'=> persian_sql_to_php($row['GoodName']),
'author'=>persian_sql_to_php($row['moalef']),
'publisher'=>persian_sql_to_php($row['Nasher']),
'translator'=>persian_sql_to_php($row['Motarjem']),
'price'=>persian_sql_to_php($row['SellPrice1']),
'isbn'=>persian_sql_to_php($row['ISBN']),
'amount'=>persian_sql_to_php($row['Amount']), // <= if GoodMainCode is same key, I must sum it.
'year_of_publish'=>persian_sql_to_php($row['SaleChap']),
'period_print'=>persian_sql_to_php($row['NobateChap'])
);
array_push($mjson,$arr);
}
//added
foreach($mjson as $v){
if(!isset($result[$v['GoodMainCode']]))
$result[$v['GoodMainCode']] = $v;
else
$result[$v['GoodMainCode']]['amount'] += $v['amount'];
}
This should work for you:
Just loop through your array and check if in your $result array is a key with the letter of the current inner Array from $arr. If not add it to the $result array and initialize the second key with the number.
If there is already a key with this letter you can simply add the numbers together in this array. At the end I simply use array_values() to reindex the entire array.
<?php
foreach($arr as $v) {
if(!isset($result[$v[0]]))
$result[$v[0]] = $v;
else
$result[$v[0]][1] += $v[1];
}
$result = array_values($result);
print_r($result);
?>
output:
Array
(
[0] => Array
(
[0] => A
[1] => 35
)
//...
[2] => Array
(
[0] => G
[1] => 5
)
)
This is the array I have:
Array
(
[0] => name:string:255
[1] => weight:integer
[2] => description:string:255
[3] => age:integer
)
I want it to look like this
NewArray
(
[0] => Array
(
[0] => name
[1] => string
[2] => 255
[1] => Array
(
[0] => weight
[1] => integer
[2] => Array
(
[0] => description
[1] => string
[2] => 255
[3] => Array
(
[0] => age
[1] => integer
)
Or better yet, I would prefer this result. Making two separate arrays based off the number of groups.
NewArray1
(
[0] => Array
(
[0] => name
[1] => string
[2] => 255
[1] => Array
(
[0] => description
[1] => string
[2] => 255
)
NewArray2
(
[0] => Array
(
[0] => weight
[1] => integer
[1] => Array
(
[0] => age
[1] => integer
)
I have tried exploding and implode and foreaching but I'm not quite getting the result I want.
Use array_reduce() to build a new array while iterating over the old, splitting it up by type:
$array = [
'name:string:255',
'weight:integer',
'description:string:255',
'age:integer',
];
$result = array_reduce($array, function(&$result, $item) {
$parts = explode(':', $item, 3);
$result[$parts[1]][] = $parts;
return $result;
}, []);
Try this:
<?php
$array = array("name:string:255", "weight:integer", "description:string:255", "age:integer");
function map_func($value) {
return explode(':', $value);
}
$newArray = array_map(map_func, $array);
echo "Output 1:\n";
print_r($newArray);
$sorted = array();
foreach($newArray as $el)
$sorted[$el[1]][] = $el;
echo "Output 2:\n";
print_r($sorted);
Output:
Output 1:
Array
(
[0] => Array
(
[0] => name
[1] => string
[2] => 255
)
[1] => Array
(
[0] => weight
[1] => integer
)
[2] => Array
(
[0] => description
[1] => string
[2] => 255
)
[3] => Array
(
[0] => age
[1] => integer
)
)
Output 2:
Array
(
[string] => Array
(
[0] => Array
(
[0] => name
[1] => string
[2] => 255
)
[1] => Array
(
[0] => description
[1] => string
[2] => 255
)
)
[integer] => Array
(
[0] => Array
(
[0] => weight
[1] => integer
)
[1] => Array
(
[0] => age
[1] => integer
)
)
)
I doubt the following is the best solution but it does return what you wanted.
$array = array(
"0" => "name:string:255",
"1" => "weight:integer",
"2" => "description:string:255",
"3" => "age:integer"
);
foreach ($array as $key => $val) {
foreach (explode(':', $val) as $part) {
$new_array[$key][] = $part;
}
}
print_r($new_array);
above returns the following.
Array
(
[0] => Array
(
[0] => name
[1] => string
[2] => 255
)
[1] => Array
(
[0] => weight
[1] => integer
)
[2] => Array
(
[0] => description
[1] => string
[2] => 255
)
[3] => Array
(
[0] => age
[1] => integer
)
)
So you have a 1-dimentional array of strings ($arr01) . strings are separated by :
and you need to have a 2-dimentional array ($arr02) where the second dimension is an array of strings composed by splitting the initial set of strings based on their separator character :
$arr01 = array("name:string:255", "weight:integer", "description:string:255", "age:integer");
$arr02 = array(array());
for($i=0; $i<sizeof($arr01); $i++) {
$arr02[$i] = explode(":",$arr01[$i]);
}
display the two arrays....
echo "array 01: <br>";
for($i=0; $i<sizeof($arr01); $i++) {
echo "[".$i."] ".$arr01[$i]."<br>";
}
echo "<br><br>";
echo "array 02: <br>";
for($i=0; $i<sizeof($arr01); $i++) {
echo "[".$i."] ==> <br>";
for($j=0; $j<sizeof($arr02[$i]); $j++) {
echo " [".$j."] ".$arr02[$i][$j]." <br>";
}
}
echo "<br><br>";
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
)
)
I am trying to write some php code to process the second dimension's value of an array based on similar values of the first dimension values.
Following is the sample output.
[0] => Array (
[0] => 1
[1] => 0.091238491238491
)
[1] => Array (
[0] => 2
[1] => 0.2221793635487
)
[2] => Array (
[0] => 2
[1] => 0.10662717512033
)
[3] => Array (
[0] => 4
[1] => 0.44354338998346
)
[4] => Array (
[0] => 6
[1] => 0.2248243559719
)
[5] => Array (
[0] => 6
[1] => 0.31764705882353
)
[6] => Array (
[0] => 6
[1] => 0.15764625384879
)
[7] => Array (
[0] => 6
[1] => 0.19160083160083
)
[8] => Array (
[0] => 12
[1] => 0.31054875069499
)
[9] => Array (
[0] => 12
[1] => 0.10915034227918
)
[10] => Array (
[0] => 15
[1] => 0.32915461266474
)
//...........goes to 46000 elements
Now what I want to do is, if the index 0 values of each array is similar then I want to add the index 1's value.
So for example, if 0 index values for 4 arrays are same , I want to add index 1 values of all 4 arrays.
If there is a unique value on 0th index, dont add it with anything, simply store index 1's value and move on.
Thanks very much.
Ghanshyam
$added = array();
foreach ($array as $item) {
if (isset($added[$item[0]])) {
$added[$item[0]] += $item[1];
} else {
$added[$item[0]] = $item[1];
}
}
$p=0;
$temp = $final_prod_ex[0][1];
for($x=0; $x<count($final_prod)-1; $x++){
if($final_prod_ex[$x][0]==$final_prod_ex[$x+1][0]){
$temp = $temp + $final_prod_ex[$x+1][1];
}
else{
$ans[$p] = $temp." ".$final_prod_ex[$x][0];
$temp = $final_prod_ex[$x+1][1];
$p++;
}
}
Finally figured it out after a lot of thinking(I'm new to programming)...Array's name is $final_prod_ex. Comment on this if I can make it better. And sorry #deceze. I could not understand your solution. I know you were trying to give the value of one array as an index to another. But what the scenario is, that value isnt like 0,1,2,3,4.... Its like 1,3,5,6,7,10. We are missing numbers in between. Maybe I didnt understand your solution. Correct me if I am wrong.
Thanks for all the help.