Ajutor ForEach

Secţiune dedicată începatorilor.

Moderatori: Moderatori, Start Moderator

cipcip
Senior Member
Mesaje: 201
Membru din: Dum Feb 22, 2009 1:26 pm

Ajutor ForEach

Mesajde cipcip » Vin Mar 01, 2013 11:45 am

Salut, vreau sa preiau de pe un site niste date prin cURL, dar m-am impotmolit la acest foreach.

Cod: Selectaţi tot

$content = get_content("www.test.ro");
$coduri = array();
$cod = preg_match('#Cod: <b>([0-9\.]*)</b>#',$content,$CodProdus);

$i = 0;
foreach($cod as $item)
{
   $coduri = $item;
   $i++;
}


Unde get_content este o functie ce contine cURL.

Primesc eroarea urmatoare:

Warning: Invalid argument supplied for foreach() in C:\Program Files (x86)\EasyPHP-12.1\www\test\index.php on line 49


Ma puteti ajuta va rog ? Multumesc.


Imagine

cipcip
Senior Member
Mesaje: 201
Membru din: Dum Feb 22, 2009 1:26 pm

Re: Ajutor ForEach

Mesajde cipcip » Vin Mar 01, 2013 2:06 pm

Am mai umblat putin la cod:

Cod: Selectaţi tot

preg_match("#Cod: <b>([0-9\.]*)</b><br />Pret: ([0-9\.]*)#",$content,$Produs);
$cod = htmlentities($Produs[1]);
$pret = htmlentities($Produs[2]);
//echo "Cod: $cod<br />Pret: $pret";
var_dump($Produs);
die();
$i = 0;
foreach($Produs as $item)
{
echo 'Cod:'.$item[1].'<br/> Pret:'.$item[2];
$i++;
}


Tot nu prea imi da ce vreau eu, adica dupa acel var_dump primesc astA:

Cod: Selectaţi tot

array(3) { [0]=> string(33) "Cod: 32720
Pret: 1.17" [1]=> string(5) "32720" [2]=> string(4) "1.17" }


Acelasi cod de 2 ori, pe pagina respectiva sunt 24 coduri, respectiv 24 preturi. Cred ca nu am facut bine acel preg_match.
Imagine

Avatar utilizator
MihaiC
Senior Member
Mesaje: 1644
Membru din: Dum Mai 14, 2006 8:07 pm

Re: Ajutor ForEach

Mesajde MihaiC » Vin Mar 01, 2013 3:31 pm

Foloseste preg_match_all.
preg_match se opreste la prima.
Dev @ oblio.eu

cipcip
Senior Member
Mesaje: 201
Membru din: Dum Feb 22, 2009 1:26 pm

Re: Ajutor ForEach

Mesajde cipcip » Vin Mar 01, 2013 6:40 pm

Imi puteti da un exemplu de a afisa un array multi-dimensional va rog ? Am incercat o gramada de variante si niciuna nu merge...

Mie imi trebuie sa afiseze asta:

Cod: Selectaţi tot

echo "Cod: ".$Produs[1][0];
echo "<br />";
echo "Pret: ".$Produs[2][0];


Codul se afla in $Produs[1] si Pretul in $Produs[2]
Imagine

cipcip
Senior Member
Mesaje: 201
Membru din: Dum Feb 22, 2009 1:26 pm

Re: Ajutor ForEach

Mesajde cipcip » Vin Mar 01, 2013 8:27 pm

Am rezolvat cu:

Cod: Selectaţi tot

$xx = sizeof($Produs[1]);
$yy = sizeof($Produs[2]);

for($x = 0, $y = 0; $x <= $xx, $y <= $yy; $x++, $y++) {
   echo 'Cod: '.$Produs[1][$x].'<br /> Pret:'.str_replace('ron/buc(tva inclus)','',$Produs[2][$y]). '<br />';
}


Multumesc.
Imagine

Avatar utilizator
MihaiC
Senior Member
Mesaje: 1644
Membru din: Dum Mai 14, 2006 8:07 pm

Re: Ajutor ForEach

Mesajde MihaiC » Vin Mar 01, 2013 10:54 pm

Nu inteleg, ce sens are toata aiureala aia ?

Cod: Selectaţi tot

$xx = sizeof($Produs[1]);
$yy = sizeof($Produs[2]);

$xx si $yy vor avea tot timpul valori egale.

Si daca ai un array pe care trebuie sa il parcurgi, de ce for si nu foreach ?

Cod: Selectaţi tot

foreach ($Produs[1] as $key=>$value) {
    echo 'Cod: '.$Produs[1][$key].'<br /> Pret:'.str_replace('ron/buc(tva inclus)','',$Produs[2][$key]). '<br />';
}
Dev @ oblio.eu

cipcip
Senior Member
Mesaje: 201
Membru din: Dum Feb 22, 2009 1:26 pm

Re: Ajutor ForEach

Mesajde cipcip » Sâm Mar 02, 2013 4:10 pm

Multumesc, am folosit foreach pana la urma. Folosisem for nu stiu de ce, asa mi-a venit ideea atunci cand am facut.
Imagine


Înapoi la “PHP Incepători”

Cine este conectat

Utilizatori ce ce navighează pe acest forum: Google [Bot] și 32 vizitatori