วันอังคารที่ 7 ธันวาคม พ.ศ. 2553

XPath กับ CakePHP

น่าดีใจสำหรับผู้ใช้ CakePHP ซึ่งตอนนี้มีคำสั่งที่ช่วยให้ใช้ XPath กับ Array ได้
โดยไม่ต้องง้อ PHP Standard Function เลย อิๆ

<?php
Configure::write('debug',1);

$a=array(
array('User'=>array('id'=>1,'name'=>'somchai','salary'=>100)),
array('User'=>array('id'=>2,'name'=>'somsri','salary'=>300)),
array('User'=>array('id'=>3,'name'=>'doraemon','salary'=>250)),
array('User'=>array('id'=>4,'name'=>'nobita','salary'=>150)),
array('User'=>array('id'=>5,'name'=>'suneo','salary'=>250)),
);



$b=Set::extract($a,'/User/id'); pr($b);
/*
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 4
)
*/


$b=Set::extract($a,'/User[id=3]'); pr($b);
/*
Array
(
[0] => Array
(
[User] => Array
(
[id] => 3
[name] => doraemon
[salary] => 250
)
)
)
*/
$b=Set::extract($a,'/User/.[id=3]'); pr($b);
/*
Array
(
[0] => Array
(
[id] => 3
[name] => doraemon
[salary] => 250
)
)
*/


$b=Set::extract($a,'/User[id>3]'); pr($b);
/*
Array
(
[0] => Array
(
[User] => Array
(
[id] => 4
[name] => nobita
[salary] => 150
)
)
[1] => Array
(
[User] => Array
(
[id] => 5
[name] => suneo
[salary] => 250
)
)
)
*/


$b=Set::extract($a,'/User[id>3]/name'); pr($b);
/*
Array
(
[0] => nobita
[1] => suneo
)
*/
$b=Set::extract($a,'/User[:last]'); pr($b);
/*
Array
(
[0] => Array
(
[User] => Array
(
[id] => 5
[name] => suneo
[salary] => 250
)
)
)
*/

?>

ไม่มีความคิดเห็น:

แสดงความคิดเห็น