access associative array - php

I am using Code Igniter and I get following data structure after executing a query at DB
array
'application' =>
array
0 =>
object(stdClass)[19]
public 'app_id' => string '16' (length=2)
public 'app_name' => string 'dddddddd' (length=8)
public 'app_title' => string 'sdfsdf' (length=6)
public 'app_comments' => string 'sdfsdf' (length=6)
public 'active_flg' => string 'N' (length=1)
I know one way to access the values is
foreach($application as $key => $value)
$value->app_id
But I know that I will get only one record each time so I want to access the elements without using foreach.
I have tried to $application->app_id and $application['app_id'] but I keep getting error.
Can anybody please help me to understand how to access the data directly??

You are using multidimensional mixed type of array, with numeric indexing on the second level. SO, while accessing the values, you have to use them too. Like
echo $array['application'][0]->app_id;

A simple example to show you the structure of your array and how you might access it...
$objArray = array('app_id' => 7, 'app_name' => 'apps demo', 'app_title' => 'apps demo title');
$applicationArray = array('application' => array((object)$objArray));
// access the array
print $applicationArray['application'][0]->app_id;

Are you getting that result by doing the following?
$res = $this->db->query('select * from application limit 1')->result();
If so, you can put that result into an object by doing:
$app = $this->db->query('select * from application limit 1')->row();
This way you can access the properties as follows:
echo $app->app_id;
You should check out codeigniters manual on getting results.

Related

how to access fields in mongo document

I have a php codeigniter web app that has a mongo db backend.
i'm stuck for now using the mongoclient library for php.
I often have to run commands like this:
$result = $collection->find(
array("didnum" => $didnum)
);
$result = iterator_to_array($result);
Assuming that $result looks like this:
array (size=1)
'5824b9376b6347a422aae017' =>
array (size=10)
'_id' =>
object(MongoId)[22]
public '$id' => string '5824b9376b6347a422aae017' (length=24)
'users' =>
array (size=1)
0 =>
array (size=2)
...
'rules' =>
array (size=1)
0 =>
array (size=5)
...
'id' => string '5824b9376b6347a422aae017' (length=24)
'last_assigned' => string 'missing' (length=7)
'widgetnum' => string '+18455100023' (length=12)
'location' => string 'missing' (length=7)
What is the easiest way to access the location field?
In other words, in cases where I know there will only be one result, I'm still finding that i have to loop through $result because the array is an associative one, and i won't know what the ID is.
Just wondering if there's an easier way to do this?
Thanks.
I'm not very much into PHP, but findOne method seems to exist for PHP binding as well as other languages' bindings
You can use $collection->findOne instead of $collection->find

"Trying to get property of non-object" in SOAP PHP - not sure why this error is occuring

Hope you guys can help me... Still new at PHP and I am struggling to display parts of this Object/Array set of Results.
I am getting the following result $results back from a SOAP webservice:
`object(stdClass)[9]
public 'Summary' =>
object(stdClass)[2]
public 'ID' => string '1096408402' (length=10)
public 'IKey' => string '1440010962' (length=10)
public 'Address' =>
object(stdClass)[4]
public 'Forename' => string 'TEST' (length=4)
public 'Surname' => string 'TESTER' (length=6)
public 'DOB' => string '0000-00-00' (length=10)
public 'Telephone' => string 'Unavailable' (length=11)
public 'Occupants' =>
array (size=3)
0 =>
object(stdClass)[12]
...
1 =>
object(stdClass)[13]
...
2 =>
object(stdClass)[14]
...
3 =>
object(stdClass)[15]
...
Now I am attempting to put the data into a table format.
I have been successful in creating the table using a foreach on the section marked Occupants. I do this by calling Occupants as follows:
$occupants = ($results->Address->Occupants); and the data is extracted and populated into my table using my code (not relevent for this question).
My problem now is that when I try and do the same for Summary or Address it doesnt work: I get the error "Trying to get property of non-object"
I have tried $summary = $results->Summary and $summary = $results['Summary'] and neither works.
What I then want to do is run
<?php $summary = ($results->Summary);foreach($summary as $person):?>
and then I insert it into my table as follows:
<td><?=$person->ID?></td>
So any idea why I get this error? I dont think it is in the foreach aspect...?
Normally, you should get the "Summary" object with:
$summary = $results->Summary
in this case $summary is an object with 2 properties: "ID" and "IKey".
If you iterate over $summary with foreach, the value of $person would have the value of $summary->ID in the first loop iteration and the value of $summary->IKey in the second loop iteration. Both $summary->ID and $summary->IKey are strings and therefore non-objects, so I think that is why you get the error.
I suppose that you want do do this:
$summary = $results->Summary;
foreach ($summary as $value)
echo "<td>$value</td>";
This should output (for the given example):
<td>1096408402</td><td>1440010962</td>
For more information about Object Iteration, I recommend: http://php.net/manual/en/language.oop5.iterations.php

Sphinx extended match mode

I'm using a sphinx bundle (timewasted SphinxSearchBundle) in a symfony 2 app (https://github.com/timewasted/Search-SphinxsearchBundle) (based on the PHP API)
it works great as long as I don't try to use the SPH_MATCH_EXTENDED.
Here's a code sample
$sphinxSearch = $this->get('search.sphinxsearch.search');
$sphinxSearch->setMatchMode(SPH_MATCH_EXTENDED);
$searchResults = $sphinxSearch->search("#typemesure_id 2", $index);
But the result is always empty, and it seems that my field (#typemesure_id) is considered as a word :
'words' =>
array
'typemesure_id' =>
array
'docs' => string '0' (length=1)
'hits' => string '0' (length=1)
2 =>
array
'docs' => string '4169' (length=4)
'hits' => string '5714' (length=4)
Does anyone konws whats wrong in my request ?
Can anyone post an exemple of working extended query working with this bundle?
My goal is to get a condition like "#(user1,user2,user3) 15"
Thanks for your help
Notes:
the SPH_MATCH_ALL & SPH_MATCH_ANY are working fine.
The setFilter() method is also working
Looking at the function defition...
public function search($query, array $indexes, array $options = array(), $escapeQuery = true)
It will automaticlly escape the query, so the # will be getting escaped. Need to pass false as fourth param

how to parse the json array elements using for loop to get the values one by one

i have a json array wherer i would like to pasre the json till the last element by using for loop for that i would like to get the number of array elements in the json array ,i have more than 3 objects in anarray ,so i am confused how to parse the json till the last element,
i can say you the idea
Count($json);
echo count;
for(i=0;i<l=count($json);i++)
{
then print the value of each key
}
i am stuck ,because there is no fixed lenght for the json i am getting as it is a server response it may return one object one may be twice or thrice or many ,so i thought it would be better to do with for loop ,as a json contain more than one json with 3 keys ,such as country can have more than one state,and one state can have more than one district ,plaese help me,i am stuck with question for last 2 days
thank you
An idea :
function printJson($json) {
foreach($json as $index=>$value) {
if(is_array($value)) {
printJson($value);
} else {
echo 'Value :'.$value.'<br />';
}
}
}
$stringJson = "{'location':[{...}]}"; //for example
printJson(json_decode($stringJson));
You can alternatively decode the json tring using json_decode() which will give u a php variable which u can then easily iterate over using php.
eg.
$string = '{"image":"fox.png","puzzlepieces":{"f":{"puzzlepiece":"one","position":"top:121px;left:389px;"},"x":{"puzzlepiece":"three","position":"top:164px;left:455px;"},"o":{"puzzlepiece":"two","position":"top:52px;left:435px;"}}}';
var_dump(json_decode($string));
will output as
object(stdClass)[1]
public 'image' => string 'fox.png' (length=7)
public 'puzzlepieces' =>
object(stdClass)[2]
public 'f' =>
object(stdClass)[3]
public 'puzzlepiece' => string 'one' (length=3)
public 'position' => string 'top:121px;left:389px;' (length=21)
public 'x' =>
object(stdClass)[4]
public 'puzzlepiece' => string 'three' (length=5)
public 'position' => string 'top:164px;left:455px;' (length=21)
public 'o' =>
object(stdClass)[5]
public 'puzzlepiece' => string 'two' (length=3)
public 'position' => string 'top:52px;left:435px;' (length=20)
My xdebug extension is on in WAMP so your var_dump might be a little differently formatted but overall you'd get a php variable from the array which u can iterate using foreach or other loops.
Read more on json_decode here

How to get content from array

I'm total newbie to PHP and Drupal but I fix this simple(?) thingo on my template. I want to get title, date, text and link path from this array? I can't get it out of there. I think its because its in inside of another array and because im noob in PHP I cant get it out of there ? Also I would like to get it to a loop. If theres more content, I would get it as a list or something. I would use foreach for this? Like foreach $contents as $content and so on?
I get this output from this: var_dump($contents);
array
'total' => string '1' (length=1)
'data' =>
array
0 =>
array
'cid' => string '13231' (length=3)
'title' => string 'TITLEBLABLABLA' (length=3)
'body_text' => string 'TEXTBLABLABAL' (length=709)
'created' => string '313131' (length=10)
'created' => string '2010-07-13 14:12:11' (length=19)
'path' => string 'http://goog.fi' (length=72)
Think of accessing multidimensional arrays in the same way you'd access a file in a subdirectory: just reference each level/directory in sequence. Assuming that array is stored in $arr, you'd get the title as follows:
$arr['data']['0']['title']
Here is a basic PHP array tutorial
It comes down to this:
You retrieve an element of an array with []
$array = array( 'a' => 'b');
echo $array['a']; //Prints b;
To loop through a multi-dimensional array and echo date try this...
foreach ($contents as $key => $content) {
echo $content[$key]['created'];
}
If that doesn't work, post the output of print_r($content) in your question so I can't build you exact array. I'm kinda confused over the structure of your array in your question.

Categories