melkawakibi
melkawakibi

Reputation: 881

Combine one value from an array to all the values of another array php

For the building of a url query I need to combine one value(key) of an array to all the values(value) of another array. Each combined key => value needs to be added to an array.

The problem here is that I can combine the values of the two arrays in two foreach statements, but it creates for every instance a new array.

Update Having duplicates is impossible so mine initial output is correct.

$array1 array(

[0] => music

[1] => product

)

$array2 array(

[0] => '));waitfor delay '0:0:TIME'--1

[1] => '[TAB]or[TAB]sleep(TIME)='

)

public static function create_combined_array($array1, $array2)
{
    $newArray = array();

    foreach ($array1 as $key){

      //key = [music]

      foreach ($array2 as $value) {

        //one of the values is = '));waitfor delay '0:0:__TIME__'--1

        array_push($newArray, [$key => $value]);

       }
    }


    return $newArray;
}

Implementation

$query_array = Utils::create_combined_array($params, $payload_lines);
print_r($query_array);

$query = http_build_query($query_array);

$this->url = $baseUrl . '?' . $query; 

Build query output

protocol://localhost:8000?music='));waitfor delay '0:0:TIME'--1

Sample output

    [54] => Array
        (
            [music] => ));waitfor delay '0:0:__TIME__'--[LF]1

        )

    [55] => Array
        (
            [music] => '));waitfor delay '0:0:__TIME__'--1

        )

    [56] => Array
        (
            [music] => '));waitfor delay '0:0:__TIME__'--[LF]1

        )

    [57] => Array
        (
            [music] => "));waitfor delay '0:0:__TIME__'--1

        )

What I wanted to achieve is impossible in PHP.

Example duplicates

Array(

  [music] => "));waitfor delay '0:0:__TIME__'--1
  [music] => '/**/or/**/benchmark(10000000,MD5(1))#1

)

Upvotes: 0

Views: 171

Answers (1)

Albert221
Albert221

Reputation: 7092

Use code below:

public static function create_combined_array($array1, $array2)
{
    $newArray = array();

    foreach ($array1 as $key){
        foreach ($array2 as $i => $value) {
            $newArray[$i][$key] = $value;
         }
    }

    return $newArray;
}

The key line is $newArray[$i][$key] = $value;. It appends an array to the $newArray at $i index which is the index of your second array $array2.

Upvotes: 1

Related Questions