Forum Moderators: coopster

Message Too Old, No Replies

inserting array and match array

all question is all about array

         

nanat

2:22 am on Jan 28, 2010 (gmt 0)

10+ Year Member



Guys arrays are really pain in my head.. im working it for 2 days but still i dont get it.
My Array:

Array ( [0] => 833759,banana,1/28/2010[1] => 833280,apple,1/28/2010)

1st question: how can i insert an array within the array?
for example:
$myArray = array(orange, blah);


Array ( [0] => 833759,banana,1/28/2010,orange,blah [1] => 833280,apple,1/28/2010,orange,blah)

2nd question: how can i find the array with the match element
for example:
$find = 'banana';


$subject = Array ( [0] => 833759,banana,1/28/2010[1] => 833280,apple,1/28/2010)

if(function($find, $subject , true)
{
echo 'yes find it ';
}
else
{
echo 'no i dont';
}

i really appreciate if you help.

tnx,
nanat.

Readie

3:59 am on Jan 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For the first question, I believe you could create a similar array with the the new values you want, and then use the array_merge function:

$fruit_array = array(0 => "Banana", 1 => "Orange", 2 => "Pear");
$taste_array = array(0 => "Yeuch", 1 => "Yum", 2 => "Meh");

$tasty_fruity_array = array_merge($fruit_array, $taste_array);

For the second, I believe you could do:

if(preg_match("/banana/", $tasty_fruity_array)) {
echo 'Found it!';
} else {
echo 'It&#39s not there :(';
}

EDIT:

Upon re-reading your post, I think what you mean is calling an $array1[0] and it calls everything in array2? In which case:

$fruity_array = array(
0 => array(0 => "Orange", 1 => "Yum"),
1 => array(0 => "Banana", 1 => "Yeuch"),
2 => array(0 => "Pear", 1 => "Meh")
);

$fruity_array[0][1] returns "Yum"
$fruity_array[2][0] returns "Pear"

EDIT 2:

Alternativley, as I suspect you really want to do, you could name the array and/or the contents for the description:

$fruity_array = array(
"Banana" => array("Taste" => "Yeuch", "Shape" => "Crescent")
"Pear" => array(0 => "Meh", 1 => "Bell")
"Orange" => array(0 => "Yum", 1 => "Round")
);

$fruity_array["Orange"][1] returns "Round"
$fruity_array["Banana"]["Taste"] returns "Yeuch"

Readie

5:03 am on Jan 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just a clarification: I unintentionally give the impression in my post that the keys for each array value HAS to match the form of the other values in the array, and if numerical HAS to be consecutive.

This isn't true, you could have:

$somearray = array(
0 => array("Fruit" => "Apple", 739 => "Yummy")
);

As for why you would do that... Well, I advise against it to save annoying moments of forgetfulness

EDIT:

typo in my first post, missed commas after the array[array]s in my second example -.-

[edited by: Readie at 5:37 am (utc) on Jan. 28, 2010]

nanat

5:31 am on Jan 28, 2010 (gmt 0)

10+ Year Member



hi readie. thx for replying

this is my orginal files:

im trying to convert csv to mysql by uploading the csv.


$fp = fopen($uploadfilename ,"r");
$contents = fread($fp, filesize("".$uploadfilename.""));
$newLine = explode("\n",$contents);
$sulod = array($today, $sessionID);
$rows = 1;

i really have big trouble inserting the date and the session id bacause $newLine

result as


Array ( [0] => 833759,banana,1/28/2010[1] => 833280,apple,1/28/2010)

i try array_push(),array_merge() but it doesn't work..

do you think inserting the the session and date would be impossible in that structure of array?
so it would result as


Array ( [0] => 833759,banana,1/28/2010,$today, $sessionID [1] => 833280,apple,1/28/2010,$today, $sessionID)

Readie

5:43 am on Jan 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm afraid my experience with .csv is extremley limited (and by that I mean non existent) :)

I could tell you how to generate the array if you were pulling the stuff from a mysql database, but you'll need one of the true guru's assistance with this now.

Anyways, I'm off for bed now. Good night.

jatar_k

4:02 pm on Jan 28, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



why don't you process the file line by line using fgetcsv instead of exploding the whole file on \n

it will makes things much easier

nanat

10:16 am on Jan 29, 2010 (gmt 0)

10+ Year Member



thank you jatar_k.. i get it..