Comparing like elements in arrays and echoing out rest of the arrays - php

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.

Related

arrays. getting the 3rd item of this multi array

Please help. I want to print the price. I need to print the price only. Help me. Thanks
$x=array(array('itm_name'=>'Optimization','price'=>'100','desc'=>'Chuchu'));
It seems you are using a 2 dimensional array
have you tried
echo $x[0]['price']
note '0' is the first element of the 1st layer of the array
so this can be substituted with 'x' so can iterate through a list
additionally if you want to get the other items of the array then
echo $x[0]['itm_name']
i hope you have grasped the basic concept of multi-dimensional arrays
using this simple explanation
Use this:
$x[0]['price']
$x[0] means the first element of the outer array, and ['price'] gets the price element of the inner array.

alphanumeric sorting with php and preg_match

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;
}

How to show all values of an array in Drupal 7

I'm working on a Drupal 7 site with an event listing. I've added a date field so the user can specify the date or dates for the event. Now I'm trying to get the date(s) to show in the template. I've tried using this:
<?php print $node->field_event_date['und'][0]['value']; ?>
That works ok, but it only shows one event from the array. I could just repeat that line 10 times and replace the array item number in each one, but I figure there has to be a way to show all the items in an array, whether it's one or ten. Can this be done with PHP or do I need to make a View?
In PHP terms you're probably looking for a foreach loop, to iterate over the array and print the values.
In Drupal terms, you'll want to use that with the field_get_items() function:
$items = field_get_items('node', $node, 'field_event_date');
foreach ($items as $item) {
print $item['value'];
}
For bonus fun, check out EntityMetadataWrappers

Grabbing specific values from a multi-dimensional array in PHP

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);

Using foreach to create grid

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);

Categories