How to parse below array returned by sphinx in php
php code
if ( $result === false ) {
echo "Query failed: " . $cl->GetLastError() . ".\n";
}
else {
if ( $cl->GetLastWarning() ) {
echo "WARNING: " . $cl->GetLastWarning() . "<br>";
}
if ($result['0'] > 0) {
// print_r($result['0']['attrs']);
$words[] = $result['0']['matches'];
foreach ($words as $val) {
echo "=> " . $val['keyword'] . $val['ref'];
}
} else {
echo 'No results found';
}
}
Array
(
[0] => Array
(
[error] =>
[warning] =>
[status] => 0
[fields] => Array
(
[0] => keyword
[1] => ref
)
[attrs] => Array
(
[keyword] => 7
[ref] => 7
)
[matches] => Array
(
[25367434949034415] => Array
(
[weight] => 1643
[attrs] => Array
(
[keyword] => hunting
[ref] => activity
)
)
)
[total] => 1
[total_found] => 1
[time] => 0.000
[words] => Array
(
[hunt] => Array
(
[docs] => 1
[hits] => 1
)
)
)
)
I want to parse 'matches' array and it's subarray values like keyword, ref etc.
Lets concentrate on this bit
if ($result['0'] > 0) {
$words[] = $result['0']['matches'];
foreach ($words as $val) {
echo "=> " . $val['keyword'] . $val['ref'];
}
}
Firstly, the result isnt an integer, so shouldnt really be compared as one (although guess it might work)
Personally would recommend checking the actual matches - and using empty, which works fine on arrays...
if (!empty($result['0']['matches'])) {
Then you for some reason add the matches to an array (thats what $arry[] = does, its effectively 'push')
... but also no point assigned to a variable, as only use it once (in the foreach)
... Plus generally the document_id is the index of the matches array, so expose that.
foreach ($result['0']['matches'] as $doc_id => $val) {
finally, now have the matches, want the attribute in the attors array. so something like
$a = $val['attrs'];
echo $doc_id." => ".$a['keyword'].", ".$a['ref']."<br>\n";
putting it all together...
if (!empty($result['0']['matches'])) {
foreach ($result['0']['matches'] as $doc_id => $val) {
$a = $val['attrs'];
echo $doc_id." => ".$a['keyword'].", ".$a['ref']."<br>\n";
}
}
Related
Hey guys i am new of the laravel, how can i parse this output array? This is my array is coming from repeater using jquery.
Array
(
[tour_baslik] => 1. Day
[tour_icerik] => content here....
[lunch] => Array
(
[0] => 2
)
[dinner] => Array
(
[0] => 1
[1] => 2
)
)
Array
(
[tour_baslik] => 2.Day
[tour_icerik] => content 2 here...
[lunch] => Array
(
[0] => 1
[1] => 2
)
[dinner] => Array
(
[0] => 2
)
)
I need parse like that but i'm stuck:
foreach($myarray as $key => $data){
echo $key . '-' . $data; }
Output must be:
tour_baslik - 1.day
tour_icerik - content here..
lunch - 2
dinner - 1,2
If you need to iterate through all items of your input, you could use a recursive function like the following:
function iterator($key, $value) {
if (is_array($value)) {
foreach ($value as $key => $value) {
iterator($key, $value);
}
} else {
echo !empty($key) ? "$key - " : "";
echo $value."\n";
}
}
iterator(null, $input);
Having the following PHP array, how can I insert a string everytime the first character of the value of 'palabra' changes?
Array
(
[0] => Array
(
[palabra] => aaa
)
[1] => Array
(
[palabra] => abbb
)
[2] => Array
(
[palabra] => bbb
)
[3] => Array
(
[palabra] => ccc
)
[4] => Array
(
[palabra] => dddd
)
[5] => Array
(
[palabra] => eeee
)
)
I currently have something like so, but it just list
forearch ($word_array as $word) {
echo '<li>'.$word['palabra'].'</li>';
}
The desired result is something like
<h1>Words starting with A</h1>
<li>aaa</li>
<li>abbb</li>
<h1>Words starting with B</h1>
<li>bbb</li>
<h1>Words starting with C</h1>
<li>ccc</li>
Track first letter on every iteration and if it changes - output h1:
$currentLetter = '';
foreach ($word_array as $word) {
$firstLetter = substr($word['palabra'], 0, 1);
if ($firstLetter !== $currentLetter) {
echo '<h1>Words starting with ' . $firstLetter . '</h1>';
$currentLetter = $firstLetter;
}
echo '<li>'.$word['palabra'].'</li>';
}
As a sidenote - having h1 as item of ul will be considered as invalid markup.
I have this Array but i don't know how to get the [discount_amount] based on the [object_ids].
For example i would like to get the 93 value if my [object_ids] contain 81.
Array
(
[0] => Array
(
[id] => rule_5b0d40cd1408a
[membership_plan_id] => 106
[active] => yes
[rule_type] => purchasing_discount
[content_type] => post_type
[content_type_name] => product
[object_ids] => Array
(
[0] => 81
)
[discount_type] => amount
[discount_amount] => 93
[access_type] =>
[access_schedule] => immediate
[access_schedule_exclude_trial] =>
)
[1] => Array
(
[id] => rule_5b0d4e0f3b0b4
[membership_plan_id] => 106
[active] => yes
[rule_type] => purchasing_discount
[content_type] => post_type
[content_type_name] => product
[object_ids] => Array
(
[0] => 110
)
[discount_type] => amount
[discount_amount] => 50
[access_type] =>
[access_schedule] => immediate
[access_schedule_exclude_trial] =>
)
)
You could use a foreach and use in_array to check if the array object_ids contains 81.
foreach ($arrays as $array) {
if (in_array(81, $array["object_ids"])) {
echo $array["discount_amount"];
}
}
Demo
Try with this code .
Assuming $dataArray is the array you have printed.
foreach ($dataArray as $key => $value){
if($value['object_ids'][0] == 83){
$discount_amount = $value['discount_amount'];
}
}
echo $discount_amount
The way I mostly do this is as followed:
# Save retrieved data in array if you want to.
$test = array();
foreach($array as $line){
# Count the row where discount_amount is found.
$test[] = $line['9'];
echo "Result: ".$line['9']. "<br \>\n";
# OR another method is
$test[] = $line['discount_amount'];
echo "Result: ".$line['discount_amount']. "<br \>\n";
}
Hi everyone I have a json named rushlist.json using this format:
{"rushlist":[{"Char":"Whipthemout","Pass":1,"Fail":1,"Status":"Free"}]}
I figured out how to display the whole array with this:
<?php
$Rushlist = json_decode(file_get_contents("rushlist.json"), true);
print_r($Rushlist);
?>
It displays :
Array ( [rushlist] => Array ( [0] => Array ( [Char] => Whipthemout [Pass] => 1 [Fail] => 1 [Status] => Free ) ) )
However I want to display just the element/value not this:
Array ( [rushlist] => Array ( [0] => Array ( [Char] => Whipthemout [Pass] => 1 [Fail] => 1 [Status] => Free ) ) )
Something like this
Rushlist
[Char] = Whipthemout
[Pass] = 1
[Fail] = 1
[Status] = Free
Any help would be great!
Thanks.
EDIT
<?php
$Rushlist = json_decode(file_get_contents("rushlist.json"), true);
print_r($Rushlist);
foreach($Rushlist as $arr_name=>$arr) {
print $arr_name . "\n";
foreach($arr as $key=>$value) {
print "[" . $key . "] = " . $value . "\n";
}
}
?>
Displays this:
Array ( [rushlist] => Array ( [0] => Array ( [Char] => Whipthemout [Pass] => 1 [Fail] => 1 [Status] => Free ) ) ) rushlist
Notice: Array to string conversion in F:\Share\test\Dropbox\test\test3.php on line 9
[0] = Array
foreach($Rushlist as $arr_name=>$arr) {
print $arr_name . "\n";
foreach($arr as $arr2 ) {
foreach($arr2 as $key=>$value) {
print "[" . $key . "] = " . $value . "\n";
}
}
}
That should do it.
UPDATED
I'm trying to return a value from a multidimensional array, but it doesn't seem to be working.
Array -
[players] => Array
(
[0] => Array
(
[player] => Necro
[score] => 0
[deaths] => 0
[gq_name] =>
[gq_kills] =>
[gq_deaths] => 0
[gq_score] => 0
[gq_ping] =>
)
)
PHP Foreach
<?php
$dayzplayers = $results["dayz"]["players"];
foreach($dayzplayers as $k => $v) {
echo ' <b>'.$v["player"].'</b>';
} ?>
The ['player'] index appears to have an invisible control character in the key SOH (Start of Heading)
Try echo ' <b>'.$v[chr(1) . "player"].'</b>'; instead of echo ' <b>'.$v["player"].'</b>';
If the data is what you posted in the first listing, this should work:
foreach($dayzplayers as $player) {
echo $player[chr(1).'player'];
}
as per http://codepad.org/kUYueGVh