i am trying to get array values to string, but i fail
My array ($epg) looks like this:
Array
(
)
Array
(
[0] => Array
(
[title] => VGhlIEZhbnRhc3kgRm9vdGJhbGwgQ2x1Yg==
[lang] => en
[start] => 1425385800
[end] => 1425387600
[description] => Sm9obiBGZW5kbGV5IGFuZCBQYXVsIE1lcnNvbiBwcmVzZW50IGEgZGlzY3Vzc2lvbiBvbiBrZXkgZmFudGFzeSBmb290YmFsbCBpc3N1ZXMsIGFzIHdlbGwgYXMgdGhlIHdlZWtlbmQncyBQcmVtaWVyIExlYWd1ZSBtYXRjaGVzLiBBbHNvIGZlYXR1cmluZyBndWVzdHMgZnJvbSB0aGUgd29ybGRzIG9mIHNwb3J0IGFuZCBzaG93Yml6Lg==
)
[1] => Array
(
[title] => QmFyY2xheXMgUHJlbWllciBMZWFndWUgUmV2aWV3
[lang] => en
[start] => 1425387600
[end] => 1425391200
[description] => QSBsb29rIGJhY2sgYXQgcmVjZW50IGZpeHR1cmVzIGluIHRoZSBFbmdsaXNoIFByZW1pZXIgTGVhZ3VlLCBhcyB0aGUgc2Vhc29uIGNvbnRpbnVlZCB3aXRoIG1hdGNoZXMgYWZmZWN0aW5nIGJvdGggZW5kcyBvZiB0aGUgdGFibGUu
)
)
Array
(
)
And then i create foreach loop, and try to get values like this:
$title = $epg['title'];
$lang = $epg['lang'];
echo $lang;
echo $title;
But i get errors:
Notice: Undefined index: title in........ Notice: Undefined index:
lang in.........
I am guessing that happens, because i have strange array , these empty arrays at start and end.
If so, how can i fix it?
Regards
M
When you define the array like here
[0] => Array
(
[title] => VGhlIEZhbnRhc3kgRm9vdGJhbGwgQ2x1Yg==
[lang] => en
[start] => 1425385800
[end] => 1425387600
[description] => Sm9obiBGZW5k...
)
If you don't have title, lang, etc. delcared as variables you need to have
[0] => Array
(
['title'] => VGhlIEZhbnRhc3kgRm9vdGJhbGwgQ2x1Yg==
['lang'] => en
['start'] => 1425385800
['end'] => 1425387600
['description'] => Sm9obiBGZW5k...
)
Use this remove ur empty and then use this
print_r(array_filter($epg));
echo $title = $epg['title']
Check if your array is not empty
if (count($epg) != 0)
You get notices, not errors. But it is good practice to treat them as errors and removing from Your code.
These notices are about undefined indexes, so You have to check if given index is present in current element of array. You can do it like this:
$title = array_key_exists('title',$epg) ? $epg['title'] : NULL;
Manual
Related
I stored some data to an array using this code ($rval['arr']):
$id = $this->input->get("id");
$rval['arr'] = $this->General_model->details($id);
When I print my result by using print_r($rval['arr']); it shows like this:
Array ( [0] => Array ( [id] => 54 [name] => Rajib [address] => DumDum [mobile] => 9865321245 [doj] => 21-2-2010 [fare] => 1245 [img_name] => Penguins.jpg ) )
Now I want to store the address value to a variable like add. How can I do this?
Try this....
$address=$rval['arr'][0]['address'];
demo
You can also do it in a foreach() loop:
foreach($rval['arr'] as $array){
echo $array['name'];
echo $array['address'];
echo $array['mobile'];
}
I have this array:
Array
(
[0] => Array
(
[date] => 2016-03-08
[value] => Array
(
[key_1] => Array
(
[test_1] => 1
[test_2] => 10
[test_3] => 1000
[test_4] => 200
)
[key_2] => Array
(
[test_1] => 1
[test_2] => 15
[test_3] => 1500
[test_4] => 100
)
)
)
Now I have another array :
Array
(
[key_3] => Array
(
[test_1] =>
[test_2] =>
[test_3] =>
[test_4] => 1
)
)
I want to add this last array in the first array.
I try like this : array_push($ymlParsedData[]['value'], $a_big_gift); but not work. Can you help me please ?
You can't use $ymlParsedData[] for accessing specific element, it is a shorthand for pushing data to array.
You can use either
// NB! array_push() just adds the values, key 'key_3' is removed
array_push($ymlParsedData[0]['value'], $a_big_gift);
or
// will keep key 'key_3'
$ymlParsedData[0]['value']['key_3'] = $a_big_gift['key_3'];
or
// use array_merge() instead
$ymlParsedData[0]['value'] = array_merge($ymlParsedData[0]['value'], $a_big_gift);
A complicated answer, but this might solve your issue:
$key_name = array_keys($a_big_gift)[0];
$ymlParsedData[0]['value'][$key_name] = $a_big_gift[$key_name];
echo '<pre>'; print_r($ymlParsedData); exit;
Note: For making it dynamic and for more than one value of $a_big_gift, you need to loop it and achieve your result.
Try this
array_push($ymlParsedData[0]['value'], $a_big_gift['key_3']);
i want display the backdrop_path of the movie but i get this error
Notice: Undefined index: backdrop_path in /..../public_html/movie.php on line 167
I have added
print_r($tmdbResult);
to see the content of $tmdbResult. I want to display just backdrop_path.
Array
(
[movie_results] => Array
(
[0] => Array
(
[adult] =>
[backdrop_path] => /mbA7SCtJoFTactP1lDHA055qCf.jpg
[genre_ids] => Array
(
[0] => 35
[1] => 28
)
[id] => 261392
[original_language] => en
[original_title] => American Ultra
[overview] => A stoner and his girlfriend's sleepy, small-town existence is disrupted when his past comes back to haunt him in the form of a government operation set to wipe him out.
[release_date] => 2015-08-21
[poster_path] => /6oGHH27nqaLGfpcgYRIZYSJs7AD.jpg
[popularity] => 3.509263
[title] => American Ultra
[video] =>
[vote_average] => 5.6
[vote_count] => 134
)
)
[person_results] => Array
(
)
[tv_results] => Array
(
)
[tv_episode_results] => Array
(
)
[tv_season_results] => Array
(
)
)
The code i use is
ini_set("display_errors",1);
$tmdbResponse = curl_exec($ch);
curl_close($ch);
$tmdbResult = json_decode($tmdbResponse, true );
$backdrop_path = $tmdbResult['movie_results']['backdrop_path'];
$smarty->assign("backdrop_path",$backdrop_path);
print_r($tmdbResult);
The reason is you're getting a numeric index, because the result can contain multiple movies. In this case you're getting just one.
All you need to do is access the array like this #u_mulder said:
$bp = $tmdbResult['movie_results'][0]['backdrop_path'];
The [0] meaning that in case you get multiple results, just need to change that index to access the others. May be you shoulds think of a foreach loop, unless for some reason of yours, you KNOW you'll always get ONE single movie, in that case, hardcode your $bp = $tmdbResult['movie_results'][0]['backdrop_path']; no problem.
I'm sending a POST array to a controller. The array looks like such:
Array
(
[event] => Array
(
[0] => Array
(
['publishStart'] => 2013-12-10
['eventStart'] => 2014-05-05
['eventEnd'] => 2014-05-10
['timeStart'] =>
['timeEnd'] =>
['location_id'] => 1
['id'] => 65774
)
There are a few of these blocks, i.e. [event][1]. [event][2], etc. I'm attempting to run a foreach loop on $_POST['event'] and can confirm that on each iteration, $event contains the following:
Array
(
['publishStart'] => 2013-12-10
['eventStart'] => 2014-05-05
['eventEnd'] => 2014-05-10
['timeStart'] =>
['timeEnd'] =>
['location_id'] => 1
['id'] => 65774
)
Now, the problem. You can see in both above arrays there is a key called "id" with a corresponding value. Yet the following code returns a string of "undefined index" errors:
foreach ($_POST['event'] as $event)
{
echo $event['id'];
exit();
}
What on earth am I doing wrong?
Your array indices seem to include the single-quotes.
Try
echo $event["'id'"];
I made a php page in which I got the array within array value.
Here is my array:
Array (
[events] => Array ( [0] => 5 [1] => 7 [2] => 8 )
[fromdate] =>
[todate] =>
[description] =>
[subject] =>
[fromname] =>
[replyto] =>
[senddatetime] =>
[timezone] => America/Los_Angeles
[message] =>
[submit_skip] => Continue
)
This array is the POST data from previous page. I want to get events value in a variable. How can I do this?
I use this for getting events value but it gives me the array but I want to get first event value
echo $eventid=$_POST['events'];
To echo:
echo $_POST['events'][0];
To assign it to a variable:
$eventid = $_POST['events'][0];
To assign the array to a variable and get the value from the new array:
$events = $_POST['events'];
$eventid = $events[0];
[0] represents the key of the first value since it is set to be 0.
Output:
echo $_POST['events'][0];
Assign to variable:
$eventid = $_POST['events'][0];