Creating a string from arrays

2 views
Skip to first unread message

Ovidiu

unread,
Aug 20, 2010, 6:44:19 AM8/20/10
to Professional PHP Developers
This may seem very easy, novice, quick and painless to do, but I think
I'm going crazy! If there is someone out there that can help me with a
problem regarding some arrays I would be his/her worshiper.

My problem is this:

I have multi-dimensional array:
$tag[0][0][0]='France';
$tag[0][0][1]='Spain';
$tag[0][1][0]='fun';
$tag[0][1][1]='vacation';

What I want to output is this:

France fun;
France vacation;
Spain fun;
Spain vacation;


In case you wonder, I'm trying to create some tags from some words.
The problem is that I don't know how much deeper the array goes, some
tags could have 10 words or more because it's dynamic, I know that
it's not healthy to have so many words in one tag but I must prepare
for this. So one tag could be like this:

$tag[0][0][0]='France';
$tag[0][0][1]='Spain';
$tag[0][1][0]='fun';
$tag[0][1][1]='vacation';

[...]

$tag[0][10][0]='restaurants';
$tag[0][10][1]='beaches';


Please reply if you have been through this problem before or please
send me some links for such a problem.

Cheers!






Jeff

unread,
Aug 20, 2010, 11:24:24 AM8/20/10
to Professional PHP Developers
I have worked with 'deep' multi-dimensional arrays ... not for tags,
but in situations where you just don't know what you're going to be
working with ... the trick is to set up foreach() loops to walk
through the 'levels', with something like if($tag[0][0] == array())),
you do the walk through, else you add to the tag strings.

If(you need more help){reply to me}else{good luck!}

Jeff

Robert Gonzalez

unread,
Aug 20, 2010, 12:04:58 PM8/20/10
to professi...@googlegroups.com
Recursion comes to mind in this case, though for what you are looking to display it might not work the way you want since there is no real relationship between IDs of your array.

And for the record, foreach() isn't always the best way to walk large arrays. Extra credit points if you can tell us why.

--
This group is managed and maintained by the development staff at 360 PSG. An enterprise application development company utilizing open-source technologies for todays small-to-medium size businesses.

For information or project assistance please visit :
http://www.360psg.com

You received this message because you are subscribed to the Google Groups "Professional PHP Developers" group.
To post to this group, send email to Professi...@googlegroups.com
To unsubscribe from this group, send email to Professional-P...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/Professional-PHP

sc_mach

unread,
Aug 20, 2010, 1:42:34 PM8/20/10
to Professional PHP Developers
Oo Oo I know!

foreach() creates a copy of the array to loop over. If your array is
massive, memory usage can become an issue.

Maybe I haven't had enough Rockstar yet, but I don't see how
"if($tag[0][0] == array()))" will work... wouldn't you want to use
something like "if(isset($tag[0][0]) && is_array($tag[0][0]))"?

Recursion sounds like where I would start for this problem, but it is
hard to really go much further since I don't understand how you are
joining array members. Is it that each $tag[0][0] member gets joined
to "all" other array members?



On Aug 20, 9:04 am, Robert Gonzalez
<robert.anthony.gonza...@gmail.com> wrote:
> Recursion comes to mind in this case, though for what you are looking to
> display it might not work the way you want since there is no real
> relationship between IDs of your array.
>
> And for the record, foreach() isn't always the best way to walk large
> arrays. Extra credit points if you can tell us why.
>

Ovidiu Alexa

unread,
Aug 20, 2010, 5:46:41 PM8/20/10
to professi...@googlegroups.com
ok, just ignore the tag for the moment.

I have these 3 arrays:


$a[0]='A';
$a[1]='B';

$b[0]='1';
$b[1]='2';

[...]

$n[0]='X';
$n[1]='Z';





please tell me a method that parses each array and 'glues' each value on a string like this:


result: $a[0].' '.$b[0].[...].$n[0]; // result A1[...]X
result: $a[0].' '.$b[0].[...].$n[1]; // result A1[...]Z

result: $a[0].' '.$b[1].[...].$n[0]; // result A2[...]X
result: $a[0].' '.$b[1].[...].$n[1]; // result A2[...]Z

result: $a[1].' '.$b[0].[...].$n[0]; // result B1[...]X
result: $a[1].' '.$b[0].[...].$n[1]; // result B1[...]Z

result: $a[1].' '.$b[1].[...].$n[0]; // result B2[...]X
result: $a[1].' '.$b[1].[...].$n[1]; // result B2[...]Z



Please note: I know how many arrays there are ($a, $b, $c,....$n); but this would require a method which I think it's best....


I must make a big array with all the possible words and all the possible values. Then I must make a function which call's itself then returns an array that is parsed as a 'child'. after making this 'tree' I will make another function that recursively 'glues' all the values in a new array. 


this would be like this: 

$tag[0]=$a;
$tag[1]=$b;
[...]
$tag[$n]=$n;

function call_me($array)
    {
    global $tag;
    global $array;
    global $result;
    for($x=0; $x<count($tag); $x++)
       {
        for($y=0; $y<count($tag[$x]); $y++)
            {
            $array[$x][$y]=$tag[$x][$y];
            $result=call_me($array[$x][$y]);
            
            }
       }
    return $result;
    }

$fix_me=call_me($tag);

print_r($tag);

or something like this... I don't know... I'm crazy already, this is not my original ideea... I tried to reverse what another guy did here http://kevin.vanzonneveld.net/techblog/article/convert_anything_to_tree_structures_in_php/

Cheers!

Jolly

unread,
Aug 20, 2010, 6:20:14 PM8/20/10
to Professional PHP Developers
I would never go with recursion in this case.. If it can be done
easily with foreach, then please do it.. Recursion will be more memory
intensive in this case and if not properly tested, then............
copying once is better than copying all the time..

Also, why are you using 3D arrays?
Also, you can do a cross join on the table in which these words are
stored.. Just get the ids.. that way will be much faster than pulling
down all the words and then doing the cross join in your application.
> original ideea... I tried to reverse what another guy did herehttp://kevin.vanzonneveld.net/techblog/article/convert_anything_to_tr...
>
> Cheers!

Subir Jolly

unread,
Aug 20, 2010, 6:21:15 PM8/20/10
to Professional PHP Developers
Correction:
By ids i mean the words..

Robert Gonzalez

unread,
Aug 20, 2010, 6:43:07 PM8/20/10
to professi...@googlegroups.com
I'm still not understanding what you're doing. I know you are NOT creating a hierarchy since it looks like all elements in the lowest level array are members of the all the parent levels of the array. I wish I could understand what you're after because at this point it is eluding me.

Heath Aiken

unread,
Aug 20, 2010, 7:44:38 PM8/20/10
to professi...@googlegroups.com

I think he wants to output strings of every possible combination of his array values.

On Aug 20, 2010 6:43 PM, "Robert Gonzalez" <robert.anth...@gmail.com> wrote:

I'm still not understanding what you're doing. I know you are NOT creating a hierarchy since it looks like all elements in the lowest level array are members of the all the parent levels of the array. I wish I could understand what you're after because at this point it is eluding me.



On Fri, Aug 20, 2010 at 3:21 PM, Subir Jolly <subir...@gmail.com> wrote:
>
> Correction:

> By i...

--

This group is managed and maintained by the development staff at 360 PSG. An enterprise application ...

Ovidiu Alexa

unread,
Aug 23, 2010, 3:01:12 AM8/23/10
to professi...@googlegroups.com
Right ON!

This group is managed and maintained by the development staff at 360 PSG. An enterprise application development company utilizing open-source technologies for todays small-to-medium size businesses.

Sam Petrenko

unread,
Aug 20, 2010, 7:18:41 AM8/20/10
to Professional PHP Developers
don't really understand what you need, please explain and i'll try
help you.
Reply all
Reply to author
Forward
0 new messages