foreach함수인데 해석좀 도와주세요...;;; 정보
그누보드 foreach함수인데 해석좀 도와주세요...;;;본문
여기다 이런글 올리는거 점 머하지만... phpschool에 올려도
장시간 답변이 없길래 여기분덜 답변도 빠르고 친절하셔서
올려봅니다. 부탁드리면서 질문은...담과 같습니다.
foreach($items as $i=>$opmlitem)
{
$showeditems=$opmlitem['showeditems'];
$xmlurl=$opmlitem['xmlurl'];
$position=$opmlitem['position'];
}
위 구문좀 해석해 주세요... 이런형태는 웬지 어색하네용
items라는 배열에는 "showeditems"=>숫자, "xmlurl"=>주소, "position"=> 숫자
이런식으로 저장되어 있는거 같고요...
갑자기 머리에 이거 저거 쑤셔넣다보니 정리가 안되네용...
위 구문좀 해석 ^^;
장시간 답변이 없길래 여기분덜 답변도 빠르고 친절하셔서
올려봅니다. 부탁드리면서 질문은...담과 같습니다.
foreach($items as $i=>$opmlitem)
{
$showeditems=$opmlitem['showeditems'];
$xmlurl=$opmlitem['xmlurl'];
$position=$opmlitem['position'];
}
위 구문좀 해석해 주세요... 이런형태는 웬지 어색하네용
items라는 배열에는 "showeditems"=>숫자, "xmlurl"=>주소, "position"=> 숫자
이런식으로 저장되어 있는거 같고요...
갑자기 머리에 이거 저거 쑤셔넣다보니 정리가 안되네용...
위 구문좀 해석 ^^;
댓글 전체
그니깡 저 짧은 6줄의 의미는 완두콩님의 첫번재-네번째 구문과 같은 의미를 가진다는
말씀이군요...
말씀이군요...
(에혀... 역시 초보가 나서기엔... 너무... -_-a)
(해보려하다 도저히 안되겠어서 해보던데까지 적고... =3=3=3=3=3=3=3=3)
아래 테스트해보던소스 붙여봤는데... 4개모두 같은값 출력...
첫번째 그냥 뽑는건... 아주 작은배열에서나 사용가능...
보통 두번째 for()문정도 사용하는게 일반적...
(두번째랑 세번째랑 비교해보시는게...)
*** 그런데, ***
아래 2차원배열처럼 0,1, ... 이렇게 키가 순차적인 기본배열이 아니라면...
for()문 사용에 꽤(?) 애로점있음.(전 할줄 모름-_-a;)... 이럴때 foreach() 사용...
(아래 2차원배열에서 0 =>... 를 "a" =>... 문자등의 키로 바꾸면 for()사용한 두번째 방법은 안됨..
echo $items['a']['showeditems']; 이런식으로 접근가능..)
네번째는 2차원배열이라서 foreach()를 두번써본건데... 제 수준에선 거의 암호-_-;;;
list, each 도 비슷하게 쓰이는데... 그나물에그밥-_-a;;;
(역시 업데이트끝났다고 홀가분한 기분에 너무 나...섰...ㄷ... =3=3=3)
<?php
//2차원배열...
$items = array(
0 => array ("showeditems"=>1234, "xmlurl"=>"1.2.3.4", "position"=> 12341234),
1 => array ("showeditems"=>5678, "xmlurl"=>"5.6.7.8", "position"=> 56785678)
);
// 첫번째 배열값 직접 뽑기
$showeditems = $items[0]['showeditems'];
$xmlurl = $items[0]['xmlurl'];
$position = $items[0]['position'];
echo "$showeditems, $xmlurl, $position <br>\n";
$showeditems = $items[1]['showeditems'];
$xmlurl = $items[1]['xmlurl'];
$position = $items[1]['position'];
echo "$showeditems, $xmlurl, $position <br>\n";
echo "//^---------------1-------------<BR>\n";
echo "<BR>\n";
//두번째 for문으로 좀더 간단하게 뽑기
for ($i=0;$i<sizeof($items);$i++) {
$showeditems = $items[$i]['showeditems'];
$xmlurl = $items[$i]['xmlurl'];
$position = $items[$i]['position'];
echo "$showeditems, $xmlurl, $position <br>\n";
}
echo "//^---------------2-------------<BR>\n";
echo "<BR>\n";
//세번째 foreach 는 약간더 간단.
//$i 에는 키값 0,1 ... (위처럼 된 2차원배열이라면...) <--- 0 =>
//$opmlitem는 1차원배열... $items 에서 배열하나씩 빼옴... <--- array ("showeditems"=>1234, "xmlurl"=>"1.2.3.4", "position"=> 12341234)
foreach($items as $i=>$opmlitem)
{
$showeditems = $opmlitem['showeditems'];
$xmlurl = $opmlitem['xmlurl'];
$position = $opmlitem['position'];
echo "$showeditems, $xmlurl, $position <br>\n";
}
echo "//^---------------3-------------<BR>\n";
echo "<BR>\n";
//네번째 foreach를 한번더 쓰는것도 가능... 보기엔 영 꽝이지만...
// $$ 는 가변변수...
foreach($items as $i=>$opmlitem)
{
foreach($opmlitem as $key=>$value)
{
$$key = $value;
}
echo "$showeditems, $xmlurl, $position <br>\n";
}
echo "//^---------------4-------------<BR>\n";
echo "<P><P><P>---End";
?>
(해보려하다 도저히 안되겠어서 해보던데까지 적고... =3=3=3=3=3=3=3=3)
아래 테스트해보던소스 붙여봤는데... 4개모두 같은값 출력...
첫번째 그냥 뽑는건... 아주 작은배열에서나 사용가능...
보통 두번째 for()문정도 사용하는게 일반적...
(두번째랑 세번째랑 비교해보시는게...)
*** 그런데, ***
아래 2차원배열처럼 0,1, ... 이렇게 키가 순차적인 기본배열이 아니라면...
for()문 사용에 꽤(?) 애로점있음.(전 할줄 모름-_-a;)... 이럴때 foreach() 사용...
(아래 2차원배열에서 0 =>... 를 "a" =>... 문자등의 키로 바꾸면 for()사용한 두번째 방법은 안됨..
echo $items['a']['showeditems']; 이런식으로 접근가능..)
네번째는 2차원배열이라서 foreach()를 두번써본건데... 제 수준에선 거의 암호-_-;;;
list, each 도 비슷하게 쓰이는데... 그나물에그밥-_-a;;;
(역시 업데이트끝났다고 홀가분한 기분에 너무 나...섰...ㄷ... =3=3=3)
<?php
//2차원배열...
$items = array(
0 => array ("showeditems"=>1234, "xmlurl"=>"1.2.3.4", "position"=> 12341234),
1 => array ("showeditems"=>5678, "xmlurl"=>"5.6.7.8", "position"=> 56785678)
);
// 첫번째 배열값 직접 뽑기
$showeditems = $items[0]['showeditems'];
$xmlurl = $items[0]['xmlurl'];
$position = $items[0]['position'];
echo "$showeditems, $xmlurl, $position <br>\n";
$showeditems = $items[1]['showeditems'];
$xmlurl = $items[1]['xmlurl'];
$position = $items[1]['position'];
echo "$showeditems, $xmlurl, $position <br>\n";
echo "//^---------------1-------------<BR>\n";
echo "<BR>\n";
//두번째 for문으로 좀더 간단하게 뽑기
for ($i=0;$i<sizeof($items);$i++) {
$showeditems = $items[$i]['showeditems'];
$xmlurl = $items[$i]['xmlurl'];
$position = $items[$i]['position'];
echo "$showeditems, $xmlurl, $position <br>\n";
}
echo "//^---------------2-------------<BR>\n";
echo "<BR>\n";
//세번째 foreach 는 약간더 간단.
//$i 에는 키값 0,1 ... (위처럼 된 2차원배열이라면...) <--- 0 =>
//$opmlitem는 1차원배열... $items 에서 배열하나씩 빼옴... <--- array ("showeditems"=>1234, "xmlurl"=>"1.2.3.4", "position"=> 12341234)
foreach($items as $i=>$opmlitem)
{
$showeditems = $opmlitem['showeditems'];
$xmlurl = $opmlitem['xmlurl'];
$position = $opmlitem['position'];
echo "$showeditems, $xmlurl, $position <br>\n";
}
echo "//^---------------3-------------<BR>\n";
echo "<BR>\n";
//네번째 foreach를 한번더 쓰는것도 가능... 보기엔 영 꽝이지만...
// $$ 는 가변변수...
foreach($items as $i=>$opmlitem)
{
foreach($opmlitem as $key=>$value)
{
$$key = $value;
}
echo "$showeditems, $xmlurl, $position <br>\n";
}
echo "//^---------------4-------------<BR>\n";
echo "<P><P><P>---End";
?>