trying to learn
trying to learn

Reputation: 129

Merge two flat arrays into a 2d array with predefined keys in each row

I want to achieve this kind of array using PHP.

[
  {
    "url": "https://url1.com",
    "code": "BNXl421s"
  },
  {
    "url": "https://url2.com",
    "code": "BNKpt1L2"
  },
  {
    "url": "https://url3.com",
    "code": "BMwhPRih"
  }
]

While i have these arrays :

$urls = 
  [
    "https://url1.com",
    "https://url2.com",
    "https://url3.com"
  ]

$codes= 
  [
    "BNXl421s",
    "BNKpt1L2",
    "BMwhPRih" 
  ]

I tried writing 2 foreach statements but it duplicates results since I had no idea how to achieve that.

Upvotes: 0

Views: 40

Answers (4)

Nytrix
Nytrix

Reputation: 1139

Here, try this ( Or the demo here ):

function merge($array1,$array2){
    $output = [];
    for($i = 0; $i < count($array1); $i++){
        $output[$array1[$i]] = $array2[$i];
    }
    return $output;
}


var_dump(merge($urls,$codes));

This will output an array like this

Array(
        "https://url1.com" => "BNXl421s",
        "https://url2.com" => "BNKpt1L2",
        "https://url3.com" => "BMwhPRih"
    )

To make your direct wanted result, try this:


function merge($array1,$array2){
    $output = [];
    for($i = 0; $i < count($array1); $i++){
        $output[] = array("url" => $array1[$i], "code" => $array2[$i]);
    }
    return $output;
}
var_dump(merge($urls,$codes));

Same idea, but this output:

array(3) {
  [0]=>
  array(2) {
    ["url"]=>
    string(16) "https://url1.com"
    ["code"]=>
    string(8) "BNXl421s"
  }
  [1]=>
  array(2) {
    ["url"]=>
    string(16) "https://url2.com"
    ["code"]=>
    string(8) "BNKpt1L2"
  }
  [2]=>
  array(2) {
    ["url"]=>
    string(16) "https://url3.com"
    ["code"]=>
    string(8) "BMwhPRih"
  }
}

Try it out here

Upvotes: 1

mister martin
mister martin

Reputation: 6252

Using the exact code you provided as an example:

Edit: updated, since you updated your question

$result = [];
for ($i = 0; $i < 3; $i++) {
    $result[$i] = ['url:' => $urls[$i], 'code:' => $codes[$i]];
}
var_dump($result);

Output:

array(3) {
  [0]=>
  array(2) {
    ["url:"]=>
    string(16) "https://url1.com"
    ["code:"]=>
    string(8) "BNXl421s"
  }
  [1]=>
  array(2) {
    ["url:"]=>
    string(16) "https://url2.com"
    ["code:"]=>
    string(8) "BNKpt1L2"
  }
  [2]=>
  array(2) {
    ["url:"]=>
    string(16) "https://url3.com"
    ["code:"]=>
    string(8) "BMwhPRih"
  }
}

Upvotes: 1

MarcM
MarcM

Reputation: 2251

// Your array definitions

$urls = 
  [
    "https://url1.com",
    "https://url2.com",
    "https://url3.com"
  ];

// Warning! You had $codes[] here!!! Array of arrays? Not necessary I think... 
$codes =  
  [
    "BNXl421s",
    "BNKpt1L2",
    "BMwhPRih"
  ];

$results = array();
$obj = new stdClass();

foreach ($urls as $key => $url) 
{
  $obj->url = $url;
  $obj->code = $codes[$key];
  $results[]=$obj;
}

// Done! You can check it doing:
var_dump($results);

Note: In case both arrays might not have same lenght, add some extra controls.

Upvotes: 1

Steve
Steve

Reputation: 20469

Provided both arrays are the same length, and single dimensional ($codes[] =[...] should read $coded = [...]) then you can just create a single loop, and push the values into a new array, then use json_encode to get the required json output:

$out=[];
foreach($urls as $key=>$url)
    $out[]=['url'=>$url, 'code'=>$codes[$key]];

echo json_encode($out);

If your $codes array is intensionally multidimentional then you would need to access the correct inner array within in, for example:

$out[]=['url'=>$url, 'code'=>$codes[0][$key]];

Upvotes: 1

Related Questions