I have an array called $itemArray[] that contains a set of strings, such as:
myItem_1,
myItem_2,
myItem_3,
myItem_4,
anotherName_1,
anotherName_2,
anotherName_3,
anotherName_4
I also have the following code that goes through the array and filters out the ones that contain 'myItem'.
The loop works, however, it returns results in varied order. Is there a way to sort the result of the array so
that it lists them alphanumerically?
foreach($itemArray as $item) {
if (preg_match('/myItem/',$item))
echo '$item';
}
I don't have much experience with php and would definitely appreciate the help! Thank you!
PHP supports lots of ways to sort arrays, see how they work here. You could run it as asort($itemArray).
The right way to solve this is by sorting the array prior to outputting it. Something along the lines of:
asort($itemArray);
foreach($itemArray as $item) {
if (preg_match('/myItem/',$item))
array[index++] = $item;
}
Related
I have created multiple arrays from text documents that are uploaded monthly. One of the Arrays item numbers, description and picture. the other array is item numbers, price and quantity.
What I am trying to do is if the item number is the same then be able to Echo out all the information that I need. Example would be
if($array1[0] ==$ array2[4]){
echo $array1{4];
echo $array2[6];
}
this doesn't work and having issues with getting array_intersect to work with it also.
I don't really understand how there are so many variable inside an array. Item's number, description, and picture inside an array? I assuming you are looking for matching item's number inside both arrays so..
Good luck trying:
foreach($array1 as $a){
foreach($array2 as $b){
if($a==$b){
echo $a;
}
}
}
Let me know how it works for you soon.
this didnt work for what I was trying to do but I did figure it out. I needed to use strpos() in a if statement to get the items I was looking for.
I'm new to programming and I'm tackling arrays. I thought I had multi-dimensional arrays figured out but I guess I don't. Here's the code I'm working on:
$loopcounter = 0;
while ($myrow = mysql_fetch_array($results)) {
//...other stuff happens...
$allminmax[$loopcounter][] = array("$myrow[3]","$currentcoltype","$tempmin","$tempmax");
$loopcounter++;
}
This chunk of code is supposed to create an array of four values ($myrow[3], currentcoltype, tempmin, tempmax) and insert it into another array every time the loop goes through. When I do this:
echo implode($allminmax);
I get:
ArrayArrayArrayArrayArrayArrayArrayArrayArray
Do I need to implode each array before it goes into the master array? I really want to be able to do something like $allminmax[0][4] and get the $tempmax for the first row. When I try this now nothing happens. Thanks -- any help is appreciated!
It looks like you should either use [$loopcounter] or [], but not both. You also should drop the quotes. They're unnecessary and in the case of "$myrow[3]" they interfere with the variable interpolation.
$allminmax[] = array($myrow[3], $currentcoltype, $tempmin, $tempmax);
By the way, arrays are zero-indexed, so to get $tempmax for the first row it'd be $allminmax[0][3] not $allminmax[0][4].
Also, a better way to display the contents of your array is with print_r or var_dump. These will display arrays within arrays while a simple echo will not.
var_dump($allminmax);
I'm trying to compare two entries in a database, so when a user makes a change, I can fetch both database entries and compare them to see what the user changed, so I would have an output similar to:
User changed $fieldName from $originalValue to $newValue
I've looked into this and came across array_diff but it doesn't give me the output format I need.
Before I go ahead and write a function that does this, and returns it as a nice $mtextFormatDifferenceString, can anyone point me in the direction of a solution that already does this?
I don't want to re-invent the wheel..
Since you require "from $originalValue to $newValue", I would go ahead and select the two rows, put them in assoc arrays, then foreach through the keys, saving the ones that aren't equal. Kind of like:
$fields = array_keys($row1);
$changedFields = array();
foreach ($fields as $field) {
if ($row1[$field] != $row2[$field]) {
$changedFields[] = $field;
}
}
I realize you were asking about the existence of pre-built wheels but I felt the solution was pretty simple.
?>
Although you didn't define what format you needed, but well-known diff algorithm is probably for you. Google for PHP diff algorithm and you'll find some suggestions I am sure.
You could get the changed values ($newValue) with array_diff_assoc and then just use the keys ($fieldName) to find the original value $originalValue and output it in anyformat you want
I have tried to do this:
<?=implode(array('A','B','C'));
To display an array but was wondering if there was an easier way of showing an array?
I tried
<?=print_r(array('A','B','C'));
But it actually displays an array structure. I want the string to be like ABC.
No, there is no easier way than this implode.
Normally while debugging array printing is done using implode() and print_r(). If you want to display the arrays in your different way, just create your own function for that.
I have seen in many examples var_dump(...).
link text
Yeah, sure... If you always want an array to display, say, with each element on a new line, you can just write your own function (with a really short name... if you are just that lazy... [not recommended...])!
<?php
// To Call Function:
$array = array(2,3,4,5,'awesome!');
ez($array);
/* echoes:
2
3
4
5
awesome!
*/
// Poorly named function...
function ez($array = array()) {
if(!$array || empty($array)) return;
$output = implode('\n',$array);
echo "<pre>{$output}</pre>";
}
?>
Sorry, this is kind of a smartass answer.
I am creating a grid in GD and was wondering if this is the right way to do it.
I have 2 arrays. One contains all X values, the other contains all Y values.
foreach ($xpointsArray as $xvalue) {
foreach ($ypointsArray as $yvalue) {
// Draw point at coordinates $xvalue, $yvalue
}
}
I just think there must be a more elegant way to set this up, and I would like to further
access the points values without doing this every time.
What you're doing is correct. There's possibly some more elegant solutions using a single two dimensional array, but either way you have to iterate through two loops.
Additionally, using a two dimensional array you could reference specific points by $Array[$x][$y] to get a specific point.
foreach, while, for are nice and I personally would prefer them, but since you are asking, what about using array_walk()?
array_walk($array1, "print_sudoku_field", $array2);