I wish to ask about operations over arrays in php.
I'm capturing two cells from one table in database, which are holding strings formatted like "1;3;6;" and whole second table - it has numbers in first column and names in second. Im dividing strings from first table into two arrays using explode() function, and second table into next two arrays with column per array. Therefore, when print_r() is used over those three arrays, I will see:
error_reporting(E_ALL); ini_set('display_errors', 1);
first table
"amount" string transformed into array called $ship_storage_parted_amount Array
(
[0] => 5
[1] => 10
[2] =>
)
"ware id" string transformed into array called $ship_cargo_bay_parted Array
(
[0] => 1
[1] => 2
[2] =>
)
second table transfomed into two arrays $ware_id and $ware_name
table contains 7 rows at all
Array
(
[0] => Array
(
[ware_id] => 1
[name] => Energized energy cell
)
[1] => Array
(
[ware_id] => 2
[name] => Depleted energy cell
)
[2] => Array
(
[ware_id] => 3
[name] => Vegetables
)
[3] => Array
(
[ware_id] => 4
[name] => Meat
)
)
I want to achieve next thing: showing at website appropriate equal names from second table according to their IDs with conjunction of what is stored in first table.
Therefore, it should look like:
Energized energy cell x5
Depleted energy cell x10
When I've tryied to show them like that:
echo $ware_name[$ship_cargo_bay-1]." x".$ship_storage_parted_amount[$ware_position];
it resulted in such output:
Energized energy cell x
where $ship_cargo_bay contains all captured string "ware id" and $ware_position was amount counter. Amount counter pointed at 3rd array element which is empty; but when I reorganized printing with for()
for($i=0;$i}lowerthan{$ware_counter;$i++)
{echo $ware_name[$ship_cargo_bay_parted[$i]]." x".$ship_storage_parted_amount[$i];}
I've got now
x5 x10 x x
And my poor knowledge ends here. As I know, first attempt of printing shall not work at all, but why I've got such output in second attempt is over my understanding.
Finally found reasons, where I made mistakes. After carefully checking names of each single variable, printing what exactly everything contains, I found that Ive somehow mispelled variable names.
for($i=1;$i<$ware_counter;$i++)
{
echo $ware_name[$ship_storage_parted[$i]]." x ".$ship_storage_parted_amount[$i]."<br/>";
}
This few lines gives exactly what Ive needed. Therefore Im putting it here - maybe someone will encounter similar problem.
Related
I searched many thread but i can't find this solution
I have this Array
Array
( [0] => [1] => Array ( [0] => 2019-01-11T23:30:00CET [1] => -12.6 ) [2] => [3] => Array ( [0] => 2019-01-11T23:20:00CET [1] => -12.5 ) [4] => [5] => Array ( [0] => 2019-01-11T23:10:00CET [1] => -12.6 ) [10] => [11] => Array ( [0] => 2019-01-11T22:40:00CET [1] => -12.4 )
I found the path to have the maximum or minimum value ( Column [1] ) from this Array but i need to find the relative Parent
(example the minimum -12.6 is in the [1][0] as 2019-01-11T22:20:00CET)
of this two values that are show in the first column ( Column[0] )
Thanks
If you use array_column() to extract the second column of your data, then you can use min() or max() with that array to pick which one you want. This code then extracts the ones that match using a foreach() and if to check if it matches (not exactly sure what you want as output, but this should help)...
$source = [["2019-01-11T23:30:00CET", -12.6],
["2019-01-11T23:20:00CET", -12.5],
["2019-01-11T23:10:00CET", -12.6]
];
$extract = min(array_column($source, 1)); // or use max()
$output = [];
foreach ($source as $key => $element) {
if ( $element[1] == $extract ) {
// Matches, so add to output
$output[$key] = $element[0];
}
}
print_r($output);
will give
Array
(
[0] => 2019-01-11T23:30:00CET
[2] => 2019-01-11T23:10:00CET
)
You could use array_filter() to extract the matching rows, but a foreach() is enough for a straightforward thing like this (IMHO).
If there is a possibility of blank values or strings in the value column, this may confuse the min() as it will consider the values and compare them as strings, to ensure they are all compared as numbers you can add...
$values = array_map("floatval", array_column($source, 1));
$extract = min($values); // or use max()
The array_map("floatval",... goes through the list and converts them all to float values.
Also, here's a generalized algorithm-sketch for "finding the max in some array", expressed as pseudo-code:
"Leave quietly" if the array is empty, or throw an exception.
Otherwise, assume that the first element in the array is the biggest one.
Now, loop through the remaining elements, testing if each one is, in fact, bigger than the "biggest one" that you have so far. If so, select it as the "biggest."
When the loop is finished, return your answer.
Now – this is what a geek would call "an O(n) algorithm," which is to say that its execution-time will be "on the order of" the number of elements in the array. Well, if this is a "one-off" requirement, that's fine. Whereas if what you actually want to do is to get "more than one" max-element, sorting the array (once, then holding on to the sorted result ...) becomes significantly better, because the sort is going to be O(log(n)) ... "on the order of some logarithm of the number of elements," ... and the subsequent cost of "popping off" elements from that sorted array becomes non-existent.
There are other ways to do it, of course – trees and such - but I've already blathered-on too long here.
I've looked at a number of suggestions for this, and they seem to rely on array_combine() which unfortunately is not suitable, as they arrays need to stay separate for other functions.
The arrays are set out as per
Array ( [0] => 3 [1] => 1 [2] => 3 )
Array ( [0] => 194 [1] => 0 [2] => 452 )
When I read in the first array, I get the key as $key and everything works fine.
Whenever I try and access the second array the whole script just whites out the page.
I want the code to work simliar to this ...
$a2value = $a2[$key] => $value;
echo $a2value;
Then I can do SQL lookups using $a2value
Any help would be appreciated
Here Try this
let's suppose two arrays of same number of elements
$a1=[1,2,3];
$a2=[194,0,452];
//A Simple foreach
foreach($a1 as $key=>$a1value){
$a2value=$a2[$key];
//Perform Query here
}
Remember one thing number of elements of both array should always be equal or it will cause an error
If you aren't certain about the size of both arrays then replace the line with this code
$a2value=empty($a2[$key]) ? null : $a2[$key];
Hope this works !!!
I am quite not able to get the logic for my requirement.
Lets consider I have an array
Array
(
[0] => 1
[1] => 1
[2] => 2
[3] => 1
[4] => 3
[5] => 3
)
I would like to split the below array on the basis if the array element matches its previous element.
i.e in the above example value at [1] matches the value at [0]. Hence put it into the same array as [0]. Now check if the value at [2] matches the value at [1] if it matches put it into the same array, if not put it into a different array. The process continues.
Below is an example of the desired outpout.
Array
(
[0] => 1
[1] => 1
)
Array
(
[0] => 2
)
Array
(
[0] => 1
)
Array
(
[0] => 3
[1] => 3
)
Thanks for your help in advance.
Justin
you can obtain that result in a loop checking on previous element. the output can be an array of arrays! (or anything you would prefer.. do your thing here)
$array1 = array(1, 1, 2, 1, 3, 3);
$output_array=array();
$previous_value="";
$output_array_index=0;
foreach ($array1 as $value) {
if($value != $previous_value){
$output_array_index+=1;
}
$output_array[$output_array_index][]=$value;
$previous_value=$value;
}
print_r($output_array);
so, let me know if you need more pointers! array logic is fun, and php will let you do alot, out of the box. though this specific need is not covered, have a look when you have a minute # the manual, it'll save you time in the future, guarantee http://php.net/manual/en/ref.array.php
This question is a bit confusing but doesn't sound too difficult to implement if I'm understanding it correctly. All you need to do is have a temporary array (or array list) that checks user input. If that user input happens to be the same as the previous input (you can keep a counter variable and check to see if ArrayList.get(counter) == ArrayList.get(counter-1)). Keep adding things to this temporary arrayList and once you have a number that is different, just iterate through the arraylist and add it to a new array.
Another question you have to consider is how you are going to store all these arrays. For that you may want to create an ArrayList containing Arrays. That way after you find user input that is different from the previous input you can just use the toArray method provided with the ArrayList class and add it to the ArrayList containing all of your separate Arrays!
Hope this helps!
I have tried for the last two days now searching on google and in all the forums, but can't seem to find any answer that remotely helps me with this problem .
I have a stock feed .csv file which I need to change the values of the shoe sizes to work with Woocommerce. The shoe sizes are different on each row.
The sizes in the csv are listed like this: 4-10, 5-12, 3-9 etc. one set of numbers per row 4-10. I have inputed the file into an array in my php script.
So for each shoe I have an array like this:
Array
(
[0] => 4578
[1] => kors
[2] => red
[3] => wedge
[4] => 4-10
)
I need to take the last value e.g. 4-10 and change them to something like this: 4|5|6|7|8|9|10.
So basically I need to take the first number in the element and increment it by 1 and separate it with the pipe character " | "until it reaches the value of the last number. Then I need it to replace the 4-10 in the element with the 4|5|6|7|8|9|10.
This should work for you:
(Here I first get the last element of the array and explode() it with - as delimiter. After this I simply create an array with range() where I use the $start and $end variable. At the end I simply save the element back by implode()'ing it.)
<?php
$arr = [4578, "kors", "red", "wedge", "4-10"];
list($start, $end) = explode("-", $arr[count($arr)-1]);
$arr[count($arr)-1] = implode("|", range($start, $end));
print_r($arr);
?>
output:
Array ( [0] => 4578 [1] => kors [2] => red [3] => wedge [4] => 4|5|6|7|8|9|10 )
i have some problem to understand array_multisort
See how it sorts when two values are the same:
$a1=array("Dog","Dog","Cat");
$a2=array("Pluto","Fido","Missy");
array_multisort($a1,$a2);
print_r($a1);
print_r($a2);
The output of the code above will be:
Array ( [0] => Cat [1] => Dog [2] => Dog )
Array ( [0] => Missy [1] => Fido [2] => Pluto )
let me know why Missy comes first, if you do by ascending it must be
Array ( [0] => Fido, [1] => Missy, [2] => Pluto )
for descending vise versa
also see this
With sorting parameters:
$a1=array("Dog","Dog","Cat");
$a2=array("Pluto","Fido","Missy");
array_multisort($a1,SORT_ASC,$a2,SORT_DESC);
print_r($a1);
print_r($a2);
The output of the code above will be:
Array ( [0] => Cat [1] => Dog [2] => Dog )
Array ( [0] => Missy [1] => Pluto [2] => Fido )
but Array ( [0] => Missy [1] => Pluto [2] => Fido ) not at SORT_DESC is some type of mixed up.
can some one explain me how the array_multisort is working, so that i can understand how it's working.
Well, you're sorting the arrays in a similar way to programs like Excel. Each array corresponds to a column.
First, all arrays are sorted by the first array given. If there are identical values, those affected are sorted by the second array given. If there are again equal values, the third array is used, etc.
Or in other words: The arrays are sorted using all arrays, but beginning on the right (if you assume it really sorts by all columns once).
For your particular example (the second one):
At first you want to sort in ascending order, so Cat will be first. Therefore the last array element will be moved to the first position in both arrays. The other two elements, Dog are equal. This causes the function to look at the next array. It's told to sort this array in descending order, so Pluto comes first. In this case this leads to the result that the elements aren't moved at all (as their order is correct already).
The entries in the second array corresponding to the identical entries in the first array.
If you look at the documentation and the first example, you'll notice that this is the expected behavior.
With two arguments, both arrays: the first array is sorted; the second array will have its corresponding values re-arranged and sorted if the corresponding values in first column tie. As for your example, think of it as you're doing a SQL ORDER BY Animal, Name:
Cat comes first
The two Dogs have a tie so Fido comes first because Fido < Pluto