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
Related
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";
}
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";
}
}
I'm trying to merge/sums 2 arrays that can contain integers or more arrays (themselves containing integer).
When the values are integers, I need to sum them in the final array.
When the values are arrays, I need to loop through the values and sum them in the final array.
If a value or a sub-array exists only in 1 of the base array, it needs to be added in the sub-array of the final array. (This is what I can't do)..)
My arrays are like this:
ARRAY 1
[1466859600] => Array
(
[TOTAL] => 27217
[AAA] => Array
(
[FD_CDP] => 1746
[LO_SC_MIC] => 4654
[FD_ATS] => 893
[CDP] => 40
[SUPERVISION] => 9
[CONTROL] => 4
[ATS] => 4
[EVT_ACK] => 3
)
[BBB] => Array
(
[FD_CDP] => 1376
[LO_SC_MIC] => 4606
[FD_ATS] => 826
[FD_ATSS] => 451
[LO_SFRC] => 4
[FD_S2] => 259
[2_LOSC] => 2
)
[CCC] => Array
(
[FD_CDP] => 1333
[LO_SC_MIC] => 4725
[FD_ATS] => 856
[CONTROL] => 4
[ATS] => 2
[EVT_ACK] => 5
)
ARRAY 2
[1466859600] => Array
(
[TOTAL] => 95406
[AAA] => Array
(
[FD_ATSS] => 1719
[LO_SC_MIC] => 16830
[CONTROL] => 16
[NEW] => 7
[NOEL] => 206
)
[BBB] => Array
(
[SUPERVISION] => 23
[CDP] => 158
[CONTROL] => 40
[2_LOSC] => 14
[ATS] => 6
[EVT_ACK] => 4
)
[CCC] => Array
(
[EVT_ACK] => 167
[LO_SFRC] => 248
[SUPERVISION] => 23
)
I wrote a function like this :
function sumArrayValues($array1, $array2)
{
foreach ($array1 as $key => $value)
{
if (is_array($array1[$key]))
{
echo "it's an array\n I need to reloop\n";
sumArrayValues($array1[$key], $array2[$key]);
}
else
{
echo "FIRST VALUE TO SUM\n";
print_r($array1[$key]."\n");
echo "SECOND VALUE TO SUM\n";
print_r($array2[$key]."\n");
$array1[$key] = (int)$array1[$key] +(int)$array2[$key];
echo "--------RESULT of SUM array1&2----------\n";
}
}
return $array1;
}
But this function doesn't take into account 2 (and probably more) cases: if the sub-array are not in the same order, if a sub-array or a value only exist in second array.
A example of function would be a good help, but on a more fundamental level, I even can't figure the algorithm to do that.
Any ideas ?
You can get all the keys for the foreach loop, live demo.
Note, you also can check if a key of any array is undefined, then save the defined value for the key.
function sumArrayValues($array1, $array2)
{
$keys = array_keys($array1 + $array2);
foreach ($keys as $key)
{
if (is_array($array1[$key]) || is_array($array2[$key]))
$array1[$key] = sumArrayValues($array1[$key], $array2[$key]);
else
#$array1[$key] = (int)$array1[$key] +(int)$array2[$key];
}
return $array1;
}
I need to display a certain object from an array before showing the rest of the array.
The array looks like this:
Array
(
[0] => stdClass Object
(
[template_id] => 91
[template_name] => Alphabet
[template_thumbnail] => blank-template-thumbnail.jpg
[template_create_date] => 1456821665
[template_customer_id] => 0
[template_is_responsive] => no
[template_type] => builder
[template_category] => simple
[sort] => 2
)
[1] => stdClass Object
(
[template_id] => 92
[template_name] => Blank Template
[template_thumbnail] => blank-template-thumbnail.jpg
[template_create_date] => 1456821670
[template_customer_id] => 0
[template_is_responsive] => no
[template_type] => builder
[template_category] => simple
[sort] => 2
)
[2] => stdClass Object
(
[template_id] => 31
[template_name] => Holiday Specials
[template_thumbnail] => accommodation-1-20110926.jpg
[template_create_date] => 1456821660
[template_customer_id] => 0
[template_is_responsive] => no
[template_type] => builder
[template_category] => Accommodation
[sort] => 3
)
)
I need to show Blank Template first and then show the rest alphabetically (the order it is in now.
Is there a more elegant solution than looping through the array twice? The size of the array can be anything from 1 (the blank template) to countless objects.
$str="";
for($i=0;$i<=count($arr);$i++){
if($arr[$i]['template_name'] == "Blank Template"){
echo $arr[$i]['template_name'];
}else{
$str .= $arr[$i]['template_name']. "<br>";
}
}
echo $str;
Try this:
$firstItem = null;
$outArray = [];
foreach($yourArray as $item){
if($item->template_name == 'Blank Template'){
$firstItem = $item;
}else{
$outArray[$item->template_name] = $item;
}
}
ksort($outArray,SORT_STRING);
array_unshift($outArray,$firstItem);
Just one loop. Pay attention that this way of doing things, just work if you have uniqueness on the template_name!
Hope it helps.
This will work for you, try
<?php
$dataArray = array(0=>array('template_id'=>91,'template_name'=>'Alphabet'),
1=>array('template_id'=>92,'template_name'=>'Blank Template'),
2=>array('template_id'=>31,'template_name'=>'Holiday Specials')
);
$newArray = array();
foreach($dataArray as $key => $val)
{
if(in_array('Blank Template',$val))///find the key for black template
{
unset($dataArray[$key]); ///unset black temp from original array
$newArray[] = $val;///push black temp into new array at 0 index
foreach($dataArray as $k => $v)
{
$newArray[] = $v; ///push the renaming values into new array
}
}
}
echo "<pre>"; print_r($newArray);
?>
This will give you :
Array
(
[0] => Array
(
[template_id] => 92
[template_name] => Blank Template
)
[1] => Array
(
[template_id] => 91
[template_name] => Alphabet
)
[2] => Array
(
[template_id] => 31
[template_name] => Holiday Specials
)
)
LIVE EXAMPLE : CLICK HERE
I have the below array coming though, ideally I am looking for a way of matching one value and printing out another value.
e.g.
if($randomvalue == $cards[Card][unit_id]) { echo $cards[SaleDetail][date_pid_signed]; }
I'm not sure exactly how to go about getting the above to work with the current array structure as below.
Any ideas how I can get around this?
Thanks
$cards = Array
(
[0] => Array
(
[Card] => Array
(
[id] => 210
[property_id] => 4
[unit_id] => 90
)
[SaleDetail] => Array
(
[property_agent] =>
[date_pid_signed] => 2012-06-15
[property_date_listed] =>
)
)
[1] => Array
(
[Card] => Array
(
[id] => 209
[property_id] => 4
[unit_id] => 103
)
[SaleDetail] => Array
(
[property_agent] =>
[date_pid_signed] => 2011-10-21
[property_date_listed] =>
)
)
)
foreach($cards as $card){
if($randomvalue == $card[Card][unit_id]) {
echo $card[SaleDetail][date_pid_signed];
}
}
Use $cards[0]['Card']['unit_id'] and $cards[0]['SaleDetail']['date_pid_signed']. Notice the indexes [0]. You can then use [1].
You might also want to check foreach or for loops!
if($randomvalue == $cards[0][Card][unit_id]) { echo $cards[0][SaleDetail][date_pid_signed]; }
also you can do
foreach($cards as $card)
{
if($randomvalue == $card[Card][unit_id])
{
echo $card[SaleDetail][date_pid_signed];
}
}