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
Related
I'm using mustache to handle the frontend of my PHP, and I can't find a way to iterate through a numeric array:
From an API y have something like:
data =>
array (size=2)
50 =>
array (size=2)
'appid' => int 50
'name' => string 'Half-Life: Opposing Force' (length=25)
70 =>
array (size=2)
'appid' => int 70
'name' => string 'Half-Life' (length=9)
I need to print the appid and name of each app but I cant make it work
I tested the Implicit iterator but it seems to be for printing strings, for example, this doesn't work:
{{#data}}
{{#.}}
{{appid}}: {{name}}
{{/.}}
{{/data}}
If you have any idea of how to make this work, it would be appreciated, also if I can get recommendations for another library like mustache for PHP it would be awesome
I have a problem with JSON data in PHP. I need to use data from this JSON in my SQL statement. When I'm trying to debug it with echo(var_dump, or print_r is not working too) command the output with is
{"records":"tekst","name":"[object Object]"}
This is a JSON structre:
{
records: 'tekst',
name: {
imie: 'imie1',
nazwisko: 'nazwisko1'
}
}
I'm trying to decode this by json_decode(), but I have an error
"Warning: json_decode() expects parameter 1 to be string, array
given".
Does anyone know what's wrong?
PHP manual about JSON and the format required: function.json-decode. basically, double quotes only and names must be quoted.
a demonstration of conversion using PHP.
So, you supply the json string that looks like, with the whitespace removed, like this:
{records:[{id:1,name:'n1'},{id:2,name:'n2'}]}
Which is an object containing an array with two entries that could be arrays or objects.
Except, it is not a valid JSON string as it contains single quotes. And PHP wants all the names in double quotes, as in "id":1.
So, possible PHP code to recreate that, assuming arrays as the inner entries is:
$json = new stdClass();
$records = array();
$entry = array('id' => 1, 'name' => 'n1');
$records[] = $entry;
$entry = array('id' => 2, 'name' => 'n2');
$records[] = $entry;
$json->records = $records;
$jsonEncoded = json_encode($json);
Which, when 'dump'ed looks like:
object(stdClass)[1]
public 'records' =>
array
0 =>
array
'id' => int 1
'name' => string 'n1' (length=2)
1 =>
array
'id' => int 2
'name' => string 'n2' (length=2)
Now, the string that structure produces is:
{"records":[{"id":1,"name":"n1"},{"id":2,"name":"n2"}]}
Which looks similar to yours but is not quite the same. Note the names in double quotes.
However, if your json string looked the same then PHP could decode it, as is shown below:
$jsonDecoded = json_decode($jsonEncoded);
var_dump($jsonDecoded, 'decoded');
Output: Note all objects...
object(stdClass)[2]
public 'records' =>
array
0 =>
object(stdClass)[3]
public 'id' => int 1
public 'name' => string 'n1' (length=2)
1 =>
object(stdClass)[4]
public 'id' => int 2
public 'name' => string 'n2' (length=2)
We may want arrays instead so use the true as the second parameter in the 'decode'
$jsonDecoded = json_decode($jsonEncoded, true);
var_dump($jsonDecoded, 'decoded with true switch');
Output: with arrays rather than objects.
array
'records' =>
array
0 =>
array
'id' => int 1
'name' => string 'n1' (length=2)
1 =>
array
'id' => int 2
'name' => string 'n2' (length=2)
string 'decoded with true switch' (length=24)
I'm trying to get the Array so i can loop it through and get the category_id from it. Its output is in Odd behavior, 1st it comes in Public Row, then Public Rows, and then Num Rows. I'm not able to loop to these object as the loop code terminated in the 1st Row only and not going further in Rows. Basically, the output query returns like this:
array (size=3)
'row' =>
array (size=2)
'product_id' => string '50' (length=2)
'category_id' => string '20' (length=2)
'rows' =>
array (size=2)
0 =>
array (size=2)
'product_id' => string '50' (length=2)
'category_id' => string '20' (length=2)
1 =>
array (size=2)
'product_id' => string '50' (length=2)
'category_id' => string '28' (length=2)
'num_rows' => int 2
I'm trying to get the simple array so i can loop it dynamically and get the desired output.
Here's my PHP code:
$categories = (array)$this->db->query("SELECT * FROM " . DB_PREFIX ."product_to_category WHERE product_id=50");
var_dump($categories);
However I've converted the Object into Array by prefixing (array) to the query but it doesn't worked for me as its still in object. Please suggest me as its taking too much time to settle this problem????
I DON'T KNOW WHY THIS IS COMING IN OBJECT?? BUT I GOT THE SOLUTION'S USING
var_dump($categories['rows']);
If anybody know the exact solution your help might be acceptable.
Which framework you are using ?? if you arr using Simple PHP look at this function
mysql_fetch_array
return all the data in an array using function mysql_fetch_array
$categories = $this->db->query("SELECT * FROM " . DB_PREFIX ."product_to_category WHERE product_id=50");
var_dump(mysql_fetch_array($categories));
Font: http://us2.php.net/mysql_fetch_array
When trying to access an array inside an array, only NULL is output.
My Code:
$aStats = array();
$aStats['hd'] = array();
$aStats['hd'][] = array
(
'dev' => $device,
'total' => $total,
'used' => $used,
'free' => $free,
'used_perc' => $used_perc,
'mount' => $folder
);
echo $aStats['hd']['free'];
When using json_encode, the values are displayed correctly:
die( json_encode( $aStats ) );
Where is my mistake?
Replace these lines:
$aStats['hd'] = array();
$aStats['hd'][] = array
With this:
$aStats['hd'] = array
You appear to be accessing your array ($aStats['hd']['free'];) as if the value of hd is an associated array, but using [] creates a new integer index in the array, and stores the value in that index. Joe Walker's answer shows what happens instead, that you have an associative array pointing to an indexed array pointing to another associative array, rather than the associative to associative array you suggest you're trying to use in your echo statement.
This is a practical tip that will let you find out where is the issue easly, all you need to do is:
var_dump($aStats);
This will output:
array (size=1)
'hd' =>
array (size=1)
0 =>
array (size=6)
'dev' => string 'SomeDevice' (length=10)
'total' => string '10000' (length=5)
'used' => boolean true
'free' => boolean false
'used_perc' => string 'none' (length=4)
'mount' => string '/some/directory/here/' (length=21)
Now you know you can access this element using
$aStats['hd'][0]['free'];
This will return null in your question because your variables are not yet initialized, but I guess you do have them initialized in your code, hope this helps.
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.