php - how to make this array like array? -


array (     [0] => simplexmlelement object         (             [@attributes] => array                 (                     [timestamp] => 05:08:49:000                 )              [0] => title="test title 1"         )      [1] => simplexmlelement object         (             [@attributes] => array                 (                     [timestamp] => 05:14:20:000                 )              [0] => title="test title 2",artist="artist name"         )      [2] => simplexmlelement object         (             [@attributes] => array                 (                     [timestamp] => 05:14:36:000                 )              [0] => title="test title 3"         )  ) 

how make array array=>timestamp=>xyz, title=>xyz , artist=>xyz

i did best prepare array similar 1 have provided:

<?php  $string = <<<string <?xml version="1.0" encoding="utf-8" standalone="yes" ?> <whatever>     <things>         <thing timestamp="05:08:49:000">title="test title 1"</thing>         <thing timestamp="05:14:20:000">title="test title 2",artist="artist name"</thing>         <thing timestamp="05:14:36:000">title="test title 3"</thing>     </things> </whatever> string;  $array = array(); if( $xml = @simplexml_load_string( $string ) ) {     foreach( $xml->things->thing $thing )     {         $array[] = $thing;     } }  ?> 

then convert desired array:

<?php  $new_array = array(); foreach( $array $xmlobj ) {     $attributes = (string) str_replace(',',' ',$xmlobj);     $el = '<x ' . $attributes . ' />';     $x = new simplexmlelement( $el );      $title = isset( $x->attributes()->title )          ? (string) $x->attributes()->title         : null;      $artist = isset( $x->attributes()->artist )          ? (string) $x->attributes()->artist         : null;      $new_array[] = array(         'timestamp' => (string) $xmlobj->attributes()->timestamp,         'title' => $title,         'artist' => $artist     ); }  echo '<pre>'; print_r( $new_array ); echo '<pre>';  ?> 

that gives me array this, assume want based on question:

array (     [0] => array         (             [timestamp] => 05:08:49:000             [title] => test title 1             [artist] =>          )      [1] => array         (             [timestamp] => 05:14:20:000             [title] => test title 2             [artist] => artist name         )      [2] => array         (             [timestamp] => 05:14:36:000             [title] => test title 3             [artist] =>          )  ) 

Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -