I need some help. I need to remove duplicate data as per key value using PHP. I am explaining my code below.
$orginalArr = array(
array("id"=>1,"name"=>"Raj"),
array("id"=>1,"name"=>"Raj"),
array("id"=>2,"name"=>"Ram"),
array("id"=>2,"name"=>"Ram"),
array("id"=>3,"name"=>"Rahul")
);
echo json_encode($orginalArr);
Here I need to remove the data as per id, meaning if id value is same then one set of data will be removed. Please help me.
Just iterate through values and push it to the new array.
Example:
<?php
$orginalArr=array(array("id"=>1,"name"=>"Raj"),array("id"=>1,"name"=>"Raj"),array("id"=>2,"name"=>"Ram"),array("id"=>2,"name"=>"Ram"),array("id"=>3,"name"=>"Rahul"));
$newArray = array();
foreach ( $orginalArr as $arr )
$newArray[$arr['id']] = array(
'id' => $arr['id'],
'name' => $arr['name']);
echo json_encode(array_values($newArray));
?>
Related
I have two arrays which I wish to combine so that name and value can be side by side.
I have this code:
$mgl = array(
'200101',
'200201',
'200202'
);
$mpro = array(
'Current Account',
'Regular Saving Account',
'Ileya Target'
);
array_push($response, array(
"glno"=>$mgl,
"product"=>$mpro
));
echo json_encode(array("server_response"=> $response));
When I viewed it it appeared this way:
{"server_response":[{"glno":["200101","200201","200202"],"product":["Current
Account","Regular Saving Account","Ileya Target"]}]}
I want it in this format
{"server_response":[
{"glno":"104100","product":"Micro Loans"},
{"glno":"200101","product":"Current Account"},
{"glno":"200201","product":"Regular Saving Account"}
]}
It's easy enough using a foreach loop, using the first array as the start point and (as long as the arrays are the same length) just pick out the same value from the second array...
$response = [];
foreach ( $mgl as $key => $value ) {
$response[] = ["glno" => $value, "product" => $mpro[$key]];
}
echo json_encode(array("server_response"=> $response));
gives..
{"server_response":[{"glno":"200101","product":"Current Account"},
{"glno":"200201","product":"Regular Saving Account"},
{"glno":"200202","product":"Ileya Target"}]}
You could use array_combine(), this would be one solution of many in PHP.
<?php
$mgl=array("Micro Loans","Current Account","Ileya Target");
$mpro=array("104100","200101","200201");
$c=array_combine($mgl,$mpro);
print_r($c);
?>
I have those two arrays that I added to attachments.
"PartnerAffiliateCodeId" from first array and "Id" from second array is our primary key.
"UserAction" must be counted for every unique "PartnerAffiliateCodeId" so in our case it is 5.
Normally I think this must be done by SQL but unfortunately this is a API method that I am receiving so I have to handle it by PHP.
Any ideas about how I can make such join with PHP using these two arrays?
I'm unclear on exactly what you're trying to get at with UserAction, but you could try something like this:
//$array1 = the first array
//$array2 = the second array
array_push($array_1, array(
"DateTime" => "",
"HttpReferer" => "",
"Id" => count($array1),
"PartnerAffiliateCodeId" => $array2["Id"],
"UserAction" => "Click"
));
It sounds like you want to match the ID key to the PartnerAffiliateCodeId in your returned data set.
Without knowing your setup, or bothinging with total optimization here a workable solution which will give you some direction.
function selectPartnerWhere($id=null; $from=array())
{
$codes = array();
foreach($from as $k => $p)
{
if($id == $p['PartnerAffiliateCodeId'])
{
return $from[$k];
}
}
return array();
}
$theData = //your array above
$thePartner = //your partner above
$partnerData = selectPartnerWhere($thePartner['Id'], $theData);
How can i add key value pair in multi-dimentional array without index in php
ex:
<?php
$GLOBALS['app_list_strings']['ip_list']=array (
'192.168.1.51' => 'server1',
// i have to add key and value pair here just like above
);
?>
$GLOBALS['app_list_strings']['ip_list']['10.0.0.1'] = 'server2';
You can add one by one as below :
$GLOBALS['app_list_strings']['ip_list']['192.168.1.51'] = 'server1';
$GLOBALS['app_list_strings']['ip_list']['192.168.1.52'] = 'server2';..... and so on..
Or
You can use foreach to add all at a time as below :
$array_server_ips = array(
'192.168.1.51'=>'server1',
'192.168.1.52'=>'server2',
'192.168.1.53'=>'server3',
'192.168.1.54'=>'server4',
'192.168.1.55'=>'server5',
'192.168.1.56'=>'server6',
'192.168.1.57'=>'server7'
);
foreach($array_server_ips as $key=>$value){
$GLOBALS['app_list_strings']['ip_list'][$key] = $value;
}
the code above is
$GLOBALS['app_list_strings']['ip_list']['192.168.1.51'] = 'server1';
so do this
two examples of how to add two your array
$GLOBALS['app_list_strings']['ip_list']['152.124.25.25'] = 'server x';
$GLOBALS['app_list_strings']['ip_list']['152.100.25.25'] = 'server r';
or
$GLOBALS['app_list_strings']['ip_list']=array ( '192.168.1.51' => 'server1', '152.100.25.25' => 'server x' );
I'm attempting to add a couple of columns to a multidimensional PHP array inside a loop.
Inside the loop I currently have this:
$html[]['strongsNum'] = $strongsCode;
$html[]['wordNum'] = $wordNumber;
However, because I'm not setting the index manually, it creates two separate entries for the two. How can I make it add the two columns to the one entry / row of the array?
try:
$html[] = array(
'strongsNum' => $strongsCode,
'wordNum' => $wordNumber,
);
$html[] = array(
'strongsNum' => $strongsCode,
'wordNum' => $wordNumber
);
If you don't want to use the array(key => value) syntax:
After adding the initial 'strongsNum', you can re-access the last member of your array by using count($myArray)-1 as the index.
$html[]['strongsNum'] = $strongsCode;
$html[count($html) - 1]['wordNum'] = $wordNumber;
I thought this would be simple but I cant seem to get it to work. All I want to do is to add a value into a userdata array. If a value is already in the viewed_news_items array I do not want to replace it.
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => true,
'viewed_news_items' => array()
);
$this->session->set_userdata($data);
insert value into viewed_news_items array
$a = array($desk_id => $desk_id);
$this->session->set_userdata('viewed_news_items', $a);
You're using $desk_id as both the key and value, meaning unless you already have the value of $desk_id, you won't be able to look it up in the array.
Instead of this:
$a = array($desk_id => $desk_id);
You probably wanted to do this:
$a = array('desk_id' => $desk_id);
That is, use the string 'desk_id' as the key whose corresponding value is $desk_id.
Is there a way to have the 'desk_id' just as an auto number so each time the code is executed another value is added instead of replacing 'desk_id'?
You can push items onto the end of an array via array_push($array, $value) or $array[] = $value. PHP will automatically assign the next numeric ID as the index for the new array element.
In your current scenario, you'll have to pull the existing list of IDs out of the session, append an ID to it, and then put it back into the session:
# pull the existing IDs out of the session
$viewed_news_items = $this->session->userdata('viewed_news_items');
# on first load, the array may not be initialized
if (!is_array($viewed_news_items))
$viewed_news_items = array();
# append $desk_id to the list of viewed items
$viewed_news_items[] = $desk_id;
# put the new list back into the session
$this->session->set_userdata('viewed_news_items', $viewed_news_items);
invoke the type of data you retrieved from session
and use it as you want.
$ID = 123;
$data = (array)$this->session->userdata("session_name");
$data[] = $ID;
$this->session->set_userdata("session_name",$data);