/    Sign up×
Community /Pin to ProfileBookmark

collecting values in array from tags ??

Hello forums.
Supppose i had following format in a string

[code=php]$string = ‘<tag value=”1″>Name1</tag>
<tag value=”2″>Name2</tag>
:
:
<tag value=”n”>Namen</tag>’;[/code]

I would like to collect those values and names in an array as

[code=php]$array[‘name’] = array(‘Name1’, ‘Name2’, .., ‘Namen’);
$array[‘value’] = array(‘1’, ‘2’, .., ‘n’);[/code]

Any suggestions and hints are warmly welcome.

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@UI-ZEIKVKNov 26.2007 — Check this!

[code=php]
<?php
$string = '<tag value="1">Name1</tag><tag value="2">Name2</tag><tag value="n">Namen</tag>';

$array = array();

preg_match_all("|<[^>]+>(.*)</[^>]+>|U", $string, $out, PREG_PATTERN_ORDER);


foreach ($out[1] as $key=>$o)
$array['name'][$key] = $o;

foreach ($out[0] as $key=>$o)
{
preg_match_all('/<tag value="([^"]+)"/i', $o, $out, PREG_PATTERN_ORDER);
$array['value'][$key] = $out[1][0];
}

print_r($array);

?>

[/code]


Cheers!
×

Success!

Help @PHPycho spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...