I have 2 arrays that contain user details, including emails addresses. I would like to create a new array that contain only the user details where email that appear in both arrays. is there any function that do that?
I tried to use "array_intersect_assoc". no good...
$newGroupsArray = array_intersect ($firstArray , $secondArray );
for instance, - in this example only "nathalie" details should be in the new array.
Is there any function that do that?
$firstArray = array( 0 => Array ( 'name' => 'ronen', 'email' => 'ronen$Experts.com' , 'contactID' => 43 ) ,
1 => Array ( 'name' => 'shlomig' , 'email' => 'shlomig$tours.co.il' ,'contactID' => 28 ) ,
2 => Array ( 'name' => 'nathalie', 'email' => 'nathalie$obra.co.il', 'contactID' => 57 )
);
$secondArray = array(0 => Array ( 'name' => 'nathalie', 'email' => 'nathalie$obra.co.il', 'contactID' => 57 ) ,
1 => Array ('name' => 'roi' , 'email' => 'roi$mail.com' ,'contactID' => 99 )
);
You can combine array_values and array_intersect_key to get desired result.
$Array1 = array();
$Array2 = array();
foreach ($firstArray as $value)
{
$Array1[$value['email']] = $value;
}
foreach ($secondArray as $value)
{
$Array2[$value['email']] = $value;
}
$matches = array_values(array_intersect_key($Array1, $Array2));
print_r($matches);
You can also use foreach loops and check the data using an if condition as below.
$matches = array();
foreach($firstArray as $key => $value)
{
foreach($secondArray as $value1)
{
if($value['email'] === $value1['email'])
$matches[] = $value;
}
}
print_r($matches);
Related
I have a multidimensional array with key and value and some key is empty also. Then I want to set a value for internal not empty array.
$oldArray = array("Lexus LS600" => array(),
"Toyota Alphard" => array(),
"Benz S550" => array(0 => array(
"card_no" => "G2FPCBS3",
"travel_date" => "2020-09-10"
"travel_time" => "16:15:00",
"car_id" => 12,
"return_time" => "17:25")),
"BMW X6" => array());
I had this array but I want to set return_time 00:00 all over array. I tried foreach loop but foreach is remove empty array but I want empty array also.
I want this type array:-
$newArray = array("Lexus LS600" => array(),
"Toyota Alphard" => array(),
"Benz S550" => array(0 => array(
"card_no" => "G2FPCBS3",
"travel_date" => "2020-09-10"
"travel_time" => "16:15:00",
"car_id" => 12,
"return_time" => "00:00")),
"BMW X6" => array());
Try this foreach again, I think it will solve your problem if I understood you correctly.
foreach ($arrays as $key => $values) {
if (is_array($values)) {
if (count($values)) {
foreach ($values as $index => $data) {
$arrays[$key][$index]['return_time'] = "00:00";
}
} else {
$arrays[$key] = $values;
}
}
}
It will change return_time to "00:00" and also retain the empty index to your array.
array_walk_recursive() is very suitable for this.
$oldArray = array("Lexus LS600" => array(),
"Toyota Alphard" => array(),
"Benz S550" => array(0 => array(
"card_no" => "G2FPCBS3",
"travel_date" => "2020-09-10",
"travel_time" => "16:15:00",
"car_id" => 12,
"return_time" => "17:25")),
"BMW X6" => array());
$keySearch = "return_time";
$replaceWith = "00:00";
array_walk_recursive(
$oldArray,
function(&$val,$key) use($keySearch,$replaceWith){
if($key == $keySearch) $val = $replaceWith;
}
);
var_export($oldArray);
Output:
array (
'Lexus LS600' =>
array (
),
'Toyota Alphard' =>
array (
),
'Benz S550' =>
array (
0 =>
array (
'card_no' => 'G2FPCBS3',
'travel_date' => '2020-09-10',
'travel_time' => '16:15:00',
'car_id' => 12,
'return_time' => '00:00',
),
),
'BMW X6' =>
array (
),
)
Use two foreach loops to traverse the limited-depth array and make all values modifiable by reference (& before the variable). In doing so, you don't need to create a separate array, just update the input array. It is SUPER easy to read and maintain.
Code: (Demo)
foreach ($array as &$cars) {
foreach ($cars as &$entry) {
if ($entry) {
$entry["return_time"] = "00:00";
}
}
}
var_export($array);
Output:
array (
'Lexus LS600' =>
array (
),
'Toyota Alphard' =>
array (
),
'Benz S550' =>
array (
0 =>
array (
'card_no' => 'G2FPCBS3',
'travel_date' => '2020-09-10',
'travel_time' => '16:15:00',
'car_id' => 12,
'return_time' => '00:00',
),
),
'BMW X6' =>
array (
),
)
This is my form input $data, i want this keep this input.
$data = "39X3,29X5";
this my code convert string to array
$data = explode(",", $data);
$out = array();
$step = 0;
foreach($data as $key=>$item){
foreach(explode('X',$item) as $value){
$out[$key][$step++] = $value;
}
print '<pre>';
print_r($out);
print '</pre>';
result
Array
(
[0] => Array
(
[0] => 39
[1] => 3
)
[1] => Array
(
[2] => 29
[3] => 5
)
)
but i want change the keys and fix this for support query builder class
$this->db->insert_batch('mytable',$out).
Like this.
array
(
array
(
'number' => 39
'prize' => 3
),
array
(
'number' => 29
'prize' => 5
)
)
i try hard and confuse using loop.
So you need to remove inner foreach and put relevant values into array.
foreach($data as $key=>$item){
$exp = explode('X', $item);
$out[$key] = [
'number' => $exp[0],
'prize' => $exp[1]
];
}
Check result in demo
change your foreach loop to the following:
foreach($data as $key=>$item){
$temp = explode('X',$item);
$out[] = ['number' => $temp[0] , 'prize' => $temp[1]];
}
I want to create a list where if its already in the array to add to the value +1.
Current Output
[1] => Array
(
[source] => 397
[value] => 1
)
[2] => Array
(
[source] => 397
[value] => 1
)
[3] => Array
(
[source] => 1314
[value] => 1
)
What I want to Achieve
[1] => Array
(
[source] => 397
[value] => 2
)
[2] => Array
(
[source] => 1314
[value] => 1
)
My current dulled down PHP
foreach ($submissions as $timefix) {
//Start countng
$data = array(
'source' => $timefix['parent']['id'],
'value' => '1'
);
$dataJson[] = $data;
}
print_r($dataJson);
Simply use an associated array:
$dataJson = array();
foreach ($submissions as $timefix) {
$id = $timefix['parent']['id'];
if (!isset($dataJson[$id])) {
$dataJson[$id] = array('source' => $id, 'value' => 1);
} else {
$dataJson[$id]['value']++;
}
}
$dataJson = array_values($dataJson); // reset the keys - you don't nessesarily need this
This is not exactly your desired output, as the array keys are not preserved, but if it suits you, you could use the item ID as the array key. This would simplify your code to the point of not needing to loop through the already available results:
foreach ($submissions as $timefix) {
$id = $timefix['parent']['id'];
if (array_key_exists($id, $dataJson)) {
$dataJson[$id]["value"]++;
} else {
$dataJson[$id] = [
"source" => $id,
"value" => 1
];
}
}
print_r($dataJson);
You should simplify this for yourself. Something like:
<?
$res = Array();
foreach ($original as $item) {
if (!isset($res[$item['source']])) $res[$item['source']] = $item['value'];
else $res[$item['source']] += $item['value'];
}
?>
After this, you will have array $res which will be something like:
Array(
[397] => 2,
[1314] => 1
)
Then, if you really need the format specified, you can use something like:
<?
$final = Array();
foreach ($res as $source=>$value) $final[] = Array(
'source' => $source,
'value' => $value
);
?>
This code will do the counting and produce a $new array as described in your example.
$data = array(
array('source' => 397, 'value' => 1),
array('source' => 397, 'value' => 1),
array('source' => 1314, 'value' => 1),
);
$new = array();
foreach ($data as $item)
{
$source = $item['source'];
if (isset($new[$source]))
$new[$source]['value'] += $item['value'];
else
$new[$source] = $item;
}
$new = array_values($new);
PHP has a function called array_count_values for that. May be you can use it
Example:
<?php
$array = array(1, "hello", 1, "world", "hello");
print_r(array_count_values($array));
?>
Output:
Array
(
[1] => 2
[hello] => 2
[world] => 1
)
I am trying to uniformly rename all of my array keys using a foreach loop and unset.
Array before:
Array
( [0] => Array (
[store_nl] => Store One
[id_nl] => 123456
[title_nl] => Product One
[price_nl] => $9.00 )
[1] => Array (
[store_ds] => Store Two
[id_ds] => 789012
[title_ds] => Product Two
[price_ds] => $8.00 )
)
foreach using unset:
if(isset($data)){
foreach ( $data as $k=>$v )
{
//Store One (ds)
$data[$k]['Store'] = $data[$k]['store_ds'];
$data[$k]['ItemID'] = $data[$k]['id_ds'];
$data[$k]['Description'] = $data[$k]['title_ds'];
$data[$k]['Price'] = $data[$k]['price_ds'];
unset($data[$k]['store_ds']);
unset($data[$k]['id_ds']);
unset($data[$k]['title_ds']);
unset($data[$k]['price_ds']);
//Store Two (nl)
$data[$k]['Store'] = $data[$k]['store_nl'];
$data[$k]['ItemID'] = $data[$k]['id_nl'];
$data[$k]['Description'] = $data[$k]['title_nl'];
$data[$k]['Price'] = $data[$k]['price_nl'];
unset($data[$k]['store_nl']);
unset($data[$k]['id_nl']);
unset($data[$k]['title_nl']);
unset($data[$k]['price_nl']);
}
}
Array after:
Array
( [0] => Array (
[Store] => Store One
[ItemID] => 123456
[Description] => Product One
[Price] => $9.00 )
[1] => Array (
[Store] =>
[ItemID] =>
[Description] =>
[Price] => )
)
All of the array keys have been changed, but some of the data is now gone? Can someone please tell me a better way to do this without data loss?
The following will do what you deed:
$myArray = array(
array(
'store_ni' => 'Store One',
'id_ni' => 123456,
'title_ni' => 'Product One',
'price_ni' => '$9.00'
),
array(
'store_ds' => 'Store Two',
'id_ds' => 789012,
'title_ds' => 'Product Two',
'price_ds' => '$8.00'
)
);
$newKeys = array('Store', 'ItemID', 'Description', 'Price');
$result = array();
foreach($myArray as $innerArray)
{
$result[] = array_combine(
$newKeys,
array_values($innerArray)
);
}
array_combine() combines the first array you pass to it and assign it as the keys of the returned array and the second array you pass it as the values of that array.
Use array_flip() to flip between the keys and values, then it'll be easy to run over the values and "flip" the array again (back to its original state). It should be something like:
$tmp_arr = array_flip($arr);
$fixed_arr = array();
foreach($tmp_arr as $k => $v){
if($val == "store_nl"){
$fixed_arr[$k] = "Store";
}
// etc...
}
// and now flip back:
$arr = array_flip($fixed_arr);
The arrays should have the same number of elements, but I think that's the case here.
$data = [[
'store_nl' => 'Store One',
'id_nl' => 123456,
'title_nl' => 'Product One',
'price_nl' => '$9.00'
], [
'store_ds' => 'Store Two',
'id_ds' => 789012,
'title_ds' => 'Product Two',
'price_ds' => '$8.00'
]];
$keys = ['Store', 'ItemID', 'Description', 'Price'];
$data = [
'nl' => array_combine($keys, $data[0]),
'ds' => array_combine($keys, $data[1])
];
Recursive php rename keys function:
function replaceKeys($oldKey, $newKey, array $input){
$return = array();
foreach ($input as $key => $value) {
if ($key===$oldKey)
$key = $newKey;
if (is_array($value))
$value = replaceKeys( $oldKey, $newKey, $value);
$return[$key] = $value;
}
return $return;
}
Array
(
[0] => Array
(
[uid] => 43543534
)
)
I'm trying to get output as [0] => [43543534]
I tried foreach() but I'm getting string as output
Update How do i find max value now in this?
Why don't you have only 1 dimensional array array('0' => 43543534), if you have only 'uid' in the second one
foreach ($yourArray as $key => $val) {
echo '['.$key.'] => ['.$val['uid'].']<br />';
}
$var = array( '0' => array ( 'uid' => '43543534' ) );
foreach($var as $arr):
echo $arr['uid'];
endforeach;
Your question is very unclear, but here are two ways to accomplish that using the original array:
$array = array( '0' => array ( 'uid' => '43543534' ) );
$result[0] = $array[0]['uid];
or with foreach
$array = array( '0' => array ( 'uid' => '43543534' ) );
foreach($array as $a){
$result[] = $a['uid'];
}