/    Sign up×
Community /Pin to ProfileBookmark

php associative array parse issue

Hello everybody,

I have a quick php array issue:

I want parse the following array:

[CODE] Array ( [Heading1] => Someheading [0] => Array ( [0] => stdClass Object ( [par] => 3 [content] => hello world [id] => 1 ) ) )
[/CODE]

I have tried a foreach loop but only returns ‘heading1’, should there be a counter within this loop? ?

note: the array can have more than one heading.

thanks in advance

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@CharlesAug 09.2011 — I think that you mean by "parse" something different from what I mean. What [i]exactly[/i] are you trying to accomplish?
Copy linkTweet thisAlerts:
@guest2478authorAug 09.2011 — I think that you mean by "parse" something different from what I mean. What [i]exactly[/i] are you trying to accomplish?[/QUOTE]

by parsing i mean looping and outping each of the array value.


oops i should have showed a example,

i want the array to be outputted in the following format:

[Heading1] Someheading

[par] 3

[content] hello world

[id] 1


Heading1 will be array which contain a array inside it storing the information about that heading, there can be more than one Heading1 arrays that why i need a loop.
Copy linkTweet thisAlerts:
@NogDogAug 09.2011 — Something like this? (I know, it's ugly.)
[code=php]
<?php
/*
Array (
[Heading1] => Someheading
[0] => Array (
[0] => stdClass Object (
[par] => 3
[content] => hello world
[id] => 1
)
)
)
*/

// create test data:
$obj = new stdClass();
$obj->par = 3;
$obj->content = 'hello world';
$obj->id = 1;
$data = array(
'Heading1' => 'Someheading',
0 => array(
0 => $obj
)
);

// extract data:
foreach($data as $key => $value) {
if(is_array($value)) {
foreach($value as $key2 => $val2) {
if(is_object($val2)) {
$vars = get_object_vars($val2);
foreach($vars as $key3 => $val3) {
echo "$key3: $val3<br />n";
}
}
}
}
else echo "$key: $value<br />n";
}
[/code]
Copy linkTweet thisAlerts:
@guest2478authorAug 10.2011 — Something like this? (I know, it's ugly.)
[code=php]
<?php
/*
Array (
[Heading1] => Someheading
[0] => Array (
[0] => stdClass Object (
[par] => 3
[content] => hello world
[id] => 1
)
)
)
*/

// create test data:
$obj = new stdClass();
$obj->par = 3;
$obj->content = 'hello world';
$obj->id = 1;
$data = array(
'Heading1' => 'Someheading',
0 => array(
0 => $obj
)
);

// extract data:
foreach($data as $key => $value) {
if(is_array($value)) {
foreach($value as $key2 => $val2) {
if(is_object($val2)) {
$vars = get_object_vars($val2);
foreach($vars as $key3 => $val3) {
echo "$key3: $val3<br />n";
}
}
}
}
else echo "$key: $value<br />n";
}
[/code]
[/QUOTE]




thanks i will try this out,

but im still trying to work out a more clean solution.
×

Success!

Help @guest2478 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,
)...