I want to check value in array if exits then show "Exits" otherwise show "Not in Array"..
1).
I create a array=>
$browse['pro_id'] = $id;
$browse['pro_name'] = $mobile_details[0]['pro_name'];
$browse['pro_brand'] = $mobile_details[0]['pro_brand'];
$browse['pro_price_own'] = $mobile_details[0]['pro_price_own'];
$mob_arr = $browse;
print_r($browse);
//This print array like this..
Array
(
[pro_id] => mob810013034
[pro_name] => Galaxy Y S5360
[pro_brand] => Samsung
[pro_price_own] => 6291
)
2). After this 2 time . i am pusing array in above given array=>
array_push($mob_arr,$browse);
print_r($mob_arr);
//This print array like this...
Array
(
[pro_id] => mob810013034
[pro_name] => Galaxy Y S5360
[pro_brand] => Samsung
[pro_price_own] => 6291
[0] => Array
(
[pro_id] => mobka10013042
[pro_name] => A 1
[pro_brand] => Karbonn
[pro_price_own] => 6000
)
)
I want to check if [pro_id]=mobka10013042 in whole array then continue; else puch array again in $mob_arr
array_push($mob_arr,$browse);
I use in_array but its not working for this...
Please Give me suggestion .....
Try to create a structure like this:
$myarr = Array
(
[0] => Array
(
[pro_id] => mob810013034
[pro_name] => Galaxy Y S5360
[pro_brand] => Samsung
[pro_price_own] => 6291
)
[1] => Array
(
[pro_id] => mobka10013042
[pro_name] => A 1
[pro_brand] => Karbonn
[pro_price_own] => 6000
)
)
Then use pop to retrieve whose values:
$mob_arr = array();
while(!empty($myarr))
{
$temp = array_pop($myarr);
if($temp['pro_id']!='mobka10013042'){
array_push($mob_arr,$temp);
}
}
if(empty($mob_arr)){
//actions when mob_arr variable is empty
} else {
//if not
}
Not sure what the approach is.But this might help to get things started.
$mob_arr = array();
$browse['pro_id'] = 'mob810013034';
$browse['pro_name'] = 'Galaxy Y S5360';
$browse['pro_brand'] = 'Samsung';
$browse['pro_price_own'] = '6291';
array_push($mob_arr,$browse);
$browse['pro_id'] = 'mobka10013042';
$browse['pro_name'] = 'A 1';
$browse['pro_brand'] = 'Karbonn';
$browse['pro_price_own'] = '6000';
array_push($mob_arr,$browse);
if(checkId($mob_arr, 'pro_id', 'mobka10013042')) {
print 'found the value...';
} else {
print 'no can not find the value...';
}
function checkId($arr, $k, $v) {
foreach($arr as $browse) {
if($browse[$k] == $v) {
return true;
}
}
return false;
}
$i=0;
foreach($mob_arr as $row )
{
if(is_array($row))
{
if(in_array('mobka10013042',$row))
{
$i=1;
}
}
}
if($i==1)
{
continue
}
else
{
// push another array
}
Related
I need to create path structure, with the values that I am getting from the array:
Array
(
[machineAttribute] => Array
(
[0] => TTT 1000 S
[1] => TTT 1100 S
)
[technicalAttribute] => Array
(
[0] => Certificate
[1] => Software
)
[languageAttribute] => Array
(
[0] => English
[1] => Spanish
)
)
So, I need to create path that looks like this:
Array
(
[0] => TTT 1000 S/Certificate/English
[1] => TTT 1000 S/Certificate/Spanish
[2] => TTT 1000 S/Software/English
[3] => TTT 1000 S/Software/Spanish
[4] => TTT 1100 S/Certificate/English
[5] => TTT 1100 S/Certificate/Spanish
[6] => TTT 1100 S/Software/English
[7] => TTT 1100 S/Software/Spanish
)
This is a perfect scenario and I was able to solve this with nested foreach:
if (is_array($machineAttributePath))
{
foreach ($machineAttributePath as $machinePath)
{
if (is_array($technicalAttributePath))
{
foreach ($technicalAttributePath as $technicalPath)
{
if (is_array($languageAttributePath))
{
foreach ($languageAttributePath as $languagePath)
{
$multipleMachineValuesPath[] = $machinePath . '/' . $technicalPath . '/' . $languagePath);
}
}
}
}
}
return $multipleMachineValuesPath;
}
But, the problem begins, if the array returns mixed values, sometimes, single value, sometimes array. For example:
Array
(
[machineAttribute] => Array
(
[0] => TTT 1000 S
[1] => TTT 1100 S
[2] => TTT 1200 S
)
[technicalAttribute] => Certificate
[languageAttribute] => Array
(
[0] => English
[1] => Spanish
)
)
Then the array should look like:
Array
(
[0] => TTT 1000 S/Certificate/English
[1] =>TTT 1000 S/Certificate/Spanish
[2] => TTT 1100 S/Certificate/English
[3] => TTT 1100 S/Certificate/Spanish
)
I wrote code, but it is really messy and long and not working properly. I am sure that this could be somehow simplified but I have enough knowledge to solve this. If someone knows how to deal with this situation, please help. Thank you.
You can convert any single value to array just by
(array) $val
In the same time, if $val is already array, it will be not changed
So, you can a little change all foreach
foreach((array) $something....
If you need something simple, you can convert your scalar values to array by writing simple separate function (let name it "force_array"). The function that wraps argument to array if it is not array already.
function force_array($i) { return is_array($i) ? $i : array($i); }
//...
foreach (force_array($machineAttributePath) as $machinePath)
{
foreach (force_array($technicalAttributePath) as $technicalPath)
{
foreach (force_array($languageAttributePath) as $languagePath)
{
$multipleMachineValuesPath[] = $machinePath . '/' . $technicalPath . '/' . $languagePath;
}
}
}
return($multipleMachineValuesPath);
You can do this way also without messing up. Just create the Cartesian of your input array and finally implode the generated array with /. Hope this helps :)
<?php
function cartesian($input) {
$result = array();
while (list($key, $values) = each($input)) {
if (empty($values)) {
continue;
}
if (empty($result)) {
foreach($values as $value) {
$result[] = array($key => $value);
}
}
else {
$append = array();
foreach($result as &$product) {
$product[$key] = array_shift($values);
$copy = $product;
foreach($values as $item) {
$copy[$key] = $item;
$append[] = $copy;
}
array_unshift($values, $product[$key]);
}
$result = array_merge($result, $append);
}
}
return $result;
}
$data = array
(
'machineAttribute' => array
(
'TTT 1000 S',
'TTT 1100 S'
),
'technicalAttribute' => array
(
'Certificate',
'Software'
),
'languageAttribute' => array
(
'English',
'Spanish',
)
);
$combos = cartesian($data);
$final_result = [];
foreach($combos as $combo){
$final_result[] = implode('/',$combo);
}
print_r($final_result);
DEMO:https://3v4l.org/Zh6Ws
This will solve your problem almost.
$arr['machineAttribute'] = (array) $arr['machineAttribute'];
$arr['technicalAttribute'] = (array) $arr['technicalAttribute'];
$arr['languageAttribute'] = (array) $arr['languageAttribute'];
$machCount = count($arr['machineAttribute']);
$techCount = count($arr['technicalAttribute']);
$langCount = count($arr['languageAttribute']);
$attr = [];
for($i=0;$i<$machCount;$i++) {
$attr[0] = $arr['machineAttribute'][$i] ?? "";
for($j=0;$j<$techCount;$j++) {
$attr[1] = $arr['technicalAttribute'][$j] ?? "";
for($k=0;$k<$langCount;$k++) {
$attr[2] = $arr['languageAttribute'][$k] ?? "";
$pathArr[] = implode('/',array_filter($attr));
}
}
}
print_r($pathArr);
This works too!
$results = [[]];
foreach ($arrays as $index => $array) {
$append = [];
foreach ($results as $product) {
foreach ($array as $item) {
$product[$index] = $item;
$append[] = $product;
}
}
$results = $append;
}
print_r(array_map(function($arr){ return implode('/',$arr);},$results));
I have this array:
Array
(
[datas] => Array
(
[General] => Array
(
[0] => Array
(
[id] => logo
[size] => 10
)
)
[Rooms] => Array
(
[0] => Array
(
[id] => room_1
[size] => 8
)
[1] => Array
(
[id] => room_2
[size] => 8
)
[2] => Array
(
[id] => room_3
[size] => 8
)
)
)
)
I need to update it when I receive some info like this:
$key = 'room_3';
$toChange = '9';
So in my array, I want to change the size of room_3.
I will always edit the same element (i.e. size).
What I tried:
// Function to communicate with the array
getDatas($array, 'room_3', '9');
function getDatas($datas, $got, $to_find) {
foreach ($datas as $d) {
if (array_search($got, $d)) {
if (in_array($to_find, array_keys($d))) {
return trim($d[$to_find]);
}
}
}
}
But it does not work...
Could you please help me ?
Thanks.
function getDatas($datas, $got, $to_find) {
foreach($datas['datas'] as $key => $rows) {
foreach($rows as $number => $row) {
if($row['id'] == $got) {
// u can return new value
return $row['size'];
// or you can change it and return update array
$datas['dates'][$key][$number]['size'] = $to_find; // it should be sth like $new value
return $datas;
}
}
}
}
function changeRoomSize (&$datas, $roomID, $newSize ){
//assuming that you have provided valid data in $datas array
foreach($datas['datas']['Rooms'] as &$room){
if($room['id'] == $roomID){
$room['size'] = $newSize;
break;//you can add break to stop looping after the room size is changed
}
}
}
//--- > define here your array with data
//and then call this function
changeRoomSize($data,"room_3",9);
//print the results
var_dump($data);
It's a 3-dimensional array, if you want to change the value, do like this:
$key = 'room_3';
$toChange = '9';
$array['datas'] = getRooms($array['datas'], $key, $toChange);
function getRooms($rooms, $key, $toChange) {
foreach($rooms as $k1=>$v1) foreach ($v1 as $k2=>$v2) {
if ($v2['id'] == $key)) {
$rooms[$k1][$k2]['size'] = $toChange;
}
}
return $rooms;
}
print_r($array);
I need to get the value from the serialized array by matching the index value.My unserialized array value is like
Array ( [info1] => test service [price_total1] => 10
[info2] => test servicing [price_total2] => 5 )
I need to display array like
Array ( [service_1] => Array ([info]=>test service [price_total] => 10 )
[service_2] => Array ([info]=>test servicing [price_total] => 5 ))
buy i get the result like the below one
Array ( [service_1] => Array ( [price_total] => 10 )
[service_2] => Array ( [price_total] => 5 ) )
my coding is
public function getServices($serviceinfo) {
$n = 1;
$m = 1;
$matches = array();
$services = array();
print_r($serviceinfo);
if ($serviceinfo) {
foreach ($serviceinfo as $key => $value) {
if (preg_match('/info(\d+)$/', $key, $matches)) {
print_r($match);
$artkey = 'service_' . $n;
$services[$artkey] = array();
$services[$artkey]['info'] = $serviceinfo['info' . $matches[1]];
$n++;
}
if ($value > 0 && preg_match('/price_total(\d+)$/', $key, $matches)) {
print_r($matches);
$artkey = 'service_' . $m;
$services[$artkey] = array();
$services[$artkey]['price_total'] = $serviceinfo['price_total' . $matches[1]];
$m++;
}
}
}
if (empty($services)) {
$services['service_1'] = array();
$services['service_1']['info'] = '';
$services['service_1']['price_total'] = '';
return $services;
}
return $services;
}
I try to print the matches it will give the result as
Array ( [0] => info1 [1] => 1 ) Array ( [0] => price_total1 [1] => 1 )
Array ( [0] => info2 [1] => 2 ) Array ( [0] => price_total2 [1] => 2 )
Thanks in advance.
try this. shorted version and don't use preg_match
function getServices($serviceinfo) {
$services = array();
if (is_array($serviceinfo) && !empty($serviceinfo)) {
foreach ($serviceinfo as $key => $value) {
if(strpos($key, 'info') === 0){
$services['service_'.substr($key, 4)]['info']=$value;
}elseif (strpos($key, 'price_total') === 0){
$services['service_'.substr($key, 11)]['price_total']=$value;
}
}
}
return $services;
}
$data = array('info1'=>'test service','price_total1'=>10,'info2'=>'test servicing','price_total2'=>5);
$service = getServices($data);
print_r($service);
I have data here:
Array
(
[1] => Array
(
[SiteID] => 1
[OwnerAID] => 1
)
[3] => Array
(
[SiteID] => 3
[OwnerAID] => 1
)
[6] => Array
(
[SiteID] => 6
[OwnerAID] => 2
)
)
Now, I need to group the OwnerAID into two categories: the first one is OwnerAID owning only one SiteID and the second one is OwnerAID owning more than 1 SiteID.
I've tried to make a program and do some research, but the output of my code is wrong.
Please see my code:
public function groupIndividualAndAggregateSites()
{
$arrays = array();
foreach($this->combined as $key => $key_value)
{
$SiteID = "";
if ($SiteID == "") {
$SiteID=array($key_value['SiteID']); }
else {
$SiteID = array_merge((array)$SiteID, (array)$key_value['SiteID']);
$SiteID = array_unique($SiteID);
}
} if ($SiteID != "") {
$arrays = array('AID'=>$key_value['AID'], 'Sites' => $SiteID);
}
print_r($arrays);
}
The result should be shown like this:
Array(
[1] => Array
( [Sites] => Array ([0] => 1, [1] => 3)))
Array(
[2] => Array
( [Sites] => Array ([0] => [6]))
What you should go for is array:
$owners = array(
owner_1 => SiteID1, // int Only one site
owner_2 => array (SiteID2,SiteID3), // array Multiple sites
);
and later use the array $owners like:
echo (is_array($owners[$owner_1]) ? 'Has multiple sites' : 'has one site';
Thats the basic idea of small memory footprint.
Example, not tested.
public function groupIndividualAndAggregateSites() {
$owners = array();
foreach($this->combined as $key => $value) {
$owner_id = $value['OwnerAID'];
$site_id = $value['SiteID'];
if(array_key_exists($owner_id,$owners)) {
// He has one or more sites already?
if(is_array($owners[$owner_id]){
array_push($owners[$owner_id],$site_id);
} else {
// User already has one site. Lets make an array instead and add old and new siteID
$old_site_id = $owners[$owner_id];
$owners[$owner_id] = array($old_site_id,$owner_id);
}
} else {
$owners[$owner_id] = $site_id;
}
return $owners;
}
All you need to do is loop over your initial array, appending each OwnerAID to the relevant output subarray as determined by the SiteID:
$output = array(1=>array(), 2=>array());
foreach ($original as $v) $output[$v['SiteID'] == 1 ? 1 : 2][] = $v['OwnerAID'];
Here I am using the following features:
array() initialisation function;
foreach control structure;
ternary operator;
$array[$key] addressing;
$array[] pushing.
I have the following $qtips_messages array,
Array
(
[0] => Array
(
[id] => 1
[tips_title] => email_tips
[tips_message] => ex:xxxxx#xyz.com
[tips_key] => key_email
)
[1] => Array
(
[id] => 2
[tips_title] => website_tips
[tips_message] => ex:http://www.yahoo.co.in
[tips_key] => key_website
)
[2] => Array
(
[id] => 3
[tips_title] => zipcode_tips
[tips_message] => ex:60612
[tips_key] => key_zipcode
)
[3] => Array
(
[id] => 4
[tips_title] => phone_tips
[tips_message] => ex:1234567890
[tips_key] => key_phone
)
)
For example, I want to get the tips message for the tip_title 'email_tips'
I have tried with following code,
foreach($qtips_messages as $qtipsArray){
foreach($qtipsArray as $qkey=>$qvalue){
if($qtipsArray['tips_title'] == 'email_tips'){
$emailtipsMessage = $qtipsArray['tips_message'];
}
}
}
When i ran the above code i did not get any value.
What is wrong with this code?
You only need one loop:
$message = null;
foreach ($array as $tips) {
if ($tips['tips_title'] == 'email_tips') {
$message = $tips['tips_message'];
break;
}
}
I'd probably go for something like this though:
$message = current(array_filter($array, function ($tip) { return $tip['tips_title'] == 'email_tips'; }));
echo $message['tips_message'];
$array = array();
foreach($result AS $k =>$val)
$array[$val['tips_key']] = $val['tips_message'];
return $array;
Now the array $array have all the values for q-tips based on their keys...
Hope this will helps you...
try this way.
$array = array();
foreach($qtips_messages AS $k =>$val):
if($val['tips_title']=='email_tips')
{
$array[$val['tips_key']] = $val['tips_message'];
print_r($array);
break;
}
endforeach;