Reputation: 129
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
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"
)
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
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
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
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