I am trying to return the user_id if value matches with any comma separated value, I am using strpos but I don't why is it not working with 3rd case:
To Compare: (This value is stored in $myArray variable)
Array
(
[0] => cloud
[1] => ai
[2] => test
)
Compare with: (This value is stored in $array_meta_values variable)
Array
(
[0] => Array
(
[tags] => cloud,ai
[user_id] => 1
)
[1] => Array
(
[tags] => cloud,ai,test
[user_id] => 108
)
[2] => Array
(
[tags] => storage,backup,ai
[user_id] => 101
)
)
function searchForId($meta_value, $array)
{
foreach ($array as $key => $val) {
if (strpos($val['tags'], $meta_value)) {
return $val['user_id'];
}
}
}
foreach ($myArray as $usertags) {
$userids[] = searchForId($usertags, $array_meta_values);
}
print_r($userids);
Getting this Output:
Array
(
[1] => 1
[2] => 108
)
It was supposed to add 101 as third element in output array but don't know why it is not working.
Any help appreciated.
Hi so I tried to replicate you question,
$existing = ['cloud', 'ai', 'test'];
$checker = [
array(
"tags" => "cloud,ai",
"user_id" => 1
),
array(
"tags" => "cloud,ai,test",
"user_id" => 108
),
array(
"tags" => "storage,backup,ai",
"user_id" => 101
)
];
function searchForId($meta_value, $array)
{
$ret_array = [];
foreach ($array as $val) {
$et = explode(",", $val['tags']);
if (in_array($meta_value, $et)) {
$ret_array[] = $val['user_id'];
}
}
return $ret_array;
}
foreach ($existing as $usertags) {
$userids[$usertags][] = searchForId($usertags, $checker);
}
echo "<pre>";
print_r($userids);
Is this what you looking for?
Your question is quite unclear, but I'll try with this one:
<?php
$myArray = array('cloud', 'ai', 'test');
$array_meta_values = array(
array(
'tags' => 'cloud,ai',
'user_id' => 1,
),
array(
'tags' => 'cloud,ai,test',
'user_id' => 108,
),
array(
'tags' => 'storage,backup,ai',
'user_id' => 101,
),
);
$userids = [];
foreach ($myArray as $value) {
foreach ($array_meta_values as $array) {
$arrayValues = explode(',', $array['tags']);
if (in_array($value, $arrayValues)) {
$userids[$value][] = $array['user_id'];
}
}
}
echo '<pre>';
print_r($userids);
Output:
Array
(
[cloud] => Array
(
[0] => 1
[1] => 108
)
[ai] => Array
(
[0] => 1
[1] => 108
[2] => 101
)
[test] => Array
(
[0] => 108
)
)
Related
I am trying to compare the keys of an array with values of another array. In case of greater values of the current key, I would like to push the value into a new array. Then I want to insert all the collected values for that specific key into a DB table.
These are my input arrays:
$productlist = [result] => Array
(
[0] => Array
(
[configoption1] => 2M
[id] => 96
)
[1] => Array
(
[configoption1] => 5M
[id] => 97
)
[2] => Array
(
[configoption1] => 15M
[id] => 98
)
[3] => Array
(
[configoption1] => 30M
[id] => 99
)
)
$myplans = Array
(
[2M] => Array
(
[price] => 10
)
[5M] => Array
(
[price] => 10
)
[15M] => Array
(
[price] => 10
)
[30M] => Array
(
[price] => 10
)
)
The following is my sample code:
$upgradelist = array()
foreach ($myplans as $plan => $data) {
foreach($productlist['result'] as $key=>$value){
if($plan == 'ENTERPRISE'){
//no higher plans than Enterprise
}else{
$plan1 = (int)substr_replace($plan, "", -1);
$value['configoption1'] = (int)substr_replace($value['configoption1'], "", -1);
#echo " configconfig=> ".$value['configoption1'];
if($plan > $value['configoption1']){
$upgrade_product_ids[$plan][] = $value['id'];
}
}
}
//insert upgrade products list
if(!empty($upgradelist)){
foreach($upgradelist as $key => $upgrade_product_id){
#$insert_stmt_upgradeproduct = <insert statement> for each $plan
}
}
}
expected output each time I come out from the foreach loop: > foreach($productlist['result']
$upgradelist = Array
(
[2] => Array
(
[0] => 97 //5M
[1] => 98 //15M
[2] => 99 //30M
)
)
$upgradelist = Array
(
[5] => Array
(
[0] => 98
[1] => 99
)
)
$upgradelist = Array
(
[15] => Array
(
[0] => 99
)
)
$upgradelist = Array
(
[30] => Array
(
)
)
You can solve like below:
foreach($myplans as $key => $plan) {
$data_to_be_inserted = [];
foreach($productlist['result'] as $product) {
if ($key > $product['configoption1']) {
$data_to_be_inserted [] = $product['id'];
}
}
if (count($data_to_be_inserted)) {
//insert into database(expected value will be available here)
}
}
YOu can do like this
<?php
$myArray = [];
$myArray['result'] = array(
array("configoption1" => "2m", "id" => 96),
array("configoption1" => "5m", "id" => 97),
array("configoption1" => "15m", "id" => 98),
array("configoption1" => "30m", "id" => 99)
);
//print_r($myArray);
$myArray2 =[
"2m" => ["price" => 10],
"5m" => ["price" => 10],
"15m" => ["price" => 10],
"30m" => ["price" => 10]
];
$resultArray = [];
foreach($myArray2 as $key => $values) {
$keyNum = (int)$key;
$newTempArray[$keyNum] = [];
foreach($myArray['result'] as $newresult) {
$configoption1Num = (int)$newresult['configoption1'];
if($configoption1Num > $keyNum) {
array_push($newTempArray[$keyNum], $newresult['id']);
}
}
print_r($newTempArray[$keyNum]);
echo "<br>";
}
http://sandbox.onlinephpfunctions.com/code/008c839fb4fad4ed2aceae7c4ed1220579e8e318
I have array based POST like this :
Array
(
[condition] => Array
(
[0] => 1
)
[container] =>
[cleaning] => Y
[owner] => Eagletainer
[last_cargo] => 1
[vessel] =>
[insulation] => 1
[tare] =>
[gross] =>
[capacity] =>
[unit_type] => IMO 1
[date_of_manu] =>
[name_manu] =>
[last25] =>
[cert25] =>
[last5] =>
[cert5] =>
[list2_item_0] => 1
[list2_kondisi_0] => 9
[list3_item_0] => 15
[list3_kondisi_0] => 3
[comments] =>
)
My case is, I want to chunk a lot of those element array into another array for insert_batch in my database.
This is the php code to chunk those array:
public function get_partition($array, $p, $c) {
$partition = array_slice($array, $p);
array_pop($partition);
return $chunk = array_chunk($partition, $c);
}
Now, use it,
$detail = $this->get_partition($this->input->post(), 17, 2);
The result is :
Array
(
[0] => Array
(
[0] => 1
[1] => 9
)
[1] => Array
(
[0] => 15
[1] => 3
)
)
My question in, how to change the key [0] and [1] into another key like [ID] and [CODE_DAMAGE]
I want them looked like this :
Array
(
[0] => Array
(
[ID] => 1
[CODE_DAMAGE] => 9
)
[1] => Array
(
[ID] => 15
[CODE_DAMAGE] => 3
)
)
Re-loop the array and achieve your desired result like this:
$detail = $this->get_partition($this->input->post(), 17, 2);
$new_array = array();
$count = 0;
foreach($detail as $row){
$new_array[$count]['ID'] = $row[0];
$new_array[$count++]['CODE_DAMAGE'] = $row[1];
}
If the indexes were already correct you could pass the optional third parameter: http://php.net/array_chunk
<?php
$array = array(0 => array(0 => 123, 1 => 1234), 1 => array(0 => 123, 1 => 1234));
$updatedArray = array();
foreach ($array as $k => $v) {
$updatedArray[$k]['ID'] = $v[0];
$updatedArray[$k]['CODE_DAMAGE'] = $v[1];
}
?>
Try this, I hope this helps.
Try this:
foreach($detail as $key => $value){
if($key == 0){
$detail['ID'] = $value;
unset($detail[$key]);
}
if($key == 1){
$detail['CODE_DAMAGE'] = $value;
unset($detail[$key]);
}
}
Just an example add your array to this code..it will work fine
$main = Array(Array(1,9),Array(15,3));
$b = array('ID', 'CODE_DAMAGE');
$new_array = array();
foreach($main as $subarray)
{
$new_array[] = array_combine($b, $subarray);
}
echo'<pre>';print_r($new_array);
I have the following array in php:
[0] => Array(
(
[post_id] => 492,
[user_id] => 1
)
[1] => Array(
(
[post_id] => 501,
[user_id] => 1
)
[2] => Array(
(
[post_id] => 568,
[user_id] => 13
)
[3] => Array(
(
[post_id] => 897,
[user_id] => 13
)
What I want to do, is to delete the ones where the user_id already exists. So the result should look like this:
[0] => Array(
(
[post_id] => 492,
[user_id] => 1
)
[1] => Array(
(
[post_id] => 568,
[user_id] => 13
)
I need an array, in which every user_id only exists one time.
array_unique() doesn't work for this example. Any ideas?
Thanks!
You can loop through and find unique values as you go:
$exists = array();
foreach($items as $key => $item) {
if(!in_array($item['user_id'], $exists)) {
$exists[] = $item['user_id'];
} else {
unset($items[$key]);
}
}
This will unset any arrays that already exist in the $exists array.
Here is the easy function to solve the issue.
$array = Array();
$array[] = Array('post_id'=>492 , 'user_id' => '1');
$array[] = Array('post_id'=>501 , 'user_id' => '1');
$array[] = Array('post_id'=>568 , 'user_id' => '13');
$array[] = Array('post_id'=>897 , 'user_id' => '13');
print_r($array);
print_r(make_unique_by_key($array,'user_id'));
function make_unique_by_key($m_arr , $key) {
$tmp_arr = array();
foreach ($m_arr as &$item) {
if (!isset($tmp_arr[$item[$key]])) {
$tmp_arr[$item[$key]] =& $item;
}
}
$ret_arr = array_values($tmp_arr);
return $ret_arr;
}
I have an array that looks like this:
getting array need to convert array as same key value as 0
foreach($array as $key=>$id){
$consumer_data[]=$this->App_model->get_session($id);
}
print_r($consumer_data);
Array
(
[0] => Array
(
[0] => Array
(
[ConsumerID] => 1
[name] => asdfd
)
[1] => Array
(
[ConsumerID] => 5
[name] => test
)
[2] => Array
(
[ConsumerID] => 3
[name] => test1
)
)
[1] => Array
(
[0] => Array
(
[ConsumerID] => 4
[name] => test4
)
)
i want to implement array like this in same key value as 0
Array
(
[0] => Array
(
[0] => Array
(
[ConsumerID] => 1
[name] => asdfd
)
[1] => Array
(
[ConsumerID] => 5
[name] => test
)
[2] => Array
(
[ConsumerID] => 3
[name] => test1
)
[3] => Array
(
[ConsumerID] => 4
[name] => test4
)
)
I am using PHP. Can anyone point me to a good starting point as to how I should go about doing this?
You can use array_merge():
$new_array[0] = array_merge($array[0], $array[1]);
Where $array is the first array.
SEE DEMO
OR for a more dynamic approach:
$new_array = array(0 => array());
foreach($array as $a) {
$new_array[0] = array_merge($new_array[0], $a);
}
SEE DEMO 2
The simpliest solution is to do it with:
$input = array(
array(
array('ConsumerID' => 1, 'name' => 'asdfd'),
array('ConsumerID' => 5, 'name' => 'test'),
array('ConsumerID' => 4, 'name' => 'test1'),
),
array(
array('ConsumerID' => 4, 'name' => 'test4'),
),
);
$output = array(
array()
);
foreach ($input as $data) {
$output[0] = array_merge($output[0], $data);
}
Try this->
$newArray = array();
foreach($values as $key=>$val){
$newArray [0][$key]=$val;
}
print_r($newArray);
Check this:
<?php
$arr[0] = array(0 => array("ConsumerID" => 1, "name" => "Ni"), 1 => array("ConsumerID" => 2, "name" => "Ab"));
$arr[1] = array(1 => array("ConsumerID" =>5, "name" => "GE"), 1 => array("ConsumerID" => 6, "name" => "DB"));
$new = array();
foreach($arr as $key => $value) {
foreach($value as $innerkey => $innervalue) {
$new[0][] = $innervalue;
}
}
print_r($new);
?>
I have the following array.
array(
[PM-AAA] => Array
(
[codePm] => PM-32249
[codeArt] => Array
(
[0] => 32249
)
[codeArtInFlux] => Array
(
[0] => 123456
)
)
[PM-BBB] => Array
(
[codePm] => PM-32249
[codeArt] => Array
(
[0] => 33270
)
[codeArtInFlux] => Array
(
[0] => 484946
)
)
[PM-CCC] => Array
(
[codePm] => PM-82242
[codeArt] => Array
(
[0] => 82242
[1] => 82245
[2] => 82246
)
[codeArtInFlux] => Array
(
[0] => 5191
[1] => 51949
[2] => 26486
)
)
)
I want keep the array where the "codePm" value is unique. For exemple, in the above array, the "PM-CCC" array will be keep, because the "codePm" is unique, contrary to the "PM-AAA" and "PM-BBB", which share the same "codePm"'s value.
Is it possible to do it with one function ?
As I know, there isn't any function that do it directly, you can try this:
function insert_unique_key($key_name,$elem,&$array) {
//Get all elements with the key
$values=array()
foreach ($array as $ii) {
$values[]=$ii[$key_name];
}
//Check if the value exists
if (in_array($elem[$key_name], $values)===FALSE) {
$array[]=$elem;
}
}
And in your code:
insert_unique_key('codePm',$elem_to_insert,$array_of_elements);
And you have to do it for each element of your array into a new array
--
EDIT: Sorry, this is for insert new unique values into the array, not for obtain the unique values.
Looking at the comments, I think you want only unique values Try this function:
function get_uniques_by_subkey($key_name,$array) {
//Get all elements with the key
$values=array();
foreach ($array as $ii) {
$values[]=$ii[$key_name];
}
//Get the elements that only appeared one time
$count=array_count_values($values);
unset($values);$values=array();
foreach ($count as $key => $n) {
if ($n==1)
$values[]=$key;
}
//Get the values
$out=array();
foreach ($array as $key => $value) {
if (in_array($value[$key_name],$values))
$out[$key]=$value;
}
return $out;
}
You can check the result here: http://codepad.org/QmuoYxsk
Something like this ?
<?
function removeDuplicatesByKey($a, $k) {
$r = array();
$tmp = array();
foreach ($a as $ind => $arr) {
$elem_found = array_search($arr[$k], $tmp);
if ($elem_found === false) {
$tmp[] = $arr[$k];
$r[$ind] = $arr;
} else {
// ok, element found, need to remove both ..
foreach ($r as $index => $r_arr) {
if ($r_arr[$k] == $arr[$k]) {
unset($r[$index]);
}
}
}
}
return $r;
}
$full_arr = array(
'PM-AAA' => array
(
'codePm' => 'PM-32249',
'codeArt' => array(
'0' => 32249
),
'codeArtInFlux' => array(
'0' => 123456
)
)
,
'PM-BBB' => array
(
'codePm' => 'PM-32249',
'codeArt' => array
(
'0' => 33270
),
'codeArtInFlux' => array
(
'0' => 484946
)
)
,
'PM-CCC' => array
(
'codePm' => 'PM-82242',
'codeArt' => array
(
'0' => 82242,
'1' => 82245,
'2' => 82246
),
'codeArtInFlux' => array
(
'0' => 5191,
'1' => 51949,
'2' => 26486
)
)
);
print_r(removeDuplicatesByKey($full_arr, 'codePm'));
?>
Output
Array
(
[1] => Array
(
[codePm] => PM-82242
[codeArt] => Array
(
[0] => 82242
[1] => 82245
[2] => 82246
)
[codeArtInFlux] => Array
(
[0] => 5191
[1] => 51949
[2] => 26486
)
)
)
I made this....on speed mode in my work....you can improve it ;)
$array = array(
'PM-AAA' => array(
'codePm' => 'PM-32249',
'codeArt' => array(32249),
'codeArtInFlux' => array(123456)
),
'PM-BBB' => array(
'codePm' => 'PM-32249',
'codeArt' => array(33270 ),
'codeArtInFlux' => array(484946)
),
'PM-CCC' => array(
'codePm' => 'PM-82242',
'codeArt' => array(82242,82245,82246),
'codeArtInFlux' => array(5191,51949,26486)
)
);
$code_count = array();
$arr2 = array();
foreach($array as $counter) {
$key = $counter['codePm'];
$code_count[] = $key;
}
$arr2 = (array_count_values($code_count));
print_r($arr2); //now i know how many times my code is repeated
while ($code_name = current($arr2)) {
if ($code_name == 1) {
$unique_code = key($arr2);
}
next($arr2);
}
echo $unique_code."</br>"; //i have my unique code
foreach ($array as $key) {
var_dump($key);
if($key['codePm']==$unique_code)
$arr_aux = $key;
}
echo "I have mi array ready with the unique val ;) </br>";
var_dump($arr_aux);
Saludos ;)