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;
Related
I have a simple Two array
$ages[] = array("Peter"=>22, "Clark"=>32, "John"=>28);
$ages1[] = array("demo"=>22);
When I print this arrays it should be like following:
Array
(
[0] => Array
(
[Peter] => 22
[Clark] => 32
[John] => 28
)
)
Array
(
[0] => Array
(
[demo] => 22
)
)
But I want to create third array which will be show demo kye value into first array like following:
Array
(
[0] => Array
(
[Peter] => 22
[Clark] => 32
[John] => 28
[demo] => 22
)
)
Can we do two array into single array in PHP like Above
Not sure what are you trying to achieve here...little more context would be helpful. But this is how you can do this,
$ages[] = array("Peter"=>22, "Clark"=>32, "John"=>28);
$ages1[] = array("demo"=>22);
$result[] = array_merge($ages[0],$ages1[0]);
This would do the job.
<?php
$ages[] = array("Peter"=>22, "Clark"=>32, "John"=>28);
$ages1[] = array("demo"=>22);
$output = prepend_array($ages,$ages1);
print_r($output);
// Function to prepend arrays
function prepend_array()
{
$num_args = count(func_get_args());
$new_array = array();
foreach (func_get_args() as $params){
foreach($params as $out_key => $param)
{
foreach($param as $key => $value)
$new_array[$out_key][$key] = $value;
}
}
return $new_array;
}
I have a following piece of code:-
$returnArr = $this->master_model->fetch_all_data($data, $selectString,$limit, $offset);
foreach($returnArr as $row)
{
if (array_key_exists($data.'_image', $row))
{
$img = base_url()."uploads/$data/". $row[$data.'_image'];
$row[$data.'_image'] = $img;
}
}
print_r($returnArr);
The $return is in the following format:
Array ( [0] =>
Array ( [sticker_image] => post_1462515402.jpg
[sticker_code] => :* )
[1] => Array ( [sticker_image] => post_1462515510.jpg
[sticker_code] => ^=^ )
[2] => Array ( [sticker_image] => post_1462515532.jpg
[sticker_code] => >_<* )
[3] => Array ( [sticker_image] => post_1462515539.jpg
[sticker_code] => :(( ) )
Now, in the following line of code, I am changing [sticker_image] to a link:
if (array_key_exists($data.'_image', $row))
{
$img = base_url()."uploads/$data/". $row[$data.'_image'];
$row[$data.'_image'] = $img;
}
Still, the changes doesn't take place. It's still coming as
[sticker_image] => post_1462515402.jpg
What am I doing wrong?
$row[$data.'_image'] = $img; will change only local copy of array element.
To change actual array element you must loop with reference:
$returnArr = ['a' => 'b'];
foreach ($returnArr as &$row) {
$row = 'cc';
}
var_dump($returnArr); // ['a' => 'cc'];
how can i count the total duplicate link in the array?
its similar question here: count of duplicate elements in an array in php
but im not sure how to implement the code on my case.
my server PHP version 5.4
Array
(
[0] => Array
(
[link] => http://myexample.com
[total] => 3
)
[1] => Array
(
[link] => http://myexampledomain.com
[total] => 2
)
[2] => Array
(
[link] => http://myexample.com
[total] => 1
)
)
I am expecting the result to be:
http://myexample.com: 4
http://myotherdomain.com: 2
You can simply use
$result = [];
array_walk($arr, function($v, $k)use(&$result) {
if (isset($result[$v['link']])) {
$result[$v['link']] += $v['total'];
}else{
$result[$v['link']] = $v['total'];
}
});
print_r($result);
Demo
Try below code:
<?php
$array = array(array("link" => "http://myexample.com", "total" => 3), array("link" => "http://myexampledomain.com", "total" => 2), array("link" => "http://myexample.com", "total" => 1));
$res = array();
foreach ($array as $vals) {
if (array_key_exists($vals['link'], $res)) {
$res[$vals['link']]+=$vals['total'];
} else {
$res[$vals['link']]=$vals['total'];
}
}
print_r($res);
?>
You can use this simple logic :
$tempArray = array();
foreach ($array as $value) {
if (!array_key_exists($value['link'],$tempArray) {
$tempArray[$value['link']] = 1;
} else {
$tempArray[$value['link']] = $tempArray['link'] + 1;
}
}
print_r($tempArray);
I have been trying to replace the values in the array
I'll name this array as $currencies when i print this it looks like.
Array
(
[0] => Array
(
[currencylabel] => USA, Dollars
[currencycode] => USD
[currencysymbol] => $
[curid] => 1
[curname] => curname1
[check_value] =>
[curvalue] => 0
[conversionrate] => 1
[is_basecurrency] => 1
)
[1] => Array
(
[currencylabel] => India, Rupees
[currencycode] => INR
[currencysymbol] => ₨
[curid] => 2
[curname] => curname2
[check_value] =>
[curvalue] => 0
[conversionrate] => 50
[is_basecurrency] =>
)
[2] => Array
(
[currencylabel] => Zimbabwe Dollars
[currencycode] => ZWD
[currencysymbol] => Z$
[curid] => 3
[curname] => curname3
[check_value] =>
[curvalue] => 0
[conversionrate] => 22
[is_basecurrency] =>
)
)
Here I am having a $conversionRate to which i need to divide the values present in the array $currencies [0] -> Array -> [conversionrate] and replace in the same place in array.
and the same operation for [1] -> Array -> [conversionrate] and so on..
for which my current approach is as follows
$conversionRate = 50;
foreach ($currencies as $key => $val) {
$key['conversionrate'] = $key['conversionrate'] / $conversionRate;
if($key['conversionrate'] == 1) {
$key['is_basecurrency'] = 1;
} else {
$key['is_basecurrency'] = '';
}
}
print_r($key);
die;
Currently this is not working kindly help
Your loop is all wrong, there is no $key['conversionrate'], it's $val['conversionrate']. In fact there doesn't seems to be a reason for the $key variable, you can just loop through the array with
foreach ($currencies as &$val)
Also, you probably want to print_r($currencies), not $key
Do not compare floating point numbers with == to 1, it might not work due to rounding errors.
You mixed up key and value and you need to use &$val to be able to change the array.
$conversionRate = 4;
foreach ($currencies as $key => &$val) {
if($val['conversionrate'] == $conversionRate) {
$val['is_basecurrency'] = 1;
} else {
$val['is_basecurrency'] = '';
}
$val['conversionrate'] = $val['conversionrate'] / $conversionRate;
}
unset($val);
print_r($currencies);
die;
$key is an index identofoer of an array and $val contain array values
so use like this
$conversionRate = 4;
foreach ($currencies as $key => $val) {
$val['conversionrate'] = $val['conversionrate'] / $conversionRate;
if($val['conversionrate'] == 1) {
$val['is_basecurrency'] = 1;
} else {
$val['is_basecurrency'] = '';
}
}
print_r($val);
die;
I was wondering if anyone could help me restructure a predefined php array. The output of my current array is:
Array
(
[71-ctns] => 1
[71-units] => 1
[308-units] => 1
[305-ctns] => 1
[306-units] => 2
)
And I would like it to look like:
Array
(
[71] => Array
(
[ctns] => 1
[units] => 1
)
[308] => Array
(
[units] => 1
)
[305] => Array
(
[ctns] => 1
)
[306] => Array
(
[units] => 2
)
)
Is this possible?
This should do it
$merged = array();
foreach($a as $k=>$v){
$t = explode('-',$k);
$id = intval($t[0]);
if(!array_key_exists($id, $merged))
$merged[$id] = array();
$merged[$id][$t[1]] = $v;
}
EDIT:
Sorry you should use explode instead of split.
Yes, but you need to loop (note: array_map can also work, but this example is more explicit):
$fin = array();
foreach( $complex as $item => $val )
{
$pieces = explode('-', $item);
$fin[$pieces[0]] = isset($fin[$pieces[0]])?:array();
$fin[$pieces[0]][$pieces[1]] = $val;
}
Find below code to restructure a predefined php array
<?php
$newArray=array();
$result = array("71-ctns"=>1,"71-units"=>1,"308-ctns"=>1,"308-units"=>1,"305-units"=>1,"306-units"=>2);
if(is_array($result) && count($result)>0) {
foreach($result as $key=>$val) {
$getKeyArray = explode("-",$key);
$newArray[$getKeyArray[0]][$getKeyArray[1]] =$val;
}
}
print"<pre>";
print_r($newArray);
?>