0

I am creating an array inside a foreach loop. The array consists of the values that i am fetching from google play store. What i want to do is combine the different arrays to a single dimensional array. The outer arrays doesn't have keys. How can i combine all the sub arrays without a key.The result that i showed is same array not different arrays so this question is different. Please help me. Thanks in advance.

php function for fetching values

function gplaystore_get_single_product_card_link($link){
    $final_array=array();
    $html = file_get_contents($link);   
    $gplay_doc = new DOMDocument();
    libxml_use_internal_errors(TRUE);
    if(!empty($html)){
        $gplay_doc->loadHTML($html);
        libxml_clear_errors();
        $gplay_xpath = new DOMXPath($gplay_doc);
        $gplay_row = $gplay_xpath->query('//a[@class="card-click-target"]/@href');

        if($gplay_row->length > 0){
            foreach($gplay_row as $key=>$val){
                $final_array[] = 'https://play.google.com'.$val->nodeValue;
            }
        }       
    }


    echo '<pre>';
    print_r($final_array);
    echo '</pre>';
}

result i am getting

Array
(
    [0] => https://play.google.com/store/apps/details?id=com.depthapps.stylish.name.maker
    [1] => https://play.google.com/store/apps/details?id=com.depthapps.stylish.name.maker
    [2] => https://play.google.com/store/apps/details?id=com.depthapps.stylish.name.maker
    [3] => https://play.google.com/store/apps/details?id=com.depthapps.stylish.name.maker
    [4] => https://play.google.com/store/apps/details?id=codeadore.textgram
}
Array
(
    [0] => https://play.google.com/store/apps/details?id=com.autoscout24
    [1] => https://play.google.com/store/apps/details?id=com.autoscout24
    [2] => https://play.google.com/store/apps/details?id=com.autoscout24
    [3] => https://play.google.com/store/apps/details?id=com.autoscout24
    [4] => https://play.google.com/store/apps/details?id=ru.gibdd_pay.app
    [5] => https://play.google.com/store/apps/details?id=ru.gibdd_pay.app
}
Array
(
    [0] => https://play.google.com/store/apps/details?id=com.northpark.beautycamera
    [1] => https://play.google.com/store/apps/details?id=com.northpark.beautycamera
    [2] => https://play.google.com/store/apps/details?id=com.northpark.beautycamera
    [3] => https://play.google.com/store/apps/details?id=com.northpark.beautycamera
    [4] => https://play.google.com/store/apps/details?id=in.letstartup.GoraTips
    [5] => https://play.google.com/store/apps/details?id=in.letstartup.GoraTips
}

the actual result that i want

 Array
    (
        [0] => https://play.google.com/store/apps/details?id=com.depthapps.stylish.name.maker
        [1] => https://play.google.com/store/apps/details?id=com.depthapps.stylish.name.maker
        [2] => https://play.google.com/store/apps/details?id=com.depthapps.stylish.name.maker
        [3] => https://play.google.com/store/apps/details?id=com.depthapps.stylish.name.maker
        [4] => https://play.google.com/store/apps/details?id=codeadore.textgram
        [5] => https://play.google.com/store/apps/details?id=com.autoscout24
        [6] => https://play.google.com/store/apps/details?id=com.autoscout24
        [7] => https://play.google.com/store/apps/details?id=com.autoscout24
        [8] => https://play.google.com/store/apps/details?id=com.autoscout24
        [9] => https://play.google.com/store/apps/details?id=ru.gibdd_pay.app
        [10] => https://play.google.com/store/apps/details?id=ru.gibdd_pay.app
        [11] => https://play.google.com/store/apps/details?id=com.northpark.beautycamera
        [12] => https://play.google.com/store/apps/details?id=com.northpark.beautycamera
        [13] => https://play.google.com/store/apps/details?id=com.northpark.beautycamera
        [14] => https://play.google.com/store/apps/details?id=com.northpark.beautycamera
        [15] => https://play.google.com/store/apps/details?id=in.letstartup.GoraTips
        [16] => https://play.google.com/store/apps/details?id=in.letstartup.GoraTips
    }
Nitin Johnson
  • 330
  • 2
  • 16
  • declare $final_array variable globally and use – Rakesh Sojitra Nov 17 '16 at 06:38
  • @RakeshSojitra i have already defined it outside the loop please see the question. $final_array=array(); – Nitin Johnson Nov 17 '16 at 06:39
  • Your current output shows that you create your arrays inside a loop, so simply merge them into a single array with `array_merge()`. – Rizier123 Nov 17 '16 at 06:41
  • 1
    Are you calling gplaystore_get_single_product_card_link() in loop ? – Rakesh Sojitra Nov 17 '16 at 06:41
  • @Rizier123 they are not different arrays that is the issue if they were different i could merged them easily but it returning inside of another array – Nitin Johnson Nov 17 '16 at 06:44
  • 1
    @RakeshSojitra yes i am calling it inside a loop – Nitin Johnson Nov 17 '16 at 06:45
  • @NitinJohnson What do you mean with *it returning inside of another array*? You have to show us how you call your function. Based on your shown output you call your function inside a loop, so you can just merge the array inside the loop. – Rizier123 Nov 17 '16 at 06:45
  • 1
    Then remove `$final_array=array();` from function. globally declare $final_array variable and use `global $final_array;` in function – Rakesh Sojitra Nov 17 '16 at 06:47
  • @Rizier123 i mean the $final_array variable is being created inside the loop and i want the different arrays inside it to be merged to a single dimensional array – Nitin Johnson Nov 17 '16 at 06:47
  • @NitinJohnson As I already said, you call your function multiple times inside a loop, so just return the array from the function and merge the returned arrays from the function in the loop into a single one. – Rizier123 Nov 17 '16 at 06:49

0 Answers0