how to display the contents of an object array - php

i have an object array from
Sql group_concat()
while($row=$result->fetch_object( ))
{ echo $rows->PreviousV ;}
which has a comma delimited string ie xxxxxx,yyyyyy
how can I loop through it using php and display its contents as links?

Try explode. It will produce an array then you can list them using foreach.
for instance:
$input = "hello,there";
foreach($input as $index=>$key)
{
echo $key;
}
output:
hello
there

Related

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)) );

Multidimensional array - how to get specific values from array

I have converted Excel data into Php array using toArray()..
actually my result is
My question how to get value from this Multidimensional array? and also how to i get header and value from array in separately?
you can access value, by following the path of the array. so in your case :
$yourArrayName['Worksheet'][0][0], will return SNo
Get one value:
print_r($your_array['Worksheet'][0]);
Get values with loop:
foreach ($your_array as $key => $value)
{
echo $key; // 'Worksheet'
print_r $value;
}
If you are trying to extract all the values of specific key then you should use for or foreach or while loop...
its something like
<?php
$array = YOUR ARRAY;
$c=count($array);
for ( $i=0; $i < $c; $i++)
{
echo $array[$i]['StudentName'];
}
?>
else if you want to get a specific value manually You can easily traverse to your value as
$array = YOUR ARRAY
echo $ara['Worksheet'][0]['StudentName']

php remove element from array without add key

I want to remove an element from an array (converted from json), but with unset, and reconvert in json, the array become indexed.
Source array:
{"rows":
[{"c":[{"v":"Date(1409052482000)"},{"v":22},{"v":22},{"v":22},{"v":null}]},
{"c":[{"v":"Date(1409052614000)"},{"v":22},{"v":22},{"v":22},{"v":null}]},
{"c":[{"v":"Date(1409052782000)"},{"v":22},{"v":22},{"v":22},{"v":null}]}
]}
Result:
{"rows":
"2":{"c":[{"v":"Date(1409052614000)"},{"v":22},{"v":22},{"v":22},{"v":null}]},
"3":{"c":[{"v":"Date(1409052782000)"},{"v":22},{"v":22},{"v":22},{"v":null}]}
}}
the problem is the "2" and "3" keys. I don't want this keys, because I use the data for google chart, and is sensible for this index key.
PHP code:
$tempdata = json_decode($jsonTempLog, TRUE);
foreach ($tempdata['rows'] as $key => $row) {
if ( $logtime < $showtime) {
unset($tempdata['rows'][$key]);
}
}
echo json_encode($tempdata);
How can I remove element from array, keep the original json syntax?
Just do this:
$tempdata["rows"] = array_values($tempdata["rows"]);
echo json_encode($tempdata);
Otherwise JSON thinks you're sending an associative array rather a numeric one
this is how i work with :
unset($infos[$i]);
$infos = array_values($infos);
maybe like this:
foreach($tempdata as $row){
$tempdata[$rows['keyfield']] = $row;
}

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"

It is not ouputting values in array when echoed

I think there is a problem when I echo $whereArray and orderByArray. If I type in a word such as "Question" and then submit it, I expect it to display in the echos "%".Question."%"; for both arrays. But instead in both echos it just displays "Array" for both echos. Does this mean that both arrays are not working when it comes to storing in the values?
$searchquestion = $_GET['questioncontent'];
$terms = explode(" ", $searchquestion);
$whereArray = array();
$orderByArray = array();
//loop through each term
foreach ($terms as $each) {
$i++;
$whereArray[] = "%".$each."%";
$orderByArray[] = "%".$each."%";
}
echo $whereArray;
echo $orderByArray;
echo() only works for strings. PHP converts your array to "Array" as a fallback.
When you're debugging, you should use var_dump(). It will tell you the type of the object and its content.
Use var_dump or print_r instead of echo (they are functions, not constructs like echo is).
An array needs to be printed out using a special function such as print_r. If you want to print out a value in your array try:
echo $whereArray[0];
To get the first element. Be careful because if the array is empty you will get an error.
you can also loop through them
foreach($arrayname as $value)
echo $value;
or
echo implode("",$arrayname);

Categories