Custom array concat with the same key in PHP - php

I have 2 arrays
$array1[01] = "audi||opel";
$array1[02] = "bmw||ford";
and the second one
$array2[01] = "blue||yellow";
$array2[02] = "white||red";
I would like to merge the two arrays and get my array in this format:
$array_custom[01] = "audi||opel||blue||yellow";
$array_custom[02] = "bmw||ford||white||red"";
How can I do that ? Thx in advance

Use array_map:
$array_custom = array_map(function ($array1, $array2) { return "$array1||$array2"; },
$array1, $array2);
If you want to keep your indexes:
foreach ($array1 as $key => $value) {
$array_custom[$key] = "$value||{$array2[$key]}";
}

Hope the following code is what you're looking for.
$arrCount = count($array1);
for($i=0;$i<=$arrCount;$i++){
$array_custom[$i] = array_merge($array1[$i],$array2[$i]) //Or whatever your preferred way to concatenate this.
}

Try this:
$array1[01] = "audi||opel";
$array1[02] = "bmw||ford";
$array2[01] = "blue||yellow";
$array2[02] = "white||red";
$array_custom[01]=array($array1[01]."||".$array2[01]);
$array_custom[02]=array($array1[02]."||".$array2[02]);
//result will be this
array(1) { [0]=> string(24) "audi||opel||blue||yellow" }
array(1) { [0]=> string(21) "bmw||ford||white||red" }

Related

Retrieve indexes of value in array

Is it possible to search an array for a given value and return all the indexes at which the value was found? So for this array:
["Red","Green","Red","Blue"]
I need
[0,2]
with regard to a search for "Red". Searching for "Yellow" in this case would return an empty array.
You can use like this:
$array = ["Red","Green","Red","Blue"];
$output = array_keys($array, "Red");
The $output will be [0,2]
I think this should work:
$input = ["Red","Green","Red","Blue"];
$x = "Red";
$keys = array_keys(array_filter($input, function ($v) use ($x) { return $v === $x;}));
You can iterate the array with foreach:
foreach($input_arr as $key => $value){
if($value == 'Red'){
needed_key_arr[] = $key;
}
}
Also, if you can have an array of values you what to search use:
$lookup_arr = ['Red', 'Green'];
foreach($input_arr as $key => $value){
if(in_array($value, $lookup_arr)){
needed_key_arr[] = $key;
}
}
$arr = ["Red","Green","Red","Blue"];
$valueToSearchFor = ["Red"];
$keys = array_keys(array_filter($arr, function ($val1) use ($valueToSearchFor) { // filter the first array
return array_filter($valueToSearchFor, function ($val) use ($val1) { // use the first array's value
return $val == $val1; // compare them and then return them
});
}));
var_dump($keys) // array(2) { [0]=> int(0) [1]=> int(2) }
First we filter the array then take the values from the first filter to another filter then we match the arrays and we return them. This works for multiple values too.
$arr = ["Red","Green","Red","Blue"];
$valueToSearchFor = ["Red", "Blue"];
$keys = array_keys(array_filter($arr, function ($val1) use ($valueToSearchFor) {
return array_filter($valueToSearchFor, function ($val) use ($val1) {
return $val == $val1;
});
}));
var_dump($keys) // array(3) { [0]=> int(0) [1]=> int(2) [2]=> int(3) }

Php array value to keys

Hi let's say I have this array
array(2) {
[0]=>
string(9) "name|a-z+"
[1]=>
string(7) "id|0-9+"
}
Now I want a new array (or the same if possible) to be like this:
array(2) {
[name]=>
string(4) "a-z+"
[id]=>
string(4) "0-9+"
}
I think the solution implies explode and array_combine, but I am not good enough, can someone help me?
Thanks in advance.
function convert_my_array($arr){
$out = array();
foreach($arr as $obj){
$data = explode("|", $obj);
$out[$data[0]] = $data[1];
}
return $out;
}
Using the original array called $array here, loop through it set the values to what you want.
$newarray = array();
foreach ($array as $key=>$val) {
list($one, $two) = explode('|', $val);
$newarray[$one] = $two;
}

How to change value of one element to key in array?

How can I make value of one element into key in the same array
from this
[0]=>
array(2) {
["name"]=>
string(7) "segment"
["value"]=>
string(9) "Name Test"
}
to this
["segment"]=> "Name Test"
Try and run each item through a function or foreach loop assigning it as you want.
$res = array();
foreach($data as $item)
{
$res[$item['name']] = $item['value'];
}
Or through a function such as array_walk
$res = array();
array_walk($data, function($item, $key) use (&$res) {
$res[$item['name']] = $item['value'];
});
Simplifying (if you have one row indexed as '0'):
$array = array('0' => array('name'=>'segment'
'value'=>'Name Test'));
$new_array = array();
$new_array[$array[0]['name']] = $array[0]['value'];
print_r($new_array);

How to create an array using array_fill function in PHP?

I want to create an array like below
array(2) {
[0]=>
array(2) {
[0]=>
int(1)
[1]=>
int(0)
}
[1]=>
array(2) {
[0]=>
int(2)
[1]=>
int(0)
}
}
Here first element of the inner array will be incremental and second element will always be 0. The outer array length should be 30. I spent a lot of of time on it but couldn't solve it by my one.
Can any one of you help me ?
Thanks
You could do it using array_map() and range():
$o = array_map(function($a) { return array($a, 0); }, range(1, 30));
Demo
The array_fill() function creates an array where all elements are identical. You're asking for an array where the elements aren't all identical, so it's not something you can create simply by using array_fill()....
$array = array_fill(0, 2, array_fill(0, 2, 0));
array_walk($array, function(&$value, $key) { $value[0] = $key+1; });
Maybe you want something like this?
<?php
function initArray() {
$array = array();
for ($i = 1; $i <= 30; $i++) {
$array[] = array($i, 0);
}
return $array;
}
// now call the initArray() function somewhere you need it
$myFancyArray = initArray();
?>

Create string to put in an IN statement from an array of arrays

I have an array of arrays that look like this:
array(40) {
[0]=>
array(2) {
["id"]=>
string(2) "ta"
["size"]=>
int(2)
[1]=>
array(2) {
["id"]=>
string(2) "tq"
["size"]=>
int(4)
....
I want to be able to get all the sizes in a way that I can do a query like this:
IN (2,4)
so... For each array, get the size key: IN (size,size,size...)
Thanks!
You could do something like this:-
$sizes = implode(',', array_map(function($v) { return $v['size']; }, $array));
Then just pass $sizes to your IN query
edit
In response to your comment below, you can use array_unique to remove duplicate sizes, eg:
$sizes = implode(',', array_unique(array_map(function($v) { return $v['size']; }, $array)));
Here you go:
$a = array("id"=>"ta","size"=>2);
$b = array("id"=>"tq","size"=>4);
$c = array($a,$b);
$in = array();
foreach ($c as $key=>$value) {
if(array_key_exists("size", $value)){
$in[] = $value["size"];
}
}
echo implode(",", $in);
$sizes = array();
foreach($array as $value) {
$sizes[] = $value['size'];
}
$query = implode(',', $sizes);
query ..." IN ($query) "..

Categories