array_splice and array_search functions- how to use it?
Posted: Fri Mar 23, 2007 1:53 pm
Hi I am using php5.
My code is as follows: I want to update the array
I am getting output is as follows:
ProductName=>ABC
YahooMinPrice=>$10
0=>$5
1=>$5
-------------------------------------------------------
ProductName=>ABC
YahooMinPrice=>$10
0=>$5
1=>$5
array_splice($yahoo_info[$i], 2, 0, $Amazon); inserting the record twice because of the loop. How to add it only once ?
I want following output
ProductName=>ABC
YahooMinPrice=>$10
AmazonPrice=>$5
-------------------------------------------------------
ProductName=>ABC
YahooMinPrice=>$10
AmazonPrice=>$5
My code is as follows: I want to update the array
Code: Select all
<?php
for($i=0; $i<2; $i++) {
$yahoo_info[$i] = array('ProductName' => "ABC" ,
'YahooMinPrice' => "$10");
}
for($i=0; $i<2; $i++) {
foreach ($yahoo_info[$i] as $key => $value) {
if (array_search("ABC", $yahoo_info[$i], true))
{
$AmazonP = array('AmazonPrice' => "$5");
array_splice($yahoo_info[$i], 2, 0, $AmazonP);
}
}
}
for($i=0; $i<2; $i++) {
echo "<hr />\n";
foreach ($yahoo_info[$i] as $key => $value) {
echo $key.'=>'.$value.'<br />';
}
}
?>ProductName=>ABC
YahooMinPrice=>$10
0=>$5
1=>$5
-------------------------------------------------------
ProductName=>ABC
YahooMinPrice=>$10
0=>$5
1=>$5
array_splice($yahoo_info[$i], 2, 0, $Amazon); inserting the record twice because of the loop. How to add it only once ?
I want following output
ProductName=>ABC
YahooMinPrice=>$10
AmazonPrice=>$5
-------------------------------------------------------
ProductName=>ABC
YahooMinPrice=>$10
AmazonPrice=>$5