I have an array with some values in different spots. I want to check if there is a value in the index then put it in a new array starting at 0 then put in index 1 then next value at index 2 and so on. I need to shorten it and move them all to the left really.
Array ( [0] => 53 [1] => [2] => 55 [3] => 76 [4] => [5] => [6] => [7] => )
The new array would be:
newArray ( [0] => 53 [1] =>55 [2] => 76)
Maybe something like this:
for ($i=0; $i < sizeof($questionWorth); $i++)
{
if($questionWorth[$i] has a value)
{
put it in new array starting at index zero
then increment the index of new array
}
}
To only get values that is not NULL or empty you could use array_filter() and array_values() like this:
$array = array(76, NULL, NULL, 56);
// remove empty values from array, notice that since no callback
// is supplied values that evaluates to false will be removed
$array = array_filter($array);
// since array_filter will preserve the array keys
// you can use array_values() to reindex the array numerically
$array = array_values($array);
// prints Array ( [0] => 76 [1] => 56 )
print_r($array);
You can use
array_filter($yourArray)
It will remove all empty values for you
Try array_filter which makes exactly this
var_dump(array_filter(array(0 => 55, 1 => 60, 2 => null)))
If you want to check if an index has a value, do this :
$variable = array ([0] => 53, [1] => , [2] => 55, [3] => 76, [4] => , [5] => , [6] => , [7] => )
foreach ($variable as $key => $value) {
var_dump($key.' => '.$value);
}
It's as simple as this: if ( $array [$i] ), and then put the value in another array with another counter that starts from 0.
$array = array(76, NULL, NULL, 56);
$count = 0;
for ($i=0; $i < sizeof($array); $i++)
{
if($array[$i])
{
$arr[$count] = $array[$i];
$count++;
}
};
print_r($array);
print_r($arr);
Related
I have an array of 83 arrays (an array that I have a chunk in 83). I'm trying to keep only the three highest values of each array. All the numbers in each array are included between -1 and 1. There is necessarily a 1 in each array that I don't want to count in my three highest values.
Array
(
[0] => Array
(
[1] => 0.5278533158407
[2] => 0.4080014506744
[3] => 0.5086879008467
[5] => 0.3950042642736
[6] => 1
[1] => Array
(
[1] => 1
[2] => 0.52873390443395
[3] => 0.52518076782133
[4] => 0.52983621494599
[5] => 0.54392829322042
[6] => 0.53636363636364
Etc...
I'm trying the below code but it doesn't work.
for ($i = 0; $i < sizeof($list_chunk); $i++) {
arsort($list_chunk[$i]);
}
for ($i = 0; $i < sizeof($list_chunk); $i++) {
array_slice($list_chunk[$i],1,3,true);
}
print("<pre>");
print_r($list_chunk);
print("</pre>");
Someone could help me? Thanks a lot
This solution uses a foreach loop with a reference to the subarray. The subarray is sorted in descending order of size. The first to third elements are extracted. If the first element is 1, then 3 elements are extracted from the 2 element onwards.
foreach($array as &$arr){
rsort($arr);
$start = $arr[0] == 1 ? 1 : 0;
$arr = array_slice($arr,$start,3);
}
Result:
array (
0 =>
array (
0 => 0.5278533158407,
1 => 0.5086879008467,
2 => 0.4080014506744,
),
1 =>
array (
0 => 0.54392829322042,
1 => 0.53636363636364,
2 => 0.52983621494599,
),
)
Full sample to try: https://3v4l.org/pUhic
How to move values from 2nd array into the empty places of 1st array
1st array as below
Array
(
[0] => 1
[1] =>
[2] => 4
[3] =>
)
2nd array as below
Array
(
[0] => 5
[1] => 9
)
I want output as merging 2nd array into 1st as shown below
Array
(
[0] => 1
[1] => 5
[2] => 4
[3] => 9
)
I have tried below code.....
for($i=0; $i<$count; $i++){
for($j=$i; $j<=$i; $j++)
if(empty($assign_taskk[$i])){
$assign_taskk[$i] = $taskkk[$i];
}
}
plz help me out for same
Lets say your arrays look like:
$a1 = [
0 => 1,
1 => null,
2 => 4,
3 => null,
];
$a2 = [
0 => null,
1 => 5,
2 => null,
3 => 9,
];
Then you can iterate over first array and add values from the second one when needed:
foreach ($a1 as $k => $v) {
if (empty($v) && !empty($a2[$k])) {
$a1[$k] = $a2[$k];
}
}
Another way to do it using below way-
<?php
$arr1= [1,null,4,null];
$arr2 = [null,5,null,9];
$result = array_values(array_filter($arr1) + array_filter($arr2));
print_r($result)
?>
DEMO: https://3v4l.org/R4aeE
Hi #amod try this
$_newArray = array_values(array_filter($array1) + array_filter($array2));
print_r($_newArray);
You can use below code for this:
$firstArray = [1,'',4,''];
$secondArray = [5,9];
$secondArrayCounter = 0;
foreach($firstArray as $key => $value) {
if (empty($value)) {
$firstArray[$key] = $secondArray[$secondArrayCounter];
$secondArrayCounter++;
}
}
print_r($firstArray);
Hope it helps you.
I have two arrays $rate_row and $totalrowcost I am trying to update a row in the second array $totalrowcost based on the key in the first array $rate_row both arrays are always the same length.
I combine both arrays using the array_combine function
my code below works if there are no duplicate keys in the array but if i combine them when there are duplicate keys
I don't get the desired result.
Example:
If the key in $rate_row is 0 I need to set the value in $totalrowcost to 0 but when combining the array, one of the rows gets dropped and only one row with a key of 0 is preserved.
I've since learned since trying to figure this out that PHP will not allow an array to have a duplicate key, but i was wondering if there is some type of work around or hack that can help any ideas would be great thanks.
enter code here
<?php
// Finds the lowest duty rate
$lowest = 0;
$row_combine = array_combine($rate_row, $totalrowcost);
foreach($row_combine as $a => $combine_row):
if ($a = 0){
$lowest = $a;
echo "";
}
endforeach;
// Finds the row value associated with the lowest duty rate
foreach($row_combine as $a => $combine_row):
if ($a === $lowest){
$lowest1 = $combine_row;
}
endforeach;
for($i = 0; $i < count($totalrowcost); $i++) {
enter code here
if ($totalrowcost[$i] == $lowest1){
$totalrowcost[$i] = 0;
}
}?>
enter code here
$rate_row
Array
(
[0] => 35
[1] => 0
[2] => 40
[3] => 0
[4] => 45
)
$totalrowcost
Array (
[0] => 100
[1] => 49.99
[2] => 102
[3] => 98
[4] => 125
)
$row_combine Output
Array (
[35] => 100
[0] => 98
[40] => 102
[45] => 125
)
Desired $row_combine Output
Array (
[35] => 100
[0] => 49.99
[40] => 102
[0] => 98
[45] => 125
)
Actual Output updated
$totalrowcost
Array (
[0] => 100
[1] => 49.99
[2] => 102
[3] => 0
[4] => 125
)
Desired Output updated
$totalrowcost
Array (
[0] => 100
[1] => 0
[2] => 102
[3] => 0
[4] => 125
)
documentation for class to read up on the syntax and more information about them.
I'm a little unclear as to what problem you are trying to solve, perhaps an edit on what the use case is might help suggest other options.
EDIT:
<?php
// you can use whatever input $rate_row is from user input or such along with the $totalrowcost
$rate_row = [35, 0, 40, 0, 45];
$totalrowcost = [100, 49.99, 102, 98, 125];
class Table {
public $row_items;
public $row_rates;
public $row_rate_applied;
private $lowestRate;
// used to init class values
function __construct($items, $rates) {
// set variables to class
// might want to run validation here to make sure the count is = for $items and $rates
$this->row_items = $items;
$this->row_rates = $rates;
$this->lowestRate = min($rates); // add validation to make sure rates is array http://php.net/manual/en/function.min.php
// main guts that are used to solve your problem is inside this loop
for($i = 0; $i < count($items); $i++) {
// here we test if rates index == lowest. if it is set the item value to lowest
if ($rates[$i] == $this->lowestRate) {
$this->row_rate_applied[$i] = $this->lowestRate;
} else {
$this->row_rate_applied[$i] = $items[$i];
}
}
}
}
$table = new Table($totalrowcost, $rate_row);
echo var_dump($table->row_rate_applied);
The above code builds up and applies the rates against the items. I'm not 100% what the expected result is actually supposed to be because the code originally posted doesn't make a ton of sense for the expected output
essentially the idea is to build your Row
If the keys have no significant value (i.e. you just want to later loop over the final array) you can make sure the keys are unique before you merge them by appending a random name at the end of them.
Here is a function that merges two arrays while keeping the duplicate keys.
function array_uniqify_keys($array,$append='',$glue='--'){
if($append === ''){
$append = uniqid() . mt_rand(1000,9999);
}
foreach($array as $key => $value){
unset($array[$key]);
$array[$key.$glue.$append] = $value;
}
return $array;
}
function array_merge_duplicate_safe($array1,$array2,$a1_name='',$a2_name=''){
$array1 = array_uniqify_keys($array1,$a1_name);
$array2 = array_uniqify_keys($array2,$a2_name);
return array_merge($array1,$array2);
}
$array1 = [
1 => 'foo',
2 => 'boo'
];
$array2 = [
1 => 'good',
2 => 'bad',
3 => 'ugly'
];
print_r(array_merge_duplicate_safe($array1,$array2));
The result for the above code is:
Array
(
[1--59d393950489c8380] => foo
[2--59d393950489c8380] => boo
[1--59d39395049197021] => good
[2--59d39395049197021] => bad
[3--59d39395049197021] => ugly
)
You can also get the old key by explode('--',$key)[0].
I have 2 arrays that I need to combine or merge together. I am at a bit of a loss as to how to achieve this.
So the first array looks like this:
$arr1 =
Array (
[0] => Array
(
[id] => 7
[round] => 1
)
[1] => Array
(
[id] => 11
[round] => 2
)
....
And the second array looks like this:
$arr2 =
Array (
[round_1] => 21
[round_2] => 32
....
And I need the result to end up like this:
$result =
Array (
[0] => Array
(
[id] => 7
[round] => 1
[disp] => 21
)
[1] => Array
(
[id] => 11
[round] => 2
[disp] => 32
)
...... etc etc
Any ideas on where to get started for this??
Thanks
$i = 1; // Counter
$result = $arr1; // Copy $arr1 to preserve it. (if necessary)
foreach ($result as $x){
$x['disp'] = $arr2['round_' . $i];
$i ++;
}
This will iterate through Array1 setting a value for 'disp' in each sub array. This value will be pulled from Array2 and the round number would be incremented by a basic counter.
You could equally use a standard for loop for this too.
$result = $arr1; // Copy $arr1 to preserve it. (if necessary)
for($i = 0; $i < count($result); $i++){
$result[$i]['disp'] = $arr2['round_' . ($i + 1)];
}
Just make sure you do "$i + 1" when calling the value from Array2 because that doesn't seem to be 0 indexed.
I have an array data that look like this :
Array (
[0] => Array (
[0] => Name:
[1] => John W.
[2] => Registration ID:
[3] => 36
)
[1] => Array (
[0] =>Age:
[1] => 35
[2] => Height:
[3] => 5'11"
)
[3] => Array (
[0] => Sex:
[1] => M
[2] => Weight:
[3] => 200lbs
)
[4] => Array (
[0] => Address
)
[5] => Array (
[0] => 6824 crestwood dr delphi, IN 46923
))
And I want to convert it to associative array like this :
Array(
['Name']=> John W.
['Registration ID']=> 36
['Age']=> 35
['Height'] => 5'11''
['Sex']=>M
['Weight']=>200lbs
['Address']=>6824 crestwood dr delphi, IN 46923
)
I have no idea at all how to do this, since the supposed to be array column header were also in sequence, so it makes difficult to convert this array.
Any help I appreciate, thx.
Given your origin array is called $origin , you can do it like this:
$merged = array();
foreach($origin as $val) {
$merged = array_merge($merged, $val);
}
$tot = count($merged) - 1;
for ($i=0;$i<$tot;$i+=2) {
$result[$merged[$i]] = $merged[$i+1];
}
var_dump($result); // To test the resulting array
Firstly, I use array_merge() to flatten the $origin array to only one dimension/depth, so we later iterate it (stepping each 2 items per iteration) and assigning each pair of items ($i and $i+1) to the resulting array.
Looks like, for the first 3 children, you can just assign the even value to the previous element as key. Then, assign the fourth one as key for fifth element.
$result = array();
foreach ($array as $key => $value)
{
if ($key < 4) {
$elements = array_values($value);
$result[$elements[0]] = $elements[1];
$result[$elements[2]] = $elements[3];
}
if ($key == 4)
$fifthkey = $value;
if ($key == 5)
$result[$fifthkey] = $value;
}
Also, note that you have to escape your height string quotes.