Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
  Advanced Search
  HOME    TRACKER    PHP


SuperbHosting.net have generously sponsored dedicated servers to ensure a reliable and scalable dedicated hosting solution for BigResource.com.





Find Item In Array


I have the following array

$array = array('k1' => 'v1','k2' => 'v2','k3' => 'v3','k4' => 'v4');

how do i select the value that is provided BEFORE the key i provide

ex:
$mykey = "k2";
$array = array('k1' => 'v1','k2' => 'v2','k3' => 'v3','k4' => 'v4');
$value = somefunctions(); // gives the value v1

ex:
$mykey = "k4";
$array = array('k1' => 'v1','k2' => 'v2','k3' => 'v3','k4' => 'v4');
$value = somefunctions(); // gives the value v3.




View Complete Forum Thread with Replies

Related Forum Messages:
Write A Conditional Statement To See If The Current Item In An Array Is The Last Item.
This seems like it would be so simple, but I haven't been able to solve it. I want to write a conditional statement to see if the current item in an array is the last item. I tried

if (count($array) == (current($array))

but that didn't work. count($array) will give me the number of items in the array. What do I use for the current position in the array.

View Replies !
Array_unique - How-to Get First Item Value In Repeated Array Item
I would like to know how-to get the first value of repeated items too in an array using something like array_unique. Code:

View Replies !
Array Explode Problem Result In A 6 Item Array
I have the following:

$BB_Basket = "basket=¦APL6007,2.00¦AVX8900,1.00¦ARCAV500,1.00";
$basket_containers = explode('¦', $BB_Basket);
unset($basket_containers[0]);
print_r($basket_containers);

This results in

Array ( [1] => APL6007,2.00 [2] => AVX8900,1.00 [3] => ARCAV500,1.00 )

Now when I try to explode further with

$basket_items = explode(',', $basket_containers);
print_r($basket_items);

This results in

Array ( [0] => Array )

This should result in a 6 item array. Any ideas what I'm doing wrong?

View Replies !
Item In Array
I have an array, set up like this:

$this->navigation_array['Home'] = array('text' => 'Home', 'act' => 'Home', 'redirect' => 'No', 'active' => 'Home', 'link' => 'index.php?act=home', 'default' => 'Info');
$this->navigation_array['Forums'] = array('text' => 'Forums', 'act' => 'Forums', 'redirect' => 'No', 'active' => 'Forums', 'link' => 'index.php?act=forums', 'default' => 'Info');
$this->navigation_array['Users'] = array('text' => 'Users', 'act' => 'Users', 'redirect' => 'No', 'active' => 'Users', 'link' => 'index.php?act=users');
$this->navigation_array['Config'] = array('text' => 'Configuration', 'act' => 'Config', 'redirect' => 'No', 'active' => 'Config', 'link' => 'index.php?act=config');

I want to find the first item inserted into the array, namely "Home". What is the fastest way to just call the first entry? You cant use $this->navigation_array[0] in this case because the keys are text not numbers.

View Replies !
Deleting An Array Item
I have a standard array:
$foo = array(
0 => 'abc'
1 => 'def'
2 => 'ghi');

and I have a variable, $bar, which equals 'def' ... and I want to check
my array to see if the value of $bar exists in the array, and if it
does, delete it from the array.

if (in_array($bar, $foo)) {
// what goes here???
}

I've done this before, but for the life of me, I'm drawing a blank now.
Help a bruthah out?

View Replies !
Random Item Of An Array
What is the best way of picking out a random item of an array, regarding speed and CPU-usage ? I need a super fast way of picking out an ID from my MySQL db.

View Replies !
Delete Item From Array
How do I search for an item within an array, then delete it?

View Replies !
Remove An Item From And Array Entirely
How do you remove an item from an array...

View Replies !
Remove Item From Array
I'm having some trouble understanding how to remove items from an array. The array is created like this: Code:

View Replies !
Modify Array Item
I'm building a shopping cart and need to add the function so if an item is selected twice, the 'quantity' counter increments. I can't get the syntax right for modifying the array value. I have this:

foreach ($_SESSION['basket'] as $key => $row)
{
if ( $row['gnsz']==$gnsz)
{
$newqty=$row['qty'] ;
$newqty++;
$row['qty']= $newqty;
}
}

it picks up the first qty value but I can't seem to modify it in the array. I'm making a dumb mistake somewhere .

View Replies !
Put The Itemid Of An Item Into An Array.
$count = 1;
foreach($trade as $val) {
$val = checkinput($val);
$sel=mysql_query("SELECT * FROM `inventory` WHERE `ID` = '$val' AND `userid` = '$userid2'") or die(mysql_error());
$n=mysql_num_rows($sel);
if($n == 0) {
$input = "You do not own this item.";
error($input);
}
$g=mysql_fetch_array($sel);
$itemid = $g['itemid'];
$itemid[$count] = $itemid;
$count++;

}

Basically, my script is trying to put the itemid of an item into an array. The problem i'm getting is that, it's not saving the values in the array. For example, if i have 3 items and it puts them into the array, it will only save the 3rd item in the array $itemid[3] and 1 and 2 will be blank.

View Replies !
Current Item From An Array
i have an include file with many arrays that look like this: Code:

$event[] = array ('stuff' => 'bla');
$times[] = mktime(0,0,0,6,9,2007);

as of right now, I am using asort to sort all of these by date and display them as a list.

View Replies !
How To Remove An Item From An Array
Been looking around on how to remove an item from an array, i need the item totally removing, without a gab in the array, as i will be using the implode fuction afterwards.

View Replies !
Item In A Multidimensional Array
I have a multidimensional array (I think). It is an array that has arrays inside it. The inside arrays all have the same structure, (qty, product, color, size). How do I count how many items are 'White' * qty? I looked into the php manual and found array_count_values but others say it doesn't work with MD arrays.:confused:

array(
[0] array([0]qty, [1]product, [2] color, [3] size)
[1] array([0]qty, [1]product, [2] color, [3] size)
[2] array([0]qty, [1]product, [2] color, [3] size)
[3] array([0]qty, [1]product, [2] color, [3] size)
[4] array([0]qty, [1]product, [2] color, [3] size)
[5] array([0]qty, [1]product, [2] color, [3] size)
[6] array([0]qty, [1]product, [2] color, [3] size)
...)

View Replies !
Extra Item In My Array
Why is my code.

$urlarray = explode("/", "/about/new/index.php");
print_r($urlarray);

returning this

Array ( [0] => [1] => about [2] => new [3] => index.php ).

Perhaps I am being dense but why do I have an extra empty value at the beginning of the array. Is there any way I can make it return the ones I want (ie only those containing an actual value) or do I simply have to work around this.

View Replies !
Delete An Item From An Index Array?
How can I delete an Item from an index array in php4?

View Replies !
Shopping Cart Item Array
Trying to build a small simple shopping cart and I am having difficulty getting the solution. The cart is displaying each product, but it is overwriting the values. So if I have added 10 products, it shows ten items, but each item has the values of the last product I added.

I think that this may be due to me trying to use a key to store both an array and string at once, but Iam not sure. I have looked at other examples but due to different methods, syntax is throwing me off. Code:

View Replies !
Remove A Single Item From An Array
I have gathered an array of ID's. I need to go throught the array and find a specific ID and remove it from the array. Because we want to keep that ID. Any suggestion of how to do that.

View Replies !
Preg_split First Item In Array Is Blank
could someone please explain to my why using the code below generates a blank item in $array[0] please. The 'x' from xbox360 is in $array[1]?

View Replies !
Remove Item From Session Array
i have a simple script that adds an item id to a session Code:

View Replies !
Select One Row Of An Array Where Item Number Is ..?
When I call the Ebays XML API I get an array or something similar back (see the code sample), is it possible to "select item_id where item_id = 123123" or something similar as in MySQL? If, How can I do that? Also, I need to attach a PHP variables to the row I selected as for example $starttime, $endtime and so on.

Here is the code sample (the end of the scritpt);

PHP Code: ....

View Replies !
Retrieving Previous Array Item
I have an array. The array key is the id of the item (for example
categories[91], 91 is the ID of the category) all ID are not exactly in
date order, but they're added to the array in date order, how do I find
the previous (in the sense of when it was added) array entry of, for
example category[ྗ']?

View Replies !
Arrays : Returning A Reference To An Array Item ??
I am currently storing a set of objects inside an array,

$itemlist = array();
$itemlist[] = new item("myitem");
//...

and I am looking to develop a search function, which returns a
reference to the found item.

function &search_item($itemlist,"myitem") {
//...
}

I am trying to figure out how to obtain a reference to a given item in
an array.
Indeed, foreach() and each() seem to work on copies of the data in the
array.
For example:

$array = array("a","b","c","d");
print_r($array);
foreach($array as $v) $v="xxx";
print_r($array); // $array is unchanged

reset($array) ; while(list(,$v)=each($array)) $v="xxx";
print_r($array); // $array is unchanged, again

View Replies !
Deleting A Single Item From The Middle Of An Array?
I have an array that contain objects, I want to be able to test against the id of that object and remove it from the array. In it's simplest for I want to go from this:

$people = array('Tom', 'Dick', 'Harriet', 'Brenda', 'Jo');

...to this...

$people = array('Tom', 'Dick', 'Brenda', 'Jo');

The fact that the items in the array are objects should not matter right? Plus I've already written my loops and conditionals to check which item i need to remove.

function removeItem ( $item ) {
   $len = count ( $_SESSION['basket_arr'] ) ;
   if ( $len == 1 ) {
      emptyBasket ( ) ;
   } else {
      for ( $i = 0 ; $i < $len ; $i++ ) {
         if ( $item->id == $_SESSION['basket_arr'][$i]->id ) {
            //$_SESSION['basket_arr'][$i] must go
         } } }}


View Replies !
Removing Item From Array Then Overwriting File
I have a bookings.txt file which has a list of dates each on a new line, for example: -

21/06/07
22/06/07
29/06/07
25/12/07
01/01/08

I also have a variable $event_dd_mm_yy which I want to remove from this file if found. Code:

View Replies !
Find A Value In An Array
I want to find the smallest value in my array and then write it on the screen wit echo. I thnik that i got the 'founding part' but the number does'nt been writet out on the screen. Code:

View Replies !
Find Array Key
I was thinking it was going to be easy using the array_key function but I can't seem to get it to work. This is my array:

$standings = array(
array("team7", "gp", "w", "l", "d", "gf", "ga", "gr", "pts"),
array("team6", "gp", "w", "l", "d", "gf", "ga", "gr", "pts"),
array("team3", "gp", "w", "l", "d", "gf", "ga", "gr", "pts"),
array("team8", "gp", "w", "l", "d", "gf", "ga", "gr", "pts"),
array("team5", "gp", "w", "l", "d", "gf", "ga", "gr", "pts"),
array("team9", "gp", "w", "l", "d", "gf", "ga", "gr", "pts"),
array("team1", "gp", "w", "l", "d", "gf", "ga", "gr", "pts"),
array("team2", "gp", "w", "l", "d", "gf", "ga", "gr", "pts"),
array("team4", "gp", "w", "l", "d", "gf", "ga", "gr", "pts"),
array("team10", "gp", "w", "l", "d", "gf", "ga", "gr", "pts"),
);

What I would like is to pull the key for "team5"... which in the case would be 4.

View Replies !
Find A Specific Value In An Array
What I want to do is compare a value against an array and perform a specified action if the value is in the array. Specifically, I want to preselect an option in a list based on whether or not the option id is in a field (a 'set' column) of a specific entry in a db. My code so far is: PHP Code:

View Replies !
Find Mysql Row In An Array?
I would like to be able to match values from my mysql table to an array.  For example:

$result = mysql_query('SELECT * FROM db');
while ($row = mysql_fetch_array($result)) {
  if ($array == $row[id]){
  echo "We found your value"}
}

I realize this isn't a functional example, but basically I would like to match the id value with the same value in the array and if it doesn't exist go to the next row of the mysql.  I've tried a few things such as array_filter, with no success. It seems like theres an easy way to do this that I don't know of.

View Replies !
Find A String In Array
i have this array:

$str = array("apple is red", "banana is yellow", "pineaple is tropical fruit", "coconut is also tropical fruit", "grape is violet");

i want to find string "coconut" inside $str and put the whole string "coconut is also tropical fruit" into variable $coconut. Code:

View Replies !
Find Current Index Value In Array
I am looking for a way to find the numerical index of an array item without coding in a manual count using a foreach.

Here is the situation:

I am throwing a class a configuration array, where all of the arrays will have the same number of items as follows: PHP Code:

View Replies !
Calculating The AVG/MEAN Of An Array? Cannot Find Any Function.
I have not found a math function for average that will work on an array. There are a lot of other functions such as MIN MAX RAND etc. but no AVG or MEAN. My data is a large csv text field and thus cannot use MySQL to calculate those for me easily.

View Replies !
Loop Through Array Find File Extension
i need to build a little script for my browser script...

i need to do the following...

extract the .ext from the given file name, then i need to pass that to an array that has a list of valid extensions and what icon to use.

View Replies !
Find And Replace Text In Preg_match Array
I'm trying to change a font color html code once my preg_match_all reads the file. Now the code only shows up when a winner has been posted for that tour. Otherwise it's not visible. So I want to search the preg results which is $matches, for the "font color=3FFF5E" and replace it with "font color=FFFFFF". If the code is not found, continue what it was doing. Code:

View Replies !
Find Number Of Instance Of Array Element
I have an array $a with values 5,7,5,8 and i want to find out how many times the number 5.

View Replies !
In_array() To Find An Object Inside An Array
in_array() to find an object inside an array ....

View Replies !
Find Prev/Next Vals In Array W/ Named Indices
Hi,

I have an array of about 15 values with all named indices. How would I find the previous/next values in the list when given, in a variable, one of those named indices? The Prev/Next values should be based on the order in which they were entered. Thanks.

View Replies !
How Do I Find A Word In A Sentence From The List Of Words In An Array?
I have few list of words (needle) I want to find in a sentence (haystack). After a word is found, how would I echo that word? Is there a way to have a multi array so:

if the sentence contains word1,word2 or word3 then echo "coding"
if the sentence contains word4,word5 or word6 then echo "forums"

<?php
$title_of_the_item = "This would be the sentence being pulled.";
$words_from_title = explode(" ", $title_of_the_item);

$word = array("structure", "php", "sentence", "pulled");

if(in_array($word,$words_from_title)){
//even though two words are found in the array, use only 1
}
?>

View Replies !
Iterator First Item?
Let's say I have the code:

foreach($data as $row)
{
do something with each $row
}

Is there a way to do something specific to the first item iterated?
Pseudocode:

foreach($data as $row)
{
if first time through iteration do something with $row
else do something which each remaining $row
}

Is this possible? I think I could use a regular 'for' loop, using the
length of the $data array as a guide, and then an 'if..else' structure
to see if it is the first time through the 'for' loop, but I was
looking for a more elegant solution.

View Replies !
Get One Row, One Item From Database
I was trying to grab just one row and one field in that row from MySQL, but couldn't find a way other than: PHP Code:

View Replies !
Trying To Split The Item...
I'm trying to make a search thru my mySQL database, so it any keywords are found then I'm adding the details to the next item in an array that I create. I've tested the array and it is storing the information into the array properly because it echoes out all of the information correctly. My problem is that, at the beginning of each item in the array I have information that tells me what part of the database it was pulled from and the id number and also how many times the a keyword was found in that specific database entry. So you have something like this.. Code:

View Replies !
Get Last Item In URL String
I realize how elementary this is, I apologize. Maybe that's why I can't find the right answer. I've tried everything from EXPLODEing the string to SUBSTR_REPLACE.

Can anyone tell me how to just get the last item in a URL string? Like turn this:

/mydirectory/myimg.jpg

into this:

myimg.jpg

View Replies !
Most Recent Item
I have a table with every post in - which has a column for the time the post was created and a column for the thread that this post is located in. My threads table has a column for which forum this thread is in and a column for whether the thread is a sticky or not.

On the forum display page, I'd like to display every thread that is in that forum, but ordering them so that any Sticky threads show first (ordered by date created), and then the non-sticky threads are displayed after those ordered according to the time of the most recent post within that thread.

View Replies !
Help With Featured Item
if its possible to display a featured item from mysql table.

Table , links (id, item, hits)

How i could i display 5 links which have bigest numbers ? Is this possible ? I only need query sample, not the entire code.

View Replies !
Pulling One Item
I have a MySQL database with info stored in it (speakers at a weekly seminar). I want to display the seminar's speaker info on my webpage every week. I need it to roll over to the next week's speaker every Thursday at 1:00. I have no clue about where to even get started with this. any ideas?

View Replies !
How To Display One Item Only
Hello, in my query I return

date1 item 1
date1 item 2
date1 item3

date2 item4
date2 item5

how can I make it display

date 1 item1
item2
item3

date 2 item4 item5

View Replies !
Get A Person's IP When They Add An Item?
Is there a way to get a person's IP when they add an item? I have a voting system on my site, but I've had a few people "stuff the ballots" and want to track IPs. How can I do this easily.

View Replies !
Activate An Item
I have a table with some items in it. Only one of them is active. On a page I display them all and you can click to activate one, the id of that one to activates is posted. What's the best way to activate that one and to deactivate the current active one? Now I use three queries: look up the current active one, deactivate it, activate the new one. I think there is a better way to do this.

View Replies !
Return Last Item
How can I return just the last item of an array? For example: PHP Code:

$mytest = explode(",", $example);

In one case: PHP Code:

$example = "milk,eggs";

PHP Code:

$example = "eggs,shampoo,batteries";

View Replies !
How To Make Url Links To An Item
Hi i was wondering if anybody could tell me how to make url links to an item in my site from a list of search results. Here is a piece of my code:

<?php
$Name = $HTTP_POST_VARS["query2"];
$Eventtype = $HTTP_POST_VARS["query2"];
$EventStart = $HTTP_POST_VARS["query2"];
$EventEnd = $HTTP_POST_VARS["query2"];
$VenueName = $HTTP_POST_VARS["query2"];
if($Name or $Eventtype or $EventStart or $EventEnd or $VenueName)
{

$conn=@mysql_connect("mysql", "u0rr")
or die("Sorry can't connect to database");

$rs = @mysql_select_db("u0rr", $conn)
or die("Sorry can't connect to database");

$sql="select * from event left join venue on
(event.VenueID = venue.VenueID)
where (Name like '%$Name%'
or VenueName like '%$VenueName%')
AND Eventtype = 'Comedy' ";
$result=mysql_query($sql,$conn);

while( $row = mysql_fetch_array($result) )
{
echo("<b>"." Event Name: "."</b>".$row["Name"]."<br>");
echo("<b>"." Event Type: "."</b>".$row["Eventtype"]."<br>");
echo("<b>"." Venue Name: "."</b>".$row["VenueName"]."<br>");
echo("<b>"." Event Start: "."</b>".$row["EventStart"]."<br>");
echo("<b>"." Event End: "."</b>".$row["EventEnd"]."<br>");
echo("<b>"." Availability: "."</b>".$row["Availability"]."<br>");
echo("<b>"." Description: "."</b>".$row["Description"]."<br><hr>");
}
}
?>
For example if i enter an event type as 'comedy', a list of search results will come up from the form input displaying comedy events. Now from these results i would like to be able to click on Venue Name and this would link to that page in another part of my site.

I hope this explanation is o.k, if not post a thread & i'll try and explain a bit better. Thanks for any help

View Replies !
Search Item In Database
I made a function that does a case insensitive search. This works fine.
But i'd like to improve that script so that it also returns items from the database that are partly similar to the search string.

this is the script atm:

View Replies !
Delete Item From Database
I still can't figure out how to delete an item from my shoppingcart. I have tried different ways to do this, but its impossible.

-a button or link that deletes a item from my database
-the php script that deletes it

here is a suggestion:

$slett = "DELETE ID, VARE, MODELL, STR, ANTALL, PRIS,
TOTALPRIS FROM USER_TRACK
WHERE USER_ID = "$user_id"";

But how can i assign this to a button?

View Replies !

Copyright © 2005-08 www.BigResource.com, All rights reserved