0

Hi I am very new to php and I am having 3 php arrays namely bookname, bookprice and bookisbn, I need to insert the values like

"bookisbn" "bookname" "bookprice" into mysql

eg:

isbn1 bookname1 bookprice1 
isbn2  bookname2 bookprice2
isbn3  bookname3 bookprice3

As of now I tried to iterate the three arrays something like,

foreach($booknamearray as $bookname && $bookpricearray as $bookprice && $bookisbnarray as $bookisbn) { .. }

and

while($booknamearray as $bookname && $bookpricearray as $bookprice && $bookisbnarray as $bookisbn){ .. }

Nothing worked me, please kindly help me out to achieve this.

Thanks in advance, Naveen.

2 Answers 2

2

Assuming they all have the same number of elements, you can use a for loop, and make a string of all the values:

for($i = 0; $i < count($booknamearray); $i++) {
    $str = $booknamearray[$i] . " " . $bookpricearray[$i] . " " . $bookisbnarray[$i];
    //Insert $str into db
}
Sign up to request clarification or add additional context in comments.

5 Comments

You are mixing js and php should be count($booknamearray) not $booknamearray.length
the joys of mixed language development :)
@General_Twyckenham: Its worked for me Thanks. Please Let me know if the length various, length will not vary in the above cases, but I may need to add the "bookofferprice" in the future. how to do in this case ?
Hey I think for the "bookofferprice" (various array length) below mentioned Marc B solution works I guess, Am I right ?
I think both Marc's solution and my solution will be able to handle adding bookofferprice, assuming the indices are the same. And yes, I did forget bookisbnarray[$i], good catch.
0

While you don't say so explicitly, I'm guessing your 3 arrays have their various values in the SAME KEY, e.g.

$booknamearray[0] -> 'name1';
$bookisbnarray[0] -> 'isbn1';
$bookpricearray[0] -> 'price1';

In which case you can do

foreach($booknamearray as $key => $name) {
   $isbn = $bookisbnarray[$key];
   $price = $bookpricearray[$key];
   etc...
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.