how to write this expression in one line , codeigniter - php

i have a piece of code , i want to get the result in on line
$lang = $this->config->item('email');
echo $lang['support_email'] ; die;
when i var_dump $lang the result
array(11) {
["useragent"]=>
string(11) "CodeIgniter"
["mailpath"]=>
string(17) "/usr/bin/sendmail"
["protocol"]=>
string(4) "mail"
["smtp_host"]=>
string(9) "localhost"
["smtp_user"]=>
string(0) ""
["smtp_pass"]=>
string(0) ""
["smtp_port"]=>
string(2) "25"
["system_email"]=>
string(21) "noreply#elephanti.com"
["help_email"]=>
string(18) "help#elephanti.com"
["inquiries_email"]=>
string(23) "inquiries#elephanti.com"
["support_email"]=>
string(21) "support#elephanti.com"
}
i tried
echo $this->config->item('email')['support_email']
and
echo echo `$this->config->item('email')->support_email
please help.............

$lang = $this->config->item('email', 'support_email');
From the doc:
http://codeigniter.com/user_guide/libraries/config.html
// Retrieve a config item named site_name contained within the blog_settings array`
$site_name = $this->config->item('site_name',
'blog_settings');

You can only do echo $this->config->item('email')['support_email'] in PHP 5.4+
Otherwise the best you can do is:
$lang=$this->config->item('email');echo $lang['support_email'];exit;
Or write a custom function to do it for you.
But then ask yourself, why must you do this on one line...?

echo $this->config->item('email')['support_email']
This actually works in PHP > 5.4. In older versions, it's not possible to do it in one statement, so you'll have to store the array in a separate local variable. You can create a function that will retrieve a value like so:
<?php
function array_get( array $array, $index ) {
return isset( $array[$index] ) ? $array[$index] : false;
}
echo array_get( $this->config->item( 'email' ), 'support_email' );
But that's useless really. Any particular reason you want to do it in one line?

Try:
foreach($lang as $key=>$val)
{
if($key == "support_email")
{
echo $val;
}
}

Related

Remove empty entries in associated array php

I have tried all the solutions suggested in this post but have not been able to make them work in my case.
My array :
array(2) {
["number_s"]=>
array(14) {
[0]=>
string(2) "22"
[1]=>
string(2) "23"
[2]=>
string(0) ""
[3]=>
string(0) ""
[4]=>
string(0) ""
}
["player_name"]=>
array(14) {
[0]=>
string(9) "John Doe"
[1]=>
string(11) "Jack Sparrow"
[2]=>
string(0) ""
[3]=>
string(0) ""
[4]=>
string(0) ""
}
}
I would like to remove all the empty entries but how to do that ?
Thanks a lot for help
It seems like array_filter for each subarray will do what you want.
$array['number_s'] = array_filter($array['number_s']);
$array['player_name'] = array_filter($array['player_name']);
When called without callback function it just removes all empty entries. See docs for details.
But be aware that it will remove "0" and all values which considered empty.
Assuming there aren't arbitrary numbers of subarrays, you may transverse them (as references) and use unset to remove them from the array:
foreach ($array as &$subarray) {
foreach ($subarray as $key => $value) {
if (empty($value)) {
unset($subarray[$key]);
}
}
}
It's simpler with a functional approach:
$array = array_map(
fn($subarray) => array_filter($subarray),
$array
);
If the array may have arbitrary levels, you may implement a recursive function:
function remove_empty_recursive(array &$array)
{
foreach($array as $key => &$value) {
if (empty($value)) {
unset($array[$key]);
} elseif (is_array($value)) {
remove_empty_recursive($value);
}
}
}
Notice all those solutions assume you want to remove "0", 0 and false from the array. Otherwise, you need to specify another criteria, instead of using empty. For instance:
$array = array_map(
fn($subarray) => array_filter(
$subarray,
fn($value) => !empty($value) || is_numeric($value) || $value === false
),
$array
);

How to add data to OBJECT from Wordpress get_results in PHP

Seems really easy, but I can't seem to figure it out...
I have a simple line that gets mysql results through wordpress like this:
$sql_results = $wpdb->get_results($sql_phrase);
Then I parse it as JSON and echo it: json_encode($sql_results);
However, I want to add other data before I parse it as JSON. But I'm not sure how.
$sql_results basically gets me a list of post ID's, title and category.
It looks like this in var_dump (this is just the first row):
array(1)
{
[0]=> object(stdClass)#2737 (7)
{
["ID"]=> string(4) "2700"
["post_title"]=> string(18) "The compact helmet"
["category"]=> string(5) "Other"
}
}
Now to start with something easy, I'd like all associative arrays inside the object to have the extra key-value. I tried the following but got an error:
500 Internal error.
foreach($sql_search as $key => $value)
{
$value['pic_img'] = "test";
$sql_search[$key]=$value;
}
$result=$sql_search;
$sql_results = array(1)
{
[0]=> object(stdClass)#2737 (7)
{
["ID"]=> string(4) "2700"
["post_title"]=> string(18) "The compact helmet"
["category"]=> string(5) "Other"
}
}
foreach($sql_results as $key=>$value)
{
$value->solution = 'good';
$sql_results[$key]=$value;
}
$result=$sql_results;
var_dump($result);
$test = array ( array("ID"=>"35", "name"=>"Peter", "age"=>"43"),
array("ID"=>"34", "name"=>"James", "age"=>"19"), array("ID"=>"31", "name"=>"Joe", "age"=>"40") );
foreach($test as $key=>$value)
{
$value['solution'] = 'good';
$test[$key]=$value;
}
$result=$test;
var_dump($result);

How to edit mulltidimensional array elements in php?

this is my array:
$array= array(3) {
[0]=> array(3) { ["name"]=> "one" ["com"]=> "com1" ["id"]=> "1" }
[1]=> array(3) { ["name"]=> "two" ["com"]=> "com2" ["id"]=> "2" }
[2]=> array(3) { ["name"]=> "three" ["com"]=> "com3" ["id"]=> "3" }
I need posibility to change values of name and com for specific id. I try some examples from Stack questions:
1.Link1
foreach($array as &$value){
if($value['id'] == 1){
$value['name'] = 'test';
$value['com'] = 'test';
break; // Stop the loop after we've found the item
}
}
But it don't work. no error but no result too.
2.Link 2
Again,no error message,but no result...
I also try a lot of other examples from Stack but fake,and finaly to write a question..
Buy,
P
Since you are not changing your array value that's why it's-not giving you desired output. Try this:-
foreach($array as $key => &$value){
if($key == 1){
$array[1]['name'] = 'test';// change value to original array
$array[1]['com'] = 'test'; //change value to original array
break; // Stop the loop after we've found the item
}
}
for($i=0;$i<count($array);$i++) {
if($array[$i]['id'] == 1) {
$array[$i]['name'] = 'test';
$array[$i]['com'] = '';
break;
}
}
print_r($array);
If you are able to change the array on creation I would recommend shifting the id to the array's key identifier. Would make life a lot easier to just do:
$array[1]['name'] = 'test';
Otherwise use the for loop posted above and look it up. (Right awnser)

PHP global variable shows null for first iteration of loop

Sorry couldn't frame a better title.
So here's the problem
I have a function inside functions.php
function show_news(){
$id_counter = 1;
$json_news = array(
"id" => 0,
"title" => ""
);
$json_o = json_decode(file_get_contents(JSON_DATA_FOLDER.'news.json'));
foreach ($json_o as $id => $news_category)
{
echo '<h2>'.$id.'<h2>';
foreach ($news_category as $news)
{
if(IsNullOrEmptyString($news->id)){$json_news['id'] = $id_counter; $id_counter++;}
else{$json_news['id']=$news->id;}
if(!IsNullOrEmptyString($news->title)){$json_news['title']=$news->title;}
var_dump($json_news);
echo "<br/>-------<br/>";
include('news-layout.php');
}
}
}
I'm reading a json file and for each element I am assigning its value to an array.
Then I'm including 'news-layout.php'. For testing purposes I've kept just these 3 lines of code inside 'news-layout.php'
<?php
global $json_news;
var_dump($json_news);
echo"<br/>=======================<hr/>";
?>
So I'm doing a var_dump inside my function as well as on the included page. But I'm getting strange result. Everything works fine except that var_dump($json_news) on included page shows NULL for first iteration of loop !!
Here's the output
todays_specials
array(2) { ["id"]=> int(1) ["title"]=> string(26) "Okie Since I have to do it" }
-------
NULL
=======================
array(2) { ["id"]=> int(2) ["title"]=> string(16) "Vegetable Samosa" }
-------
array(2) { ["id"]=> int(2) ["title"]=> string(16) "Vegetable Samosa" }
=======================
array(2) { ["id"]=> int(3) ["title"]=> string(16) "Vegetable Pakora" }
-------
array(2) { ["id"]=> int(3) ["title"]=> string(16) "Vegetable Pakora" }
=======================
You can see that strange NULL coming there.
Can anyone explain what's happening or how to fix it?
Your $json_news var is local to the functions file first. Then the layout file is included and you create the global $json_news var, and from then on the global is used. Set it to global in your functions file and remove the variable declaration in the layout file and you should be good to go!
Like this:
function show_news(){
$id_counter = 1;
global $json_news = array(
"id" => 0,
"title" => ""
);
$json_o = json_decode(file_get_contents(JSON_DATA_FOLDER.'news.json'));
foreach ($json_o as $id => $news_category){
echo '<h2>'.$id.'<h2>';
foreach ($news_category as $news){
if(IsNullOrEmptyString($news->id)){
$json_news['id'] = $id_counter; $id_counter++;
}else{
$json_news['id']=$news->id;
}
if(!IsNullOrEmptyString($news->title)){
$json_news['title']=$news->title;
}
var_dump($json_news);
echo "<br/>-------<br/>";
include('news-layout.php');
}
}
}
'news-layout.php'
<?php
var_dump($json_news);
echo"<br/>=======================<hr/>";
?>
Side note: using globals like that is not to be recommended!

How to foreach this kind of array using php

array(1) {
["album_name"]=>
string(12) "Cover Photos"
}
array(1) {
["cover"]=>
string(111) "url"
}
array(1) {
["album_name"]=>
string(24) "Fun in Your Name! Photos"
}
array(1) {
["cover"]=>
string(108) "url"
}
This is what it return when I do a var_dumpto my variable, I tried a normal foreach:
<?php
foreach ($fb_albums as $my_albumsdata):
echo $my_albumsdata['cover'];
endforeach;
?>
But doesn't work...
Try this:
for($i=0; $i < count($yourArray); $i += 2) {
$name = $yourArray[$i]["album_name"]
$cover = $yourArray[$i+1]["cover"]
}
But, I think you must change the organisation of the Array.
assuming that you have an array of those four arrays....
the problem would seem to be that not every $my_albumsdata contains a "cover".
if(array_key_exists("cover", $my_albumsdata)) echo $my_albumsdata["cover"];
^should be a quick fix, but lacking context, I'm not sure if this works for you.

Categories