Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have stored the XML path to items in a string like this: response->items->item.

What I need to do is to access an array called $xml_array like this:

$xml_array['response']['items']['item']

When I write it in the code it works. The thing is that I want it to be done on the fly.

I use this to convert response->items->item to ['response']['items']['item']:

$xml_path = 'response->items->item';
$explode_path = explode('->', $xml_path);
$correct_string = false;

foreach($explode_path as $path) {
   $correct_string .= '[\''.$path.'\']';
}

the problem is that I can't access $xml_array by doing this: $xml_array[$correct_string]

So I end up with this:

$xml_tag = 'title';
$xml_path = 'response->items->item';

$correct_string = '$items = $xml2array';

$explode_path = explode('->', $xml_path);

foreach($explode_path as $path) {
    $correct_string .= '[\''.$path.'\']';
}
$correct_string .= ';';

eval($correct_string);
foreach($items as $item) {
    echo $item[$xml_tag].'<br />';
}

and access the $xml_array array through $items array. Is there any way I can do this and avoid using eval()?

Thanks in advance!

share|improve this question

2 Answers 2

up vote 1 down vote accepted
$xml_tag = 'title';
$xml_path = 'response->items->item';

$explode_path = explode('->', $xml_path);

$items = $xml2array[$explode_path[0]][$explode_path[1]][$explode_path[2]];

foreach($items as $item) {
    echo $item[$xml_tag].'<br />';
}
share|improve this answer
    
thank you so much for this! what if I want to automatically generate the $items variable instead of manually writing $explode_path[0] etc? After all I will retrieve this from MySQL. –  userid53 Mar 27 '12 at 7:23
    
OK got it: $explode_path = explode('->', $xml_path); $count_explode = count($explode_path); $items = $xml2array; for($i=0; $i<$count_explode; $i++) { $items = $items[$explode_path[$i]]; } –  userid53 Mar 27 '12 at 7:28

I'm really glad that your goal is to stop using eval(). :-)

If I've understood you correctly, you have an array of $items and you're trying to locate things in it based on the contents of $xml_path.

I obviously haven't tested this on your data, but what about something like this?

<?php

$xml_path = 'response->items->item';

$explode_path = explode('->', $xml_path);

foreach($items as $item) {
  $step = $item;
  foreach($explode_path as $path) {
    $step = $step[$path];
  }
  echo $step . '<br />';
}

The idea is that for each of the $items, you step through the exploded path, narrowing your search as you go by refining $step perhaps to some unknown depth.

This of course assumes that for any value of $xml_path, there WILL be a corresponding item set in the $items array. Otherwise, you'll want to add some error handling. (You probably want error handling anyway.)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.