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.





Changing A Value Of An Element In A Two Dimensional Array


I am having a problem with array manipulation.

in my php script i am reading from a csv file.
the content of file is like this:




View Complete Forum Thread with Replies

Related Forum Messages:
Php Array Inserted (element By Element) Into Mysql DB
I want each element of the array to be inserted into the database, but only the last
element of the array is inserted... all the elements will echo... any ideas? I'm a PHP newbie... you can probably tell by the code:

View Replies !
Assigning A Multi-dimensional Array To & From A Session Variable Loses The Array
I have created a multidimensional array and then assigned that array to a session variable so that I can use it easily on another page.  However, when I go to the array later, there's never anything in it. Code:

View Replies !
Finding Elements Of An Array In Another Multi-dimensional Array
So I have 2 arrays:
one contains userids. It may look like:
user_id[0] =12,
user_id[1] =30,
user_id[2] =43

The other is a multi-dimensional array with fields like:
user_info [0] = Array
(
[user_id] =13
[user_flag] =1
[url] =http://www.example.com?index,0
)
user_info[1] =Array
{
Array
(
[user_id] =120
[user_flag] =1
[address] =1234 Main St, Anytown, USA
[url] =http://www.yahoo.com
)
user_info[2] =Array
{
Array
(
[user_id] =130
[user_flag] =1
[address] =134 Main St, Anytown, USA
[url] =http://www.google.com
)

View Replies !
Take The Entire Array And Apply The Htmlentities Function To Each Array Element
what I want to do is take the entire array and apply the htmlentities function to each array element. So here is what I am doing:

$result = $dbconn->query("select * from tablename");

while($case = fetch_array_html($result))

Here is the function:

function fetch_array_html($result)
{
$arr = mysql_fetch_array($result);
foreach($arr as $key=>$val)
{
$arr[$key] = htmlentities($val);
}
return $arr;
}

Here is the error I get: Warning: Invalid argument supplied for foreach() on line 129
What exactly am I doing wrong here? I can usually fix my own errors, but this one is dominating me.

View Replies !
Array Element Disappears And The Whole Array Collapses And Renumbers Itself
checkbox arrays do not hold their respective positions as do other field arrays that are established to store the contents of selected records. The problem is that when you "uncheck" one of the elements and then repost,

that array element disappears and the whole array collapses and renumbers itself with only the remaining "checked" ones -- simply useless when dealing with records in a table where all the field array elements should be in sync.

I consider it a bug -- why does the array have to collapse? Why can't it just hold a null value and keep its position? The only fix is to never hold checkbox values in an array. Store them in individual variables numbered like an array, then do an "eval" to retrieve the contents. Here's the whole thing in action: Code:

View Replies !
Array Sorting Blues....2 Dimensional Array
I have a 2 dimensional array where one col contains a Unix style date and the other contains a filename. I read through a directory to get this info. I can load the array and print it out just fine, but I can't seem to sort it using any of the sort functions! I want to reverse sort them, since they are news stories that I want to display in the correct order by date. When I try to sort I get a "Warning: Cannot compare arrays or objects" message. I have the feeling it's something stupid on my part but can't find any good examples anywhere.

Anyone have an idea about this?

P.S. I know a database would solve this but sadly it's not an option for me right now...

View Replies !
Turn Array Into Multi-dimensional Array?
How can I take an existing array and turn it into a multi-dimensional array? I want to get a single row out of a MySQL table and have it in an array, but have along side the values the true field (column) length for each of the fields from the table. Here's my stab at it:

$userDetails = mysql_fetch_array($resultID);
$i = 0;
while ($i < mysql_num_fields($resultID))
{
    $userDetails[$i][2] = mysql_field_len($resultID, $i);
    $i++;
}

And I guess the follow up question (if this is indeed possible) is will this work with a multi-row return from the DB also?

View Replies !
2D Array - Count The Size Of Each Array Element
What i want is something like

for ($i=0;$i<$var;$i++) {
create 2D array
if (condition) {
push element in array //
}
}

ie..1=>a,b,c,d
2=>e,f,g
etc
then i want to count the size of each array element

eg. for array1 : no of elements 4
for array2 : no of elements 3
etc

Not done 2D arrays before.

View Replies !
3 Dimensional Array
I have been summing one element of a 3 dimensional array. My method works, but I think there is a better way to do it. My current way of doing this is causing me problems later in my code.

line 1 for ($j = 0 ; $j < $z ; $j++){
line 2 for ($k = 0 ; $k <= $y; $k++){
line 3 for ($i = 0 ; $i < $x; $i++){
line 4 $foo[$j][$k] = $bar[$j][$k][$i] + $foo[$j][$k];};};}

View Replies !
Count 2 Dimensional Array
i need to count 2-dimensional array $this->myarray[$a][$b] after $a, my code PHP Code:

View Replies !
PHP 4 OO Issue - 3 Dimensional Array
Hello, I have this simple script that i have a 3 dimensional array. However I am kinda new to OO php, and it keep getting the same error? Any ideas?

class maps{

//Initalize the city array
var $City = array();
$City['city1']['Dundas']['Codes'] = "L9H, L85"; //ERROR LINE
$City['city1']['Dundas']['Amount'] = 13915;
$City['city2']['North Burlington']['Codes'] = "L7M, L7P";
$City['city2']['North Burlington']['Amount'] = 19668;
$City['city3']['South Oakville']['Codes'] = "L6K, L6L, L6M";
$City['city3']['South Oakville']['Amount'] = 26044;

function display_cities(){
echo "hi";
foreach($this->City as $kCity=>$vCity){
echo $kCity." = ".$vCity."<br>";
}
echo "bye";
}
}

Error is in this class file on the line noted.
Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION

This is probably really simple, but i've been looking at it for a couple hours and cant figure it out.

View Replies !
Two Dimensional Array Problem
I want to put the contents of a csv file into a 2 dimensional array so that
I can access any element in a $table[x][y] manner and have come up with the
following code:

$complete = file("source.csv");
$row=0;
foreach ($complete as $value) {
$data = explode(",", $value);
$table[$row] = $data;
$row++;
}
echo "$table[6][4]";

However the echo statement returns "Array[4]" instead of the 4th element in
the 6th row.

Any suggestions on what I ought to be doing please to make $table a proper 2
dimensional array?

View Replies !
How Do I Sort My Two Dimensional Array?
How do I sort my two dimensional array? I want to sort "numb" numerically, then "part" alphabetically. Code:

View Replies !
Getting Data Out Of Three Dimensional Array
I want to extract data from 3-dimensional array into one dimensional array here's example of two arrays that i add into one: PHP Code:

View Replies !
Turn Xml Into 2 Dimensional Array
Well, www.php.net seems utterly crashed. Does anyone know how to get
an XML stream and turn it into a 2 dimensional array?

View Replies !
Sorting A Two Dimensional Array
I have an array, thus:

Array (
[0] => Array ( [Freq] => 28 [Name] => Tomato )
[1] => Array ( [Freq] => 4 [Name] => Banana )
[2] => Array ( [Freq] => 54 [Name] => Orange )
[3] => Array ( [Freq] => 24 [Name] => Eggplant )
[4] => Array ( [Freq] => 5 [Name] => Courgette )
[5] => Array ( [Freq] => 4 [Name] => Apple )
)

I would like to be able to sort it either by the Frequency or the Name
of the fruit or vegetable.

I just can't seem to get it to work. I'm sorry - I realise that this is
probably really easy but my brain isn't working today and the examples
on php.net seem incomprehensible to me.

View Replies !
Returning A 2 Dimensional Array
I am trying to program a function to return a 2 dimensional array, but
it's not working. I reduced the return value to 1 dimension and
tested that to make sure that the problem wasn't elsewhere. It had no
problem when the array was 1 dimensional.

Is there something different I need to do in order to return a 2
dimensional array from a function?

View Replies !
2 Dimensional Array And Foreach
I have a 2-dimensional array.  so I run it through two foreach loops to get the data out.  but, i want the foreaches to run in reverse.  lets say I have an array[3,2].  right now my foreach prints [1,1] [1,2] new line [2,1][2,2] new line [3,1][3,2] and i want it to print [1,1][2,1][3,1] new line [1,2][2,2][3,2]

View Replies !
Sorting Two Dimensional Array
I have a two dimensional array that I need to sort based on a total of values in the second dimension. The top level array has a part number and another array as its elements. The second array consists of defect types and defect quantity corresponding to the part number. I am able to sort the second defect array by defect type (so whichever type has the highest quantity shows up first) but I am not sure how to sort so that the parts with the highest total defects are first. Essentially I need to sort based on the total quantities in the second array, but I am not sure how to do that on the fly. Here is a print_r of the array.

Part#101 Array ( [Scratches] => 6 )
Part#102 Array ( [Scratches] => 174 [Handling] => 22 [Supplier Mold Defect] => 9 [Dirt] => 9 )
Part#103 Array ( [Scratches] => 167 [Dirt] => 116 [Handling] => 4 [Water Spots] => 3 [Supplier Mold Defect] => 1 )
Part#104 Array ( [Scratches] => 20 [Supplier Mold Defect] => 4 )

After sorting the part order should be:
Part#103 (291 total defects)
Part#102 (214 total defects)
Part#104 (24 total defects)
Part#101 (6 total defects)

View Replies !
Does Php Have A Multi Dimensional Array?
Does php have a multi dimensional array?

So eg. $someData[i][j]

View Replies !
Access A One-dimensional Array
if i have an array such as:

$_testArray['one'] = array();
$_testArray['one']['subone'] = somethingone;
$_testArray['one']['subtwo'] = somethingtwo;

and then i want to access these values elsewhere. how would i go about acessing the subone and subtwo items. does this make sense? i was able to access a one-dimensional array by using something like:

$_testArray['one'] = array();
$_testArray['one'] = somevalue;
$_testArray['two'] = somevalue2;

and then access it using:

$variable = "one";
so i guess i don't know how to do this for ['one']['subone']

View Replies !
Syntax For Two Dimensional Array
I have got some problems using a two dimensional array in an if expression:

PHP does not like:

if (table[$j]["banana"]==0)

and says:

Parse error: parse error, unexpected '[' in script.php on line 203

"if ({table[$j]["banana"]==0})" was also not accepted, can anyone help `?

I would also use the value in a two dimensional array as an index for another array:
$array[{table[$j]["banana"]}]

PHP does not like this, too.

View Replies !
Multi-dimensional Array
I have a function that loops through email addresses and checking them to see if they are valid. if the email address is valid i want to add it to the $validEmails array, and if its invalid i want to add it to the $invalidEmails array. BUT, in order to return this data from a function it will need to be a multi-dimensional array.

View Replies !
Subtotals In 2 Dimensional Array
Using array_push() from a collection of tables I have created the array below for a report.

2007-08-21, Interest, Dad, 82.00
2007-09-01, Electricity, Mum, 63.14
2007-08-20, Petrol, Dad, 18.71
2007-09-01, Electricity, Mum, 85.00
2007-08-19, Interest, Dad, 83.00

In the report I want to produce summaries like:

Electricity = 148.00
Interest = 165.00
Petrol = 18.71

And:

Dad = 188.00
Mum = 148.00

The categories like Electricity, Interest, Dad etc will be different depending what the user has entered. Producing the the basic report is straight forward. Producing the summaries has left me brain dead. I could have obtained the totals from the database but for ease (I thought) and to teach myself how to use multidimensional arrays I tried this approach. Can it be done?

View Replies !
Multi Dimensional Array
Here is my array. PHP Code:

$playerarray = Array ( [0] => Array ( [Player1] => jimmydee2 [Reg] => 1 [Rank] => 3 [Rating] => 1490 [Checkedin] => Checked In [Wins] => 0 [Losses] => 0 ) [1] => Array ( [Player1] => bob2 [Reg] => 2 [Rank] => 1 [Rating] => 1738 [Checkedin] => Checked In [Wins] => 0 [Losses] => 0 ) )

View Replies !
How Do You Sort A 2-dimensional Array?
$fbArray[$j] = array($feedbackCategoryArray[$i]['attributes']['NAME'] =>
$feedbackCategoryArray[$i]['attributes']['DISPLAYNAME']);

I can't begin to fathom in my befuddled mind how to sort this array $fbArray
into alphabetical order according to
$feedbackCategoryArray[$i]['attributes']['NAME'] - I am utterly lost, can
someone explain in detail how I would do it and why.

View Replies !
"Array To String Conversion" Error When Constructing A Multi-dimensional Array
I'm a bit stumped as I am getting a "Notice: Array to String
Conversion" error when trying to do something that on the surface
should be a very simple task - create an array, and write a set of
values to them based on data submitted from POST Fields.

Code below:

$_SESSION["increment"] = array();
$x = 0 // Counter - This will be
incremented

$_SESSION["increment"][$x] = array("increment_bond" =>
$_POST["increment_bond"],
"increment_amount" = $_POST["increment_amount"],
"increment_comm_date1" = $_POST["increment_comm_date1"],
"increment_comm_date2" =$_POST["increment_comm_date2"],
"increment_comm_date3" = $_POST["increment_comm_date3"]);

It keeps throwing out that error, and does not write any of the POST
Values to the array (the POST values are definitely getting sent, so
that is not the problem).

If I dump out the value of $_SESSION["increment"], I get "1A" ?!?

I'm sure that there is a pretty simple mistake lurking somewhere - I
just can't seem to figure it out.

View Replies !
Does An Array Contain An Element?
I'm using PHP 4.4.4. Is there a shorter way to check if an array has
an element besides doing a for loop and iterating through each element?

View Replies !
Is The Element Of An Array The Last One ?
What is the best way to solve the next problem: PHP Code:

View Replies !
Getting Element Only Once In Array
I've got an array like so: 1,1,5 But I want to try and make it so if it has the ID appearing more than once then it won't be in the array multiple times. i.e. I want the above to say 1,5.

View Replies !
How To Get Array Element?
get_the_category() returns an array that looks like this:

ResourceArray ( [0] =stdClass Object ( [cat_ID] =3 [cat_name] =>
Certifications [category_nicename] =ms-certifications
[category_description] =Certification Resource [category_parent] =0
[category_count] =5 [fullpath] =/ms-certifications ) )

I want to get the value in "category_description" and have tried:

$category = get_the_category();
$cat_d = $category->category_description;
print_r($cat_d);

But that doesn't print anything. What is the correct way to get the
value I'm after?

View Replies !
Array Element
I am having trouble getting a weather sticker php script to work properly. I have been working with the guys that wrote it, but so far they haven't come up with a solution. They/we have narrowed it down to the section of the script that compares a text string (which has been converted into UPPER CASE) from a file generated by the weather station, with an array. It is supposed to match an index in the array, that points to which graphic file to generate. This works about 30% of the time.

(When the text string is VERY SIMPLE - like CLOUDY or CLEAR or RAIN) But not when the text string is more complex, like HEAVY RAIN, MIST OVERCAST, LIGHT RAIN or PARTLY CLOUDY.

Here is the line that defines one of the elements in the array:

Quote $vws_icon["PARTLY|MOSTLY+CLOUDY|SUNNY+THUNDERSTORM"] = "./icons/" . "$daynight" . "_tstorm.$image_format";

I did not incluse the whole script, nor the whole array because of its length.

My Question (Finally...) is --- in the line above within the square braces, he has enclosed a string in double quotes, but the string is delimited by | characters and + characters. Can someone explain to me in English what this means? I have tried reading through two big reference books on php arrays, and they only made me even less certain. (For example: "the line means - If the string is PARTLY or MOSTLY CLOUDY or SUNNY and THUNDERSTORM, then set $vws_icon to whtever the $daynight._tstorm.$image_format evaluates to.)

I know, my example translation makes NO SENSE in English. Maybe it makes no sense in php either? I think what he's trying to ask is "If the string contains 'THUNDERSTORM' and any of these other words, then generate the correct icon file for thunderstorm".

View Replies !
Using The Last Element In An Array
This is the code I have: PHP Code:

$sql = mysql_query("SELECT * FROM dt_directories WHERE category = '$category' ORDER BY name") or die(mysql_error());
  
$last_sql = mysql_query("SELECT * FROM dt_directories WHERE category = '$category' ORDER BY name DESC LIMIT 1") or die(mysql_error());

while ( $last = mysql_fetch_object($last_sql) ) {
     $last_element = "$last->name";
}

while ( $dirs = mysql_fetch_array($sql) ) {
     $allowed_dirs = $allowed_dirs."name = '$dirs[name]' OR ";
    }

What this does is output the following into the variable $allowed_dirs:

E.G - name = 'Best of the Web' OR name = 'Dmoz' OR name = 'Yahoo!' OR

Another part of the code outputs the last element in the array to the variable $last_element, in the example above, $last_element would be "Yahoo!". What I want to do is use $last_element to remove the last "OR" in the example above. Any ideas?

View Replies !
Sum One Array Element
I have an array that contains two elements partNo and Quantity. I want to sum the quantity element for all values in the array. From what I can tell array_sum() sums all elements and converts non-numeric fields to their number equivalent or something. whatever it does I don't get a good total. I'm sure there is an easy solution but I'm hitting a wall.

View Replies !
Get Key Of The First Element Of An Array
Is there an easy way to get the key of the first element of an associative array?

View Replies !
How Do I Use The First And Last Element Of An Array
I want to use the first and last element of an array, don't care what happens to the rest. I tried using.

$count=count($name);
$fname = $name[0];
$lname= $name['$count'];

but it only gives me the first one.

any ideas?

View Replies !
Add Element Onto Array
I have an array that has been already created from a script. Here is the code for that array:

$returnArray[]= array("id" => $smilieRow['id'],
"url" => $smilie->url,
"bbcode" => $smilie->bbcode);

I have another array too that is created pretty similarly to that. What I want to do is add another element to the array called "$num". Now I would just loop through it again with a foreach, but is there an easier way?

View Replies !
Get Value Element From Array:
removing one key=>value element from array: I have an array of this sort:

array(
[#document]=> <?xml version ="1.0"?>
[finemae]=>2344ddd
[subscriptionstatus]=>unread
[trackingnumber]=>45566
}

I wanted to get rid of the first Key=>value element which is #document=> <?....?>

element:

insert into table (#document,filename,subscriptionstatus,trackingnumber) value ('"<?...?>','..',...);

View Replies !
How Do You Take An Element *out* Of An Array?
is there a way to leave a certain field out?

for instance, I'm using http_build_query() to build a new query and I have page_num=2&color_id=5&start=5 and all I want to do is keep color_id=5&start=5 .. I don't want/need the page_num info How can I take that out of the array?

View Replies !
Element From An Array
I have an array that contains numbers. Each element in the array is guaranteed to be unique.Let's say I have another variable which I know for certain is in the array, but I don't know the position.


View Replies !
Sorting Multi-dimensional Array
Sorting multi-dimensional array

I have this array:

Array
(
[0] => Array
(
[account] => 263
[counter] => 1
)

[1] => Array
(
[account] => 324
[counter] => 5
)

[2] => Array
(
[account] => 972
[counter] => 3
)

[3] => Array
(
[account] => 1316
[counter] => 3
)

[4] => Array
(
[account] => 1380
[counter] => 1
)

[5] => Array
(
[account] => 1880
[counter] => 2
)
)

I need to sort by the key [counter], how can I do that?

View Replies !
Multi-dimensional Array For Checkbox
Can there be a multi-dimensional array for checkboxes?

View Replies !
How Can I Sort A Multi-dimensional Array?
How can I sort a multi-dimensional array?

For instance:
a[Name], a[adress], a[number of messages].

How to sort on the item of number of messages?

View Replies !
Two-Dimensional Array Insertion And Print
1. return results from an Oracle query into an array. there are 3 fields and approx. 2,000 records.

2. insert the results into an array of some type, inside of a loop, obviously.

3. print the results so i can verify the query is working.

PHP Code:

View Replies !
Multi-Dimensional Array Question
Can you have a multi-dimensional array like this?

$Test[0]['Fname'] = "John";
$Test[0]['Lname'] = "Doe";

Or do you have to use only numbers?

View Replies !
Building Multi Dimensional Array
I am trying to build a multi dimensional array from an unsorted array. I have an array made up of the lines in a text file, each line is a comma seperated set of fields. The first two fields are the lines id and it's parents id; eg Line ID, Parent ID, Data

1, 1 , one
2, 2, two
3, 3, three
4, 1, four
5, 4, five
6, 1, six
7, 2, seven

I am trying to build a multi dimensional array so that the final array would look like;

-- one
---- four
------ five
---- six
-- two
---- seven

View Replies !
Make A Multi-dimensional Array
I am trying to make a multi-dimensional array. I am having trouble adding to the array. What I mean by that is using the array_push() function. Does anyone know how to add to a multi-dimesional array?Ex.

$somearray = array("1" => array("This is one"));

#The line above works. how can I add

"2" = > array("This is two");

View Replies !
Sessions And A Multi-dimensional Array
I'm writing a shopping cart w/ Sessions and a multi-dimensional array. I am only storing product id and qty (for right now, but decided on the multi-dimensional array for expansion purposes - storing customer instructions, etc.) I have come up with two paths, but can't decide which is better.

1. storing info based on product id:
...[cart][$prod_id] = array("qty" => $qty)

This, of course would allow for easy product checks: if ([cart][$prod_id]) {...}, but unless I store the $prod_id in the prod_id array as well, I'd be forced to use foreach(array_expression as $key => $value) when displaying the cart (as I also want the product id displayed next to the product name)

2. storing info based on item number:
...[cart][] = array("id" => $prod_id, "qty" => $qty)

This makes for more code when checking if the product is already in the cart (multiple foreach statements), but should allow the cart to be displayed more easily and logical.

View Replies !
Counting The First Dimension Of Two Dimensional Array
I'm trying to count just the items in the first dimension of a two dimensional array.
It seems like: PHP Code:

View Replies !
How To Print Elemnets Of N-dimensional Array?
What is a common way to print elements of a multidimensional array? I
treid to do it in the straightforward way:
$input_list[0][0] = "Something";
print "$input_list[0][0]";
And it does not work.

The solution that I found is to use intermediate variable:
$array = $input_list[0];
print "$array[0]";
printArray( $output_list[0] );

Are there any more elegant (or just common) ways to do it?

View Replies !
Convert Two-Dimensional Array To String
I'm trying to convert a two-dimensional array to a string that I can put in a database. It needs to be searchable so the serialize() function will not work. I found a script at php.net that will implode a multi-dimensional array and it works fine. The script is: Code:

View Replies !
Printing From A Multi-dimensional Array
I have to create a multidimensional array that data for a bank will be stored in then from that array I will print it. My boss said to first program it like i am recieving the array. Then later he will have me create the array.

Order multidimensional Array I will recieve will be arranged in and the printed like this. Code:

View Replies !

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