Forum Moderators: coopster
$itemID Array
0] => http://www.example.net/
1] => http://www.example.org/
The second array echoes two pieces of information. An ID that can range from 0 to 20, and another URL:
$itemURL Array
0 [example1.com...]
1 [science.example2.org...]
1 [example3.de...]
0 [example4.com...]
0 [example5.it...]
1 [example6.com...]
Im trying to figure out a way to replace the 1st value of the $itemURL array with the value of the matching key in the $itemID array. For example, the above would turn into:
http://www.example.net/ [example1.com...]
http://www.example.org/ [science.example2.org...]
http://www.example.org/ [example3.de...]
http://www.example.net/ [example4.com...]
http://www.example.net/ [iistsexample5it...]
http://www.example.org/ [example6.com...]
Ive tried a few things but nothing has worked and Im pretty much stumped. Thanks in advance for your help!
Here is the complete source code for the arrays:
$itemID = array ();
$itemURL = array ();
// ItemID Array // Item URL Array [1][edited by: martymac at 9:44 pm (utc) on Sep. 28, 2009]
$xmlgz = gzopen('xml-files/analyseditem.xml.gz', 'r');
$contents = gzread($xmlgz, 10000);
$xml = simplexml_load_string($contents);
foreach ($xml->DataTables->DataTable->Row as $row) {
$pieces = explode("¦", $row);
$itemURL[] = ($pieces);
}
$file_handle = gzopen("xml-files/analyseditem.dat.gz", "r");
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 1024);
if ($line_of_text[0] == "Header") {
continue;
}
echo $line_of_text[0] . $line_of_text[2] . "<br />";
}
[edited by: dreamcatcher at 6:33 am (utc) on Sep. 29, 2009]
[edit reason] No Urls Please. See TOS. [/edit]
$itemId = array("http://example.com", "http:example.net", "http://example.org");
$itemUrl = array("http://example2.com", "http:example2.net", "http://example2.org");
$tempArr = array();
foreach($itemId as $key => $i){
//if the key exists in the second array
if(array_key_exists($key, $itemUrl)){
//add the item to the new array
$tempArr[$i] = $itemUrl[$key];
}//if array_key_exists
}//foreach
//set the array
$itemUrl = $tempArr;
?>
Let me know if you have any questions.
$targetID Array $itemURL Array [6] => Array [7] => Array [8] => Array ) In the above $itemURL array, the value of 'target-id' should be replaced with the value of whatever key it matches in the $targetID array. All '0' should be Any ideas? [1][edited by: dreamcatcher at 7:02 pm (utc) on Sep. 30, 2009]
(
[0] => http://www.example.net/
=> http://www.example.org/
)
[5] => Array
(
[target-id] => 0
[url] => [example1.com...]
)
(
[target-id] => 0
[url] => [science.example.org...]
)
(
[target-id] => 0
[url] => [science.example.org...]
)
(
[target-id] => 1
[2] => [example2.com...]
http://www.example.net' and all 1 should be 'http://www.example.org'
[edit reason] No Urls Please. See TOS. [/edit]
$itemURL = Array("5" => Array("target-id" => "0", "url" => "http://www.example.com"),
"6" => Array("target-id" => "0", "url" => "http://science.example.org/article.pl?from=rss"),
"7" => Array("target-id" => "0", "url" => "http://science.example.org/article.pl?from=rss"),
"8" => Array("target-id" => "1", "url" => "http://www.example2.com"));
//go through the targetID array
foreach($targetID as $targetIdKey => $targetIdValue){
//start the double for on the intemURL
foreach($itemURL as $itemUrlKey => $tempArr){
//as long as your array structure never varies this
//will work otherwise it won't be cautious!
if(intval($tempArr['target-id']) == $targetIdKey){
//echo($targetIdValue);
$itemURL[$itemUrlKey]["target-id"] = $targetIdValue;
}//if
}//foreach $itemUrl
}//foreache $targetID
print_r($itemURL);
[edited by: dreamcatcher at 7:03 pm (utc) on Sep. 30, 2009]
[edit reason] No Urls Please. See TOS. [/edit]
for($i=0;$i<count($itemLines);$i++) {
foreach($itemID as $key => $val) {
if($itemLines[$i][0]==$key) {
$itemLines[$i][0]=$val; }
}
And this is much closer than before, however instead of replacing the entire value, it only changes once character. For example:
0 ¦ http://www.example.com
is changing to
h ¦ http://www.example.com
Thoughts?
[edited by: dreamcatcher at 7:04 pm (utc) on Sep. 30, 2009]
[edit reason] No Urls Please. See TOS. [/edit]
In this you are trying to check $itemLines[NUMBER][0] EG ($itemLines[1][0]) against the $key and if it matches, you are saying to change $itemLines[NUMBER][0] to = $key.
I think what you want is:
$itemLines[NUMBER]['target-id'] & $itemLines[NUMBER]['url']
Remember $i is just a counter, and you have an array of arrays, so what I posted loops through the array of arrays and checks against the single array.
I think the following should be correct: (If not, please post another example, because what you used does not match the example you posted. What I posted should work for what you posted.)
for($i=0;$i<count($itemLines);$i++) {
foreach($itemID as $key => $val) {
if($itemLines[$i]['target-id']==$key) {
$itemLines[$i]['url']=$val; }
}
} // Added this closing brace
I did forget quotes in my original tho... ['url'] & ['target-id'] Oops ;)
/* Numerically you could use $itemLines[$i][0] & $itemLines[$i][1]. They are exactly the same as: $itemLines[$i]['target-id'] & $itemLines[$i]['url'] */
/* Loop through the array of arrays $itemLines */
for($i=0;$i<count($itemLines);$i++) {
/* split the $itemID array into a key / value pair and loop through the array */
foreach($itemID as $key => $val) {
/* compare the ['target-id'] (also $itemLines[$i][0]) of the current ($i = number) $itemLines[$i] array to the 'key' of each $itemID and if there is a match, set the current ['url'] (also $itemLines[$i][1]) to the 'value' of the $itemID key that matched, which is: $itemID[N]='url'. $key=N; $val='url'*/
if($itemLines[$i][0]==$key) { $itemLines[$i][1]=$val; }
}
}
As far as the the if goes I try to not use extra lines of code, and if you install YSlow and FireBug and run it on a site sometime and see how much you can compress a file just by eliminating 'white space', you'll probably understand why. If there is more than one action or statement or 'blah' following the if I spread it out, but otherwise it stays on one line and when I code comments are only allowed after braces for debugging... They come out when the script is finished and I usually single space indent lines.
But, like you said, those are all personal preference / coding style things.
</aside>