0

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

)
6
  • 1
    in the second foreach try to do $newArray[$key][]=$value Commented Jul 27, 2017 at 11:55
  • 2
    array_merge might be worth a look :) Commented Jul 27, 2017 at 11:57
  • So you want to use array1 as key and array2 as value? Commented Jul 27, 2017 at 11:59
  • @AnkitSingh for each value in array1 put all values from value 2 in it Commented Jul 27, 2017 at 12:00
  • @Cr1xus The solution gives me this as output [music] => Array ( [0] => sleep(__TIME__) [1] => 1 or sleep(__TIME__)#1 ) Commented Jul 27, 2017 at 12:06

1 Answer 1

1

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.

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

11 Comments

Yup, Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW)
I works but it generates this [0] => Array ( [0] => Array ( [key] => value ) ). See Question for expected output
Outputs [0] => Array ( [key] => value ) [1] => Array ( [key] => value )
But you know that you can't have two same keys in one array? You can't have [0] => Array ( [key1] => value, [key1] => other_value ). That's impossible in PHP
But with scalar it is possible
|

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.