Re: how to chnage the elements positons in array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



 Hi Rob
 Thanx for your resonse.
we have an arrray

<gallery>
<item>
<thumb>2.jpg</thumb>
<img>2.jpg</img>
</item>

<item>
<thumb>3.jpg</thumb>
<img>3.jpg</img>
</item>

<item>
<thumb>4.jpg</thumb>
<img>4.jpg</img>
</item>

<item>
<thumb>5.jpg</thumb>
<img>5.jpg</img>
</item>

</gallery>
it is a xml file we need to change the order of the arry. here is our code
for getting  xm as array and finding the element position.

---------------------------


<?php
if( ! ($fp = fopen( "./gallery.xml" , "r" )) )
  die("Couldn't open xml file!");
$person_counter = 0;
$person_data = array();
$xml_current_tag_state = '';
function startElementHandler( $parser, $element_name, $element_attribs )
{
  global $person_counter;
  global $person_data;
  global $xml_current_tag_state;
  if( $element_name == "ITEM" )
  {
   // $person_data[$person_counter]["author"] = $element_attribs["AUTHOR"];
  }
  else
  {
    $xml_current_tag_state = $element_name;
  }
}
function endElementHandler( $parser, $element_name )
{
  global $person_counter;
  global $person_data;
  global $xml_current_tag_state;
  $xml_current_tag_state = '';
  if( $element_name == "ITEM" )
  {
    $person_counter++;
  }
}
function characterDataHandler( $parser , $data )
{
  global $person_counter;
  global $person_data;
  global $xml_current_tag_state;
  if( $xml_current_tag_state == '' )
    return;
if( $xml_current_tag_state == "THUMB" ) {
    $person_data[$person_counter]["thumb"] = $data;
  }

  if( $xml_current_tag_state == "IMG" ) {
    $person_data[$person_counter]["img"] = $data;
  }
 /* if( $xml_current_tag_state == "ABSTRACT" ) {
    $person_data[$person_counter]["abstract"] = $data;
  }
  if( $xml_current_tag_state == "BODYTEXT" ) {
    $person_data[$person_counter]["bodytext"] = $data;
  }*/
}
if( !($xml_parser = xml_parser_create()) )
  die("Couldn't create XML parser!");

xml_set_element_handler($xml_parser, "startElementHandler",
"endElementHandler");
xml_set_character_data_handler($xml_parser, "characterDataHandler");
while( $data = fread($fp, 4096) )
{
  if( !xml_parse($xml_parser, $data, feof($fp)) )
  {
    break; // get out of while loop if we're done with the file
  }
}
xml_parser_free($xml_parser);






if(isset($_POST['submit']))
  {

 $position= $_POST['t1'] ;
  $file= $_POST['t2'];

 echo "position is".$position ."<br>";
 echo  "file is". $file ."<br>";
 echo "file is".$person_data[$position]['img']."<br>" ;


$article = $file;
echo "article is".$article;
$thisPos = -1;
$elementPos = 0;

$filename = 'gallery.xml';

if (!$fp = fopen($filename, 'w+')) {
print "Cannot open file ($filename)";

exit;
}
$lengthOfArray = count($person_data);
echo "length is". $lengthOfArray."<br>";
/*adding */

for( $i=0 ; $i < $lengthOfArray ; ++$i )
{
$thisName = $person_data[$i]["thumb"];
//echo "this name is".$thisName . "<br>";
if ($thisName == $article) {
echo "<b> match at position number: ". $i . "</b><br>";
$thisPos = $i;


echo "our match happened at". $thisPos."<br>";
if ($thisPos==-1)
{

echo "$i;br>its NOT on the ARRAY thisPos value is $thisPos we need to
append";
$elementPos = $lengthOfArray;

}
else
{

//echo "$i;br>it IS on the ARRAY at postion $thisPos we need to over write";
$elementPos = $thisPos;
 echo "elementpos".$elementPos."<br>";
}
//echo "$i;br>---this is elementpos value $elementPos<br>";
echo "before if. <br>";







}
}



On Wed, Apr 30, 2008 at 7:31 PM, Atkinson, Robert <ratkinson@xxxxxxxxxxxxx>
wrote:

>   array_multisort() can be used to sort multi-dimensional arrays. If you
> can
> give us an idea of what the arrays look like and how you want to change
> the
> order, perhaps we can suggest a method?
>
> Rob.
>
>
> -----Original Message-----
> From: php-objects@xxxxxxxxxxxxxxx <php-objects%40yahoogroups.com> [mailto:
> php-objects@xxxxxxxxxxxxxxx <php-objects%40yahoogroups.com>] On
> Behalf Of malayappa2000
> Sent: 30 April 2008 14:56
> To: php-objects@xxxxxxxxxxxxxxx <php-objects%40yahoogroups.com>
> Subject:  how to chnage the elements positons in array
>
> Hi everybody
>
> my requirement is i have a mutlidimensonal array i need to change the
> order of array elements .could you plz give me some solution how to
> chage the order elements of array
> Thanks&regards
>
>
> ***********************************************************************************
> Any opinions expressed in email are those of the individual and not
> necessarily those of the company. This email and any files transmitted with
> it are confidential and solely for the use of the intended recipient
> or entity to whom they are addressed. It may contain material protected by
> attorney-client privilege. If you are not the intended recipient, or a
> person responsible for delivering to the intended recipient, be advised that
> you have received this email in error and that any use is strictly
> prohibited.
>
> Random House Group + 44 (0) 20 7840 8400
> http://www.randomhouse.co.uk
> http://www.booksattransworld.co.uk
> http://www.kidsatrandomhouse.co.uk
> Generic email address - enquiries@xxxxxxxxxxxxxxxxx<enquiries%40randomhouse.co.uk>
>
> Name & Registered Office:
> THE RANDOM HOUSE GROUP LIMITED
> 20 VAUXHALL BRIDGE ROAD
> LONDON
> SW1V 2SA
> Random House Group Ltd is registered in the United Kingdom with company
> No. 00954009, VAT number 102838980
>
> ***********************************************************************************
>
>  
>



--


[Non-text portions of this message have been removed]


[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Soap]     [Kernel Newbies]     [Yosemite]     [Yosemite Campsites]

  Powered by Linux