1

i have to send this array with nested arrays

            $tags=[];
            foreach ($importedTags->tags as $key => $value) {
                $tags[]=array(
                    "name"=>$value->getName(),
                    "id"=>$value->getId(),
                );
            }

the array (if there were 3 importedTags) may look like this :

array(3) { 
   [0]=> array(2) { ["name"]=> string(14) "foo" ["id"]=> string(7) "3375095" } 
   [1]=> array(2) { ["name"]=> string(12) "bar" ["id"]=> string(7) "3378925" } 
   [2]=> array(2) { ["name"]=> string(8) "foobar" ["id"]=> string(7) "3405555" } 
}

as a GET query parameter on a url

i tryed

            $params=array(
                "pageId"=>$pageId,
                "tags"=>urlencode(implode(",", $tags))
            );

return $this->redirect($this->generateUrl('my_route.create_sth',$params));

but when i parse it like

    $tags=urldecode($request->query->get('tags'));
            $tags=explode(",",$tags);

it returns to

array(3) {  
     [0]=> string(5) "Array" 
     [1]=> string(5) "Array" 
     [2]=> string(5) "Array" 
}

and i know why its like this, and this makes sense

but i´m a php newbie, so how can i solve this and get a string representation like i tryed to get with implode and explode to get array back with such nested arrays?

6
  • They aren't nested arrays. They're strings (as you can see from var_dump) with the string content Array. You're most likely trying to cast an array to a string at some point. Commented Mar 22, 2014 at 11:18
  • 1
    Note $tags is a bi-dimensional array. You have $tags[$item]["name"] and $tags[$item]["id"], so a mere implode does not convert it into flat string. What value do you want to send on GET, name or id? Commented Mar 22, 2014 at 11:19
  • @h2ooooooo hmn sry for bad wording, updated first line of text @ fedorqui i need the whole array with its nested arrays Commented Mar 22, 2014 at 11:22
  • (Note you cannot use @ for two people in the same comment, just first one gets notified) How do you want the string to look like? Commented Mar 22, 2014 at 11:24
  • when i parse the queryparameter i want to have the full array with its access, so im looking for a way to string-representate the array with nested array, i updated the answer with var_dump($tags) before they get send Commented Mar 22, 2014 at 11:27

1 Answer 1

2

change generating of params to:

$params=[];
foreach ($importedTags->tags as $key => $value)
{
    $params[]=$value->getName() .'|'. $value->getId();
}

$this->redirect($this->generateUrl('my_route.create_sth',$params));

on the other site do:

$tags=[];
foreach ($params as $param)
{
    $paramArray = split('|', $param);
    $tags[$paramArray[0]]=$paramArray[1];
}

and you have nested array.

good luck

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

Comments

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.