Why does this say undefined offset 2 in a foreach loop - php

The $snippet variable has the following value:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 1
[user_id] => 2
[title] => Title
[detail] => null
[lang_id] => 19
[edited] => false
[last_edited] =>
[created_date] => 2016-09-25 02:40:03
)
[1] => Array
(
[id] => 2
[user_id] => 2
[title] => Some other title
[detail] => null
[lang_id] => 19
[edited] => false
[last_edited] =>
[created_date] => 2016-09-25 02:40:03
)
[2] => Array
(
[id] => 3
[user_id] => 2
[title] => Huh!
[detail] => ...
[lang_id] => 19
[edited] => false
[last_edited] =>
[created_date] => 2016-09-25 03:04:48
)
)
[1] => Array
(
[0] => Array
(
[id] => 1
[user_id] => 2
[title] => Title
[detail] => null
[lang_id] => 19
[edited] => false
[last_edited] =>
[created_date] => 2016-09-25 02:40:03
)
[1] => Array
(
[id] => 2
[user_id] => 2
[title] => Some other title
[detail] => null
[lang_id] => 19
[edited] => false
[last_edited] =>
[created_date] => 2016-09-25 02:40:03
)
[2] => Array
(
[id] => 3
[user_id] => 2
[title] => Huh!
[detail] => ...
[lang_id] => 19
[edited] => false
[last_edited] =>
[created_date] => 2016-09-25 03:04:48
)
)
[2] => Array
(
[0] => Array
(
[id] => 4
[user_id] => 2
[title] => apache
[detail] => ...
[lang_id] => 1
[edited] => false
[last_edited] =>
[created_date] => 2016-09-25 03:05:06
)
)
)
I wanted my result to be something like:
Title
Some other title
Huh!
Title
Some other title
Huh!
apache
So I tried the following:
<?php foreach ($snippet as $key => $value): ?>
<?= $value[$key]['title'] ?>
<br />
<?php endforeach ?>
But the results were:
Title
Some other title
Notice: Undefined offset: 2
I can't figure out what I'm doing wrong.

Try this:
<?php
foreach ($snippet as $value):
foreach($value as $title):
echo $title['title']."<br>";
endforeach;
endforeach;
?>

Using the array_column() you can print your title's with the help of implode(). This is based on your try, cause you try with one foreach().
<?php
foreach ($snippet as $key => $value):
echo implode("<br/>", array_column($value[$key], 'title'));
endforeach;
?>
Just try this and let me know is it works or not.

Related

how do i get array element isMultiple in php

hi there i am new to php and i want to access isMultiple element of array how can i do that then i want to show if the value of isMultiple is 1 then it should show yes.. any help would be appreciated and it would be great......!!.
Array
(
[result] => Array
(
[0] => Array
(
[pkFeatureTypeId] => 72
[Name] =>
[Status] => 0
[isMultiple] => 1
[isSpecial] => 1
[Order] => 0
)
[1] => Array
(
[pkFeatureTypeId] => 32
[Name] =>
[Status] => 0
[isMultiple] => 1
[isSpecial] => 1
[Order] => 0
)
[2] => Array
(
[pkFeatureTypeId] => 33
[Name] =>
[Status] => 0
[isMultiple] => 1
[isSpecial] => 1
[Order] => 0
)
[3] => Array
(
[pkFeatureTypeId] => 35
[Name] =>
[Status] => 1
[isMultiple] => 1
[isSpecial] => 1
[Order] => 0
)
)
)
Try this.
foreach($data['result'] as $item){
if($item['isMultiple'] == '1'){
echo 'Yes';
}
}

extracting an element of an array [duplicate]

This question already has answers here:
Get index of row with qualifying value from a 2d array
(5 answers)
Closed 7 years ago.
Assume this array:
Array ( [0] => Array ( [id] => 1171 [product_id] => 140 [fileid] => 479717 [purchid] => 847 [cartid] => 833 [uniqueid] => f100c3b3a853202fb6559fbacf025a6aa07f52c7 [downloads] => 99998 [ip_number] => [active] => 1 [datetime] => 2015-06-02 20:10:05 )
[1] => Array ( [id] => 1172 [product_id] => 140 [fileid] => 313624 [purchid] => 847 [cartid] => 833 [uniqueid] => f00a3c91378ad469f333abeec64753b275f10670 [downloads] => 99999 [ip_number] => [active] => 1 [datetime] => 2015-06-02 20:10:05 )
[2] => Array ( [id] => 1173 [product_id] => 140 [fileid] => 313618 [purchid] => 847 [cartid] => 833 [uniqueid] => ac125595e2dbca6a086261434582f6e7dfc5638e [downloads] => 99999 [ip_number] => [active] => 1 [datetime] => 2015-06-02 20:10:05 )
[3] => Array ( [id] => 1174 [product_id] => 140 [fileid] => 313526 [purchid] => 847 [cartid] => 833 [uniqueid] => 3e6123e0a4453de71dec91a177f5b34217625680 [downloads] => 99999 [ip_number] => [active] => 1 [datetime] => 2015-06-02 20:10:05 ))
I want to "extract" the [0] array and use it for something else BUT it has to be conditioned.
I said the 0 element because it has the fileid = 479717 the one that i want.
So i`m looking at extracting the array (in my case [0]) that has filed = $myvalue. Where i can set $myvalue to whatever i want.
Does this fits your needs ?
function findById($id, $arrayOfArrays){
foreach ( $arrayOfArrays as $contents )
if ( $contents['id'] == $id ) return $contents;
}
function filter($fileid, array $array) {
foreach ($array as $key => $value) {
if ($value['fileid'] === $fileid) {
return $value;
}
}
}

How to parse elasticsearch response in php

I try to parse my elasticsearch response in php.
When i print my json resultat with a var_dump, i've got that:
Array (
[took] => 6
[timed_out] =>
[_shards] => Array (
[total] => 5
[successful] => 5
[failed] => 0
)
[hits] => Array (
[total] => 1
[max_score] => 0.44896343
[hits] => Array (
[0] => Array (
[_index] => car
[_type] => car
[_id] => DqE0c4ygRgC81o39DNmwhQ
[_score] => 0.44896343
[_source] => Array (
[currency] => EUR
[link] => myrurl
[reference] => A785454A
[brand] => mybrand
[model] => mymodel
[description] =>
[link_picture] => mylinkpicture
[price] => myprice
[km] => mykm
[start_years] =>
[active] => 1
[title] => mytitle
[ranking] => 22
[date_create] => 2014-05-26
)
)
)
)
)
But, when i try to list or to find a title by a foreach:
$myData = json_decode($response);
foreach ($myobj->hits->hits as $result) {
echo $result->_source->title;
}
that doesn't work, because i obtain this error:
Message: Invalid argument supplied for foreach()
I don't see why.
Thanks in advnce.
In foreach you are trying to use "$myobj".
But decoded json is in "$myData".

getting title field from the process array in the webform

I am using the webform module for Drupal.
I needed to customize one of the webform nodes.
So I tried printing [title] => title of this webform goes here!!! using <?php print drupal_render($form['process']['title']); ?>
But it returns nothing, what am I doing wrong here?
Thanks!
FYI, the form can be rendered with; drupal_render($form['submitted']);
Array ( [#attached] => Array ( [css] => Array ( [0] => sites/all/modules/webform/css/webform.css ) [js] => Array ( [0] => sites/all/modules/webform/js/webform.js ) ) [#process] => Array ( [0] => webform_client_form_includes ) [#node] => stdClass Object ( [vid] => 22 [uid] => 2 [title] => title of this webform goes here!!! [log] => [status] => 1 [comment] => 1 [promote] => 1 [sticky] => 0 [nid] => 22 [type] => webform [language] => en [created] => 1373585579 [changed] => 1374117294 [tnid] => 0 [translate] => 0 [revision_timestamp] => 1374117294 [revision_uid] => 8 [body] => Array ( ) [rdf_mapping] => Array ( [rdftype] => ...
You should try this,
echo $form[#node]->title;
var_dump($form[#node]);// to test it your node data
As from your array
Array (
[#attached] => Array ([css] => Array ( [0] => sites/all/modules/webform/css/webform.css ) [js] => Array ( [0] => sites/all/modules/webform/js/webform.js ))
[#process] => Array ( [0] => webform_client_form_includes )
[#node] => stdClass Object ( [vid] => 22 [uid] => 2 [title] => title of this webform goes here!!! [log] => [status] => 1 [comment] => 1 [promote] => 1 [sticky] => 0 [nid] => 22 [type] => webform [language] => en [created] => 1373585579 [changed] => 1374117294 [tnid] => 0 [translate] => 0 [revision_timestamp] => 1374117294 [revision_uid] => 8 [body] => Array ( ) [rdf_mapping] => Array ( [rdftype] => ...

Zend paginator not working properly when array format is not as it should be

Hi my zend paginator works well when the $business_list array format is like below.
but when the array format changes it displays only one page instead of many pages.
$paginator = Zend_Paginator::factory($business_list);
Array
(
[0] => Array
(
[id] => 216
[userid] => 141
[title] => first req
[image] =>
[logo] =>
[description] =>
this is the first requirment
[date] => 2012-06-12 10:31:01
[area] => 1
[budget] => 1
[type] => 1
[status] => 1
[CmBusinessSection] => Array
(
[0] => Array
(
[id] => 315
[business_id] => 216
[section_id] => 1
[subsection_id] => 13
[description] =>
)
[1] => Array
(
[id] => 316
[business_id] => 216
[section_id] => 3
[subsection_id] => 14
[description] =>
)
[2] => Array
(
[id] => 317
[business_id] => 216
[section_id] => 4
[subsection_id] => 15
[description] =>
)
)
[CmUser] => Array
(
[id] => 141
[username] => venki
[password] =>
[firstname] => venkatesh
[lastname] => abus
[image] => 54winter.jpg
[email] => xxx#xx.com
[phone] => 23423452
[group_id] => 2
[status] =>
[registered_date] => 2012-06-04 06:32:58
[last_visit] => 0000-00-00 00:00:00
[is_active] => 1
[subscribe] => 1
)
)
[1] => Array
(
[id] => 214
[userid] => 98
[title] => gopicontractor
[image] => 54bluehills.jpg
[logo] => 239waterlilies.jpg
[description] =>
TIt uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is
[date] => 2012-06-11 12:18:58
[area] => 1
[budget] => 1
[type] => 3
[status] => 1
[CmBusinessSection] => Array
(
[0] => Array
(
[id] => 305
[business_id] => 214
[section_id] => 1
[subsection_id] => 5
[description] =>
)
[1] => Array
(
[id] => 306
[business_id] => 214
[section_id] => 1
[subsection_id] => 6
[description] =>
)
[2] => Array
(
[id] => 307
[business_id] => 214
[section_id] => 3
[subsection_id] => 1
[description] =>
)
[3] => Array
(
[id] => 308
[business_id] => 214
[section_id] => 4
[subsection_id] => 9
[description] =>
)
)
[CmUser] => Array
(
[id] => 98
[username] => gopi.s
[password] =>
[firstname] => venkatesh
[lastname] => franc
[image] =>
[email] => ss#ss.com
[phone] => 23423452
[group_id] => 3
[status] =>
[registered_date] => 2012-05-16 12:36:57
[last_visit] => 0000-00-00 00:00:00
[is_active] => 1
[subscribe] => 1
)
)
)
the above array format displays paginatin correctly.
This is the array that displays only one page in pagination.
Zend_Paginator Object
(
[_cacheEnabled:protected] => 1
[_adapter:protected] => Zend_Paginator_Adapter_Array Object
(
[_array:protected] => Array
(
[0] => Array
(
[id] => 89
[username] => xx
[password] => xx
[firstname] => xx
[lastname] => xx
[image] =>
[email] => xx#ymail.com
[phone] => 2342345
[group_id] => 2
[status] => offline
[registered_date] => 2012-05-15 10:58:53
[last_visit] =>
[is_active] => 1
[subscribe] => 0
[CmBusiness] => Array
(
[0] => Array
(
[id] => 204
[userid] => 89
[title] => xxhousing
[image] => 760067.jpg
[logo] => xx_818f3c97e6_o.jpg
[description] => xx
[date] => 2012-05-31 13:36:17
[area] => 1
[budget] => 1
[type] => 1
[status] => 1
[CmAmount] => Array
(
[id] => 1
[amount] => 1000-1500
[status] => 1
)
[CmBusinessSection] => Array
(
[0] => Array
(
[id] => 251
[business_id] => 204
[section_id] => 1
[subsection_id] => 6
[description] => xx
)
[1] => Array
(
[id] => 252
[business_id] => 204
[section_id] => 3
[subsection_id] => 2
[description] => xx
)
[2] => Array
(
[id] => 253
[business_id] => 204
[section_id] => 3
[subsection_id] => 4
[description] => xx
)
[3] => Array
(
[id] => 254
[business_id] => 204
[section_id] => 4
[subsection_id] => 9
[description] => xx
)
)
[CmArea] => Array
(
[id] => 1
[area] => manchester
[status] => 1
)
[CmType] => Array
(
[id] => 1
[type] => Personal business
[status] => 1
)
)
[1] => Array
(
[id] => 205
[userid] => 89
[title] => xx
[image] => 41217850-desktop-wallpapers-new-windows-xp.jpg
[logo] => 356anger_n.jpg
[description] => xx
[date] => 2012-05-31 13:37:15
[area] => 1
[budget] => 1
[type] => 3
[status] => 1
[CmAmount] => Array
(
[id] => 1
[amount] => 1000-1500
[status] => 1
)
[CmBusinessSection] => Array
(
[0] => Array
(
[id] => 255
[business_id] => 205
[section_id] => 1
[subsection_id] => 6
[description] =>xx
)
[1] => Array
(
[id] => 256
[business_id] => 205
[section_id] => 3
[subsection_id] => 1
[description] => xx
)
[2] => Array
(
[id] => 257
[business_id] => 205
[section_id] => 3
[subsection_id] => 2
[description] => xx
)
[3] => Array
(
[id] => 258
[business_id] => 205
[section_id] => 4
[subsection_id] => 10
[description] => xx
)
)
[CmArea] => Array
(
[id] => 1
[area] => manchester
[status] => 1
)
[CmType] => Array
(
[id] => 3
[type] => Bilde companies
[status] => 1
)
)
[2] => Array
(
[id] => 206
[userid] => 89
[title] => Nuestros recursos
[image] =>
[logo] =>
[description] => xx
[date] => 2012-05-31 13:38:04
[area] => 1
[budget] => 1
[type] => 4
[status] => 1
[CmAmount] => Array
(
[id] => 1
[amount] => 1000-1500
[status] => 1
)
[CmBusinessSection] => Array
(
[0] => Array
(
[id] => 259
[business_id] => 206
[section_id] => 1
[subsection_id] => 5
[description] =>
)
[1] => Array
(
[id] => 260
[business_id] => 206
[section_id] => 1
[subsection_id] => 6
[description] =>
)
[2] => Array
(
[id] => 261
[business_id] => 206
[section_id] => 3
[subsection_id] => 2
[description] =>
)
[3] => Array
(
[id] => 262
[business_id] => 206
[section_id] => 4
[subsection_id] => 10
[description] =>
)
)
[CmArea] => Array
(
[id] => 1
[area] => manchester
[status] => 1
)
[CmType] => Array
(
[id] => 4
[type] => Designer
[status] => 1
)
)
)
)
)
[_count:protected] => 1
)
[_currentItemCount:protected] =>
[_currentItems:protected] =>
[_currentPageNumber:protected] => 1
[_filter:protected] =>
[_itemCountPerPage:protected] => 2
[_pageCount:protected] => 1
[_pageRange:protected] =>
[_pages:protected] =>
[_view:protected] =>
)

Categories