PHP array push from JSON decode breaking array - php

$resultViewed='["88"]';
$viewed=json_decode($resultViewed);
if(!in_array("9",$viewed)){
print_r($viewed);
$viewed = array_push($viewed,"9");
print_r($viewed);
}
This prints
Array ( [0] => 88 ) 2
Instead of
Array ( [0] => 88,[1]=>9 )
The array is valid, yet using array_push() to add another value isn't working as I'd expect.

remove assignment: $viewed =:
$viewed = array_push($viewed,"9");
Just:
array_push($viewed,"9");
Its already in the manual, it returns the new number of items, not the values of the array.
Or just use the simple way:
$viewed[] = "9";

Related

How could I get value in an array or how could I convert array to string?

$tId = $this->its_model->get_status_type($property_id);
print_r($tId);
$tsId = $this->its_model->get_sub_status_type($tId);
The $tId returns this:
Array ( [0] => Array ( [tId] => 2 ) )
And now I need to use the value 2 in the second line of code. As it is in the form of array I am getting an error. How could I get the value only that is 2?
So if
$tId is
Array ( [0] => Array ( [tId] => 2 ) )
it should be simply
echo ( $tId[0]['tId'] ) // should print 2
I don't know what Data you are trying to return but if your data is one dimensional, instead of returning
$query->result_array();
you can simply use
$query->row_array();
This returns a single row/flat array rather than a multidimensional array that you need to select the index or loop through. It will be as easy as
echo $tId['tId']
Alternatively you can return an object by using
$query->row() or $query->result();
You can then call the value by using
echo $tId->tId;

pull dynamic element in single array

I am trying to pull dynamic element in single array but result is not showing properly
Ex:
$array =array();
$element="'abc1','abc2'";
$array=array('abc',$element);
//I want result like that:
array[
[0]=>abc,
[1]=>abc1,
[2]=>ab
]
If you need to parse a string of elements to an array you can use one of the csv functions. You may then merge your arrays.
$array = array('abc');
$string_elements = "'abc1','abc2'";
$array_elements = str_getcsv($string_elements, ',', "'");
$array = array_merge($array, $array_elements);
var_export($array);
Output:
array (
0 => 'abc',
1 => 'abc1',
2 => 'abc2',
)
Alternatively to add each array element to the end of another array you can push them like so using a splat:
array_push($array, ...$array_elements);
According to my understanding, $element is storing string not an array so even if you try to get output it will display in following format
Array
(
[0] => abc
[1] => 'abc1','abc2'
)
Instead of this you can store array in $element variable and use array_merge() function to get required output e.g.
$element = array('abc1','abc2');
$array = array('abc');
$result = array_merge($array,$element);
// Output
Array
(
[0] => abc
[1] => abc1
[2] => abc2
)

PHP Array_unique value error in while loop

This is my PHP script for getting plan
This is my table
plan
3|6
6|12
3|12
and
<?php
$tenure="SELECT plan from ".TABLE_TYBO_EMI_GATEWAY;
$t_result=dbQuery($tenure);
while($t_data=mysql_fetch_assoc($t_result))
{
$arrayVal=explode("|",$t_data['plan']);
print_r(array_unique($arrayVal));
}
?>
and I got the result is
Array ( [0] => 3 [1] => 6 ) Array ( [0] => 6 [1] => 12 )
Here I want 3,6,12 only. What is the problem in my script
before your while loop add this line:
$arrayVal = array();
and replace $arrayVal=explode("|",$t_data['plan']); with $arrayVal=array_merge($arrayVal, explode("|",$t_data['plan']));
$tenure="SELECT plan from ".TABLE_TYBO_EMI_GATEWAY;
$t_result=dbQuery($tenure);
$arrayVal = array();
while($t_data=mysql_fetch_assoc($t_result))
{
$arrayVal = array_merge($arrayVal, explode("|",$t_data['plan']));
}
print_r(array_unique($arrayVal));
Note: When using array_merge with associated arrays, it will overwrite values for same keys, but when using numeric keys array_merge will not overwrite them instead append as new values.

How do I access a variable from an stdClass object within an Array of Arrays?

I did a print_r on my array $total and it returned the following:
Array ( ) Array ( ) Array ( ) Array ( ) Array ( [0] => stdClass Object (
[generated] => 6 [magnitude] => 3 [log_pk] => 14 [result] => 0.5000 ) )
Array ( ) Array ( )
I need to be able to print out log_pk from within the stdClass Object. I have tried print $total[0]->log_pk but that was unsuccessful. The error was Undefined offset: 0. Any help is appreciated. Thanks.
So this is within a loop, you should check if the index 0 exists first.
if (isset($total[0])) echo $total[0]->log_pk
Are you doing this within a loop? If so then it looks like the array is empty on most iterations. Try:
if (!empty($total)) print $total[0]->log_pk;
var_dump() displays structured information about one or more expressions that includes its type and value. Arrays and objects are explored recursively with values indented to show structure.
var_dump($total)
PHP: var_dump - Manual
it looks like your print_r is inside a loop.
while(true){
$total = some_function();
print_r($total);
if($condition) break;
}
// Here - outside the loop, is not the right place for the print_r();
If you want you print outside the loop, you would change $total = some_function(); to $total[] = some_function(); and then you can do print_r($total[index])

How do i get passed an empty key in a php array

how to i get to the arrays content if it doesnt have a key like this
$products[0]
this will get me partially there but how to i get past []
( [0] => Array
( [] => Array (
[0] => Array ( [product_name] => stuff i need to get to )
That is very strange. You could try
$products[0][''][0]['product_name']
Use var_dump or var_export to print your array and you will see its an empty string.
print_r will give you output like that for empty strings.
$products[0][""][0]["product_name"]

Categories