2

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!

3 Answers 3

6

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.)

Sign up to request clarification or add additional context in comments.

Comments

3
$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 />';
}

2 Comments

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.
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]]; }
0
    $array = [];
    foreach($items as $item) {
        $inputs[] = [
            $item->name => $item->value
        ];
    }

1 Comment

Although this code may answer the question, providing additional context regarding why and/or how it answers the question would significantly improve its long-term value. Please edit your answer to add some explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.