PHP associative array keys not being set - php

In this snippet $opt is an xpath node created from this tag:
<option name="all">1</option>
//...
$this->permissions = array();
$key = $opt['name'];
$value = $opt[0];
echo $key . "=" . $value;
$this->permissions[$key] = $value;
echo $this->permissions['all'];
//...
From these echo statements I get the following return:
all=1
Which is expected from the first echo, but the second prints nothing.
When I do:
echo array_keys($this->permissions);
I get:
Array
When I do:
var_dump($this->permissions);
I get:
array(0) { }
When I do:
print_r($this->permissions);
or:
print_r(array_keys($this->permissions));
I get:
Array ( )
Any ideas, anyone? why are the keys coming back as arrays? What am I missing here...

try
var_dump($opt);
and post back the results

try to var dump like this:
var_dump(array_keys($this->permissions));
to see the structure of your array.

Related

MYSQLI/PHP How do I have to define my query/statement to output every message that was sent in a particular chatroom? [duplicate]

for output array Instead of using json_encode, use of what.(how is output(echo) array without use of json_encode?)
i use of codeigniter.
CI_Controller:
function auto_complete(){
$hotel_search = $this->input->post('search_hotel');
echo json_encode($this->model_tour->auto_complete($hotel_search));
// if use of json_encode output is '[{"name":"salal"},{"name":"salaso"},{"name":"salasi"},{"name":"salsh"}]' if don want use of json_encode output is "Array"
}
CI_model:
function auto_complete($hotel_search)
{
$query_hotel_search = $this->db->order_by("id", "desc")->like('name', $hotel_search)->get('hotel_submits');
if($query_hotel_search->num_rows()==0){
return '0';
}else{
$data = array();
foreach ($query_hotel_search->result() as $row)
{
$data[] = array('name' => $row->name);
}
return $data;
}
}
If you just want to view the array, print_r($array) or var_dump($array) will work
I'm not sure if this is what you are looking for, but it really helps show the structure of the array/JSON:
echo "<pre>";
print_r($array);
echo "</pre>";
It will show the $array broken out and will keep the tabbing so you can see how it's nested.
If you want to view the contents of the array, use print_r or vardump
If you want to do something useful with the information, you're going to have to loop through the array
foreach($this->model_tour->auto_complete($hotel_search) as $key => $value){
//do something
}
foreach($array as $key => $value)
{
echo $key . ' = ' . $value . "\n";
}
That will give you the array as string.
You can also use print_r()
print_r($array);
or var_dump()
var_dump($array);
You have a few options at your disposal:
json_encode (which you mention yourself)
print_r or var_dump (timw4mail mentions this)
implode (however, this won't work for nested arrays, and it will only display the keys)
a combination of array_map (to format individual array elements using your own function) and implode (to combine the results)
write your own recursive function that flattens an array into a string
If you're using jQuery, any .ajax(), .get(), or .post() function will try to guess the data type being returned from the HTTP request. Set the content type of your controller to JSON:
$hotel_search = $this->input->post('search_hotel');
$this->output
->set_content_type('application/json')
->set_output( json_encode($this->model_tour->auto_complete($hotel_search)) );

Extracting JSON response from JSON Object in PHP [duplicate]

for output array Instead of using json_encode, use of what.(how is output(echo) array without use of json_encode?)
i use of codeigniter.
CI_Controller:
function auto_complete(){
$hotel_search = $this->input->post('search_hotel');
echo json_encode($this->model_tour->auto_complete($hotel_search));
// if use of json_encode output is '[{"name":"salal"},{"name":"salaso"},{"name":"salasi"},{"name":"salsh"}]' if don want use of json_encode output is "Array"
}
CI_model:
function auto_complete($hotel_search)
{
$query_hotel_search = $this->db->order_by("id", "desc")->like('name', $hotel_search)->get('hotel_submits');
if($query_hotel_search->num_rows()==0){
return '0';
}else{
$data = array();
foreach ($query_hotel_search->result() as $row)
{
$data[] = array('name' => $row->name);
}
return $data;
}
}
If you just want to view the array, print_r($array) or var_dump($array) will work
I'm not sure if this is what you are looking for, but it really helps show the structure of the array/JSON:
echo "<pre>";
print_r($array);
echo "</pre>";
It will show the $array broken out and will keep the tabbing so you can see how it's nested.
If you want to view the contents of the array, use print_r or vardump
If you want to do something useful with the information, you're going to have to loop through the array
foreach($this->model_tour->auto_complete($hotel_search) as $key => $value){
//do something
}
foreach($array as $key => $value)
{
echo $key . ' = ' . $value . "\n";
}
That will give you the array as string.
You can also use print_r()
print_r($array);
or var_dump()
var_dump($array);
You have a few options at your disposal:
json_encode (which you mention yourself)
print_r or var_dump (timw4mail mentions this)
implode (however, this won't work for nested arrays, and it will only display the keys)
a combination of array_map (to format individual array elements using your own function) and implode (to combine the results)
write your own recursive function that flattens an array into a string
If you're using jQuery, any .ajax(), .get(), or .post() function will try to guess the data type being returned from the HTTP request. Set the content type of your controller to JSON:
$hotel_search = $this->input->post('search_hotel');
$this->output
->set_content_type('application/json')
->set_output( json_encode($this->model_tour->auto_complete($hotel_search)) );

How to get keys and values separately

(please don't say this is a duplicate, I checked here first )
I have a json file like this
[{"studentName":"ali","studentPhone":"123"},
{"studentName":"veli","studentPhone":"134"}
need to get keys and values separately I am trying something like this
foreach ($jsonArray as $array ) {
if(is_array($array)){
while($bar = each($array)){
echo $bar[1];
}
but gives me this output :
ali123veli134hatca134dursun13444
I have also tried this way :
if(is_array($array)){
foreach ($array as $key => $value) {
echo $value;
}
Make a try like this way with json_decode() or use array_column() to get only studentName
Using normal foreach:
<?php
$json = '[{"studentName":"ali","studentPhone":"123"}, {"studentName":"veli","studentPhone":"134"}]';
$array = json_decode($json,1); // the second params=1 makes json -> array
foreach($array as $key=>$value){
echo $value['studentName']."<br/>";
#echo $value['studentPhone']."<br/>";
}
?>
Using array_column():
<?php
$json = '[{"studentName":"ali","studentPhone":"123"}, {"studentName":"veli","studentPhone":"134"}]';
$array = json_decode($json,1);
$names = array_column($array,'studentName');
print '<pre>';
print_r($names); // to get only studentName
print '</pre>';
?>
First of all you need to decode the json string, assign it to a variable, then loop through that variable and echo out the names (studenName).
Ps: after decoding the JSON array we can acces the elements' names in each column using the -> notation, as we have objects stored in that array.
// decoding the json string
$jsonArray =
json_decode('[{"studentName":"ali","studentPhone":"123"},
{"studentName":"veli","studentPhone":"134"}]');
//loop through the array and print out the studentName
foreach($jsonArray as $obj) {
echo $obj->studentName . ' ';
// if you want to print the students phones, uncomment the next line.
//echo $obj->studentPhone . ' ';
}
// output: ali veli

Accessing first element of stdobject

I am using an API where I get the following response:
{"BTC_LTC":{"last":"0.0251","lowestAsk":"0.02589999","highestBid":"0.0251","percentChange":"0.02390438",
"baseVolume":"6.16485315","quoteVolume":"245.82513926"},"BTC_NXT":{"last":"0.00005730","lowestAsk":"0.00005710",
"highestBid":"0.00004903","percentChange":"0.16701570","baseVolume":"0.45347489","quoteVolume":"9094"}, ... }
It is no problem to access the object when I would know the key like
$result = json_decode(file_get_contents($url));
$result->BTC_LTC->last
In my case I do not know the XXX_XXX keys. How can I get the keys? I tried things like $result->{0} / $result->[0] / $result{0} / $result[0] without success.
How can I retrieve the values of XXX_XXX (e.g. in the example above BTC_LTC)?
This should do the trick:
<?php
$json = '{"BTC_LTC":{"last":"0.0251","lowestAsk":"0.02589999","highestBid":"0.0251","percentChange":"0.02390438",
"baseVolume":"6.16485315","quoteVolume":"245.82513926"},"BTC_NXT":{"last":"0.00005730","lowestAsk":"0.00005710",
"highestBid":"0.00004903","percentChange":"0.16701570","baseVolume":"0.45347489","quoteVolume":"9094"}}';
$result = json_decode($json);
$vars = get_object_vars($result);
$keys = array_keys($vars);
echo $vars[$keys[0]]->last;
?>
You can try the code here
Use a foreach loop that should work
foreach($result as $k=>$v)
{
.
.
.
}
The second argument to json_decode allows specifying that you want an array returned:
$result = json_decode(file_get_contents($url),true);
Now that result is an array you can use it as usual (looping, array_keys, e.t.c.).

I have a problems. I need to split string variable in php

I have code which extracting keywords, this keywords with td/idf rating and other options are in $tags. Variable k-keyword consisting of this word. but if i want to print all of these keywords, this keywords look like: one long string, I need to have this keywords separated with " ", or ","...
I have something like that in php
foreach($tags->keywords as $k) {
//$metTag = parseTags($k->keyword);
print_r ($k->keyword);
}
and output is
userarmethodrecommenddelivthidecisconsidactionruleadaptinformmodelcontentsituatbasiengagon-demandproactivmood
but I need output like that:
Array (
[user]
[ar]
[method]
[recommend]
...
)
You can just do
print_r($tags->keywords);
Or this way :
$array = array();
foreach($tags->keywords as $k) {
$array[] = $k->keyword;
}
print_r($array);
Try this:
echo '<pre>';
foreach($tags->keywords as $k) {
print_r ($k->keyword);
}
echo '</pre>';
From the code that you've provided, it looks like $k is an object with the field keyword. Since $tags->keywords is an array you could try to just use print_r($tags->keywords); instead of the loop but this will display all of the fields in the array $tags->keywords as well as dump all of the fields in the $keywords objects within the array. You might also try print_r($k) but again, this will also print all of the fields in the $k object.
Another option would be to simply do this:
print "Array (\n";
foreach($tags->keywords as $k) {
//$metTag = parseTags($k->keyword);
print $k->keyword . "\n";
}
print ")\n"

Categories