2

i want to know if it's possible to change an array structure, currently i'm receiving this array:

[{"ano":["2004","2006"]},{"ano":["2006",""]},{"ano":["2011",""]},{"ano":["2013",""]}]

I need those dates split one by row like this:

[{"ano":"2004"},{"ano":"2006"},{"ano":"2006"},{"ano":"2011"}]

So, basically i'm think i could clean empty and duplicated values, and then split the array or something?

I'm using PHP and MySQL SELECT to return those values like this:

while($ano = $pegaAno->fetchObject()){
    $ar1 = array("ano" => $ano->inicio);
    $ar2 = array("ano" => $ano->fim);
    $result = array_merge_recursive($ar1, $ar2);
    //print_r($result);
    array_push($return_arr,$result);
}

Any help please?

2
  • Can the array be created in the desired structure from the start? Or does it have to be changed from the array you are showing at the top`? Commented Aug 13, 2015 at 13:36
  • Hi @zedd, right now i'm requesting 2 columns from database, date_start and date_end, so it's coming 2 arrays, i've merged them recusively but it's returning json like the first array above, i need to return date_start and date_end values with the same key and remove duplicates, because i need to display a list of dates avaliable for that query Commented Aug 13, 2015 at 13:38

2 Answers 2

2
while($ano = $pegaAno->fetchObject())
   array_push($return_arr, array("ano" => $ano->inicio), array("ano" => $ano->fim));
$return_arr = array_unique($return_arr);

Perhaps you want something like this? So you can have an array as

0 => [ "ano" => value ]
1 => [ "ano" => value ]
2 => [ "ano" => value ]
//etc...
Sign up to request clarification or add additional context in comments.

5 Comments

You rock man, exactly what i needed, do you know how i can check for duplicates between both arrays and remove them?
Did not worked, maybe needed to remove duplicates before array_push?
@WilliamXavier it actually worked for me, can you show me how did you try?
I've tried it like this: array_push(array_unique($return_arr),array("ano" => $ano->inicio), array("ano" => $ano->fim));
@WilliamXavier Modified the code, use it after the while :)
0

You may also try this code

    $input[0]['anno'] = array("2004","2006");
    $input[1]['anno'] = array("2008","");
    $input[2]['anno'] = array("2002","2006");
    $input[3]['anno'] = array("2004","2013");
    $arr = array();   
    foreach ($input as $value) { 
    foreach ($value as $key =>  $value1) { 
        foreach ($value1 as  $value2) { 
            $arr[] = $value2;
        }   
    } 

    } 
    $resulted = array_filter(array_unique($arr));
    $resulted_array = array();
    foreach ($resulted as $value) { 
        $resulted_array[][$mainkey] = $value;
    }
    print_r($resulted_array);

//RESULT 

Array
(
    [0] => Array
        (
            [anno] => 2004
        )

    [1] => Array
        (
            [anno] => 2006
        )

    [2] => Array
        (
            [anno] => 2008
        )

    [3] => Array
        (
            [anno] => 2002
        )

    [4] => Array
        (
            [anno] => 2013
        )

)

2 Comments

But then i have 2 arrays, should i merge them before?
if array are in same then first merge it .

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.